intermediateSize.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * @group image
  4. * @group media
  5. * @group upload
  6. */
  7. class Tests_Image_Intermediate_Size extends WP_UnitTestCase {
  8. function tearDown() {
  9. $this->remove_added_uploads();
  10. remove_image_size( 'test-size' );
  11. remove_image_size( 'false-height' );
  12. remove_image_size( 'false-width' );
  13. remove_image_size( 'off-by-one' );
  14. parent::tearDown();
  15. }
  16. public function _make_attachment( $file, $parent_post_id = 0 ) {
  17. $contents = file_get_contents( $file );
  18. $upload = wp_upload_bits( wp_basename( $file ), null, $contents );
  19. return parent::_make_attachment( $upload, $parent_post_id );
  20. }
  21. function test_make_intermediate_size_no_size() {
  22. $image = image_make_intermediate_size( DIR_TESTDATA . '/images/a2-small.jpg', 0, 0, false );
  23. $this->assertFalse( $image );
  24. }
  25. /**
  26. * @requires function imagejpeg
  27. */
  28. function test_make_intermediate_size_width() {
  29. $image = image_make_intermediate_size( DIR_TESTDATA . '/images/a2-small.jpg', 100, 0, false );
  30. $this->assertInternalType( 'array', $image );
  31. }
  32. /**
  33. * @requires function imagejpeg
  34. */
  35. function test_make_intermediate_size_height() {
  36. $image = image_make_intermediate_size( DIR_TESTDATA . '/images/a2-small.jpg', 0, 75, false );
  37. $this->assertInternalType( 'array', $image );
  38. }
  39. /**
  40. * @requires function imagejpeg
  41. */
  42. function test_make_intermediate_size_successful() {
  43. $image = image_make_intermediate_size( DIR_TESTDATA . '/images/a2-small.jpg', 100, 75, true );
  44. $this->assertInternalType( 'array', $image );
  45. $this->assertSame( 100, $image['width'] );
  46. $this->assertSame( 75, $image['height'] );
  47. $this->assertSame( 'image/jpeg', $image['mime-type'] );
  48. $this->assertFalse( isset( $image['path'] ) );
  49. unlink( DIR_TESTDATA . '/images/a2-small-100x75.jpg' );
  50. }
  51. /**
  52. * @ticket 17626
  53. * @requires function imagejpeg
  54. */
  55. function test_get_intermediate_sizes_by_name() {
  56. add_image_size( 'test-size', 330, 220, true );
  57. $file = DIR_TESTDATA . '/images/waffles.jpg';
  58. $id = $this->_make_attachment( $file, 0 );
  59. // Look for a size by name.
  60. $image = image_get_intermediate_size( $id, 'test-size' );
  61. // Cleanup.
  62. remove_image_size( 'test-size' );
  63. // Test for the expected string because the array will by definition
  64. // return with the correct height and width attributes.
  65. $this->assertTrue( strpos( $image['file'], '330x220' ) > 0 );
  66. }
  67. /**
  68. * @ticket 17626
  69. * @requires function imagejpeg
  70. */
  71. function test_get_intermediate_sizes_by_array_exact() {
  72. // Only one dimention match shouldn't return false positive (see: #17626).
  73. add_image_size( 'test-size', 330, 220, true );
  74. add_image_size( 'false-height', 330, 400, true );
  75. add_image_size( 'false-width', 600, 220, true );
  76. $file = DIR_TESTDATA . '/images/waffles.jpg';
  77. $id = $this->_make_attachment( $file, 0 );
  78. // Look for a size by array that exists.
  79. // Note: Staying larger than 300px to miss default medium crop.
  80. $image = image_get_intermediate_size( $id, array( 330, 220 ) );
  81. // Test for the expected string because the array will by definition
  82. // return with the correct height and width attributes.
  83. $this->assertTrue( strpos( $image['file'], '330x220' ) > 0 );
  84. }
  85. /**
  86. * @ticket 17626
  87. * @requires function imagejpeg
  88. */
  89. function test_get_intermediate_sizes_by_array_nearest() {
  90. // If an exact size is not found, it should be returned.
  91. // If not, find nearest size that is larger (see: #17626).
  92. add_image_size( 'test-size', 450, 300, true );
  93. add_image_size( 'false-height', 330, 100, true );
  94. add_image_size( 'false-width', 150, 220, true );
  95. $file = DIR_TESTDATA . '/images/waffles.jpg';
  96. $id = $this->_make_attachment( $file, 0 );
  97. // Look for a size by array that doesn't exist.
  98. // Note: Staying larger than 300px to miss default medium crop.
  99. $image = image_get_intermediate_size( $id, array( 330, 220 ) );
  100. // Test for the expected string because the array will by definition
  101. // return with the correct height and width attributes.
  102. $this->assertTrue( strpos( $image['file'], '450x300' ) > 0 );
  103. }
  104. /**
  105. * @ticket 17626
  106. */
  107. function test_get_intermediate_sizes_by_array_nearest_false() {
  108. // If an exact size is not found, it should be returned.
  109. // If not, find nearest size that is larger, otherwise return false (see: #17626).
  110. add_image_size( 'false-height', 330, 100, true );
  111. add_image_size( 'false-width', 150, 220, true );
  112. $file = DIR_TESTDATA . '/images/waffles.jpg';
  113. $id = $this->_make_attachment( $file, 0 );
  114. // Look for a size by array that doesn't exist.
  115. // Note: Staying larger than 300px to miss default medium crop.
  116. $image = image_get_intermediate_size( $id, array( 330, 220 ) );
  117. // Test for the expected string because the array will by definition
  118. // return with the correct height and width attributes.
  119. $this->assertFalse( $image );
  120. }
  121. /**
  122. * @ticket 17626
  123. * @requires function imagejpeg
  124. */
  125. function test_get_intermediate_sizes_by_array_zero_height() {
  126. // Generate random width.
  127. $random_w = rand( 300, 400 );
  128. // Only one dimention match shouldn't return false positive (see: #17626).
  129. add_image_size( 'test-size', $random_w, 0, false );
  130. add_image_size( 'false-height', $random_w, 100, true );
  131. $file = DIR_TESTDATA . '/images/waffles.jpg';
  132. $id = $this->_make_attachment( $file, 0 );
  133. $original = wp_get_attachment_metadata( $id );
  134. $image_w = $random_w;
  135. $image_h = round( ( $image_w / $original['width'] ) * $original['height'] );
  136. // Look for a size by array that exists.
  137. // Note: Staying larger than 300px to miss default medium crop.
  138. $image = image_get_intermediate_size( $id, array( $random_w, 0 ) );
  139. // Test for the expected string because the array will by definition
  140. // return with the correct height and width attributes.
  141. $this->assertTrue( strpos( $image['file'], $image_w . 'x' . $image_h ) > 0 );
  142. }
  143. /**
  144. * @ticket 17626
  145. * @ticket 34087
  146. * @requires function imagejpeg
  147. */
  148. function test_get_intermediate_sizes_by_array_zero_width() {
  149. // 202 is the smallest height that will trigger a miss for 'false-height'.
  150. $height = 202;
  151. // Only one dimention match shouldn't return false positive (see: #17626).
  152. add_image_size( 'test-size', 0, $height, false );
  153. add_image_size( 'false-height', 300, $height, true );
  154. $file = DIR_TESTDATA . '/images/waffles.jpg';
  155. $id = $this->_make_attachment( $file, 0 );
  156. $original = wp_get_attachment_metadata( $id );
  157. $image_h = $height;
  158. $image_w = round( ( $image_h / $original['height'] ) * $original['width'] );
  159. // Look for a size by array that exists.
  160. // Note: Staying larger than 300px to miss default medium crop.
  161. $image = image_get_intermediate_size( $id, array( 0, $height ) );
  162. // Test for the expected string because the array will by definition
  163. // return with the correct height and width attributes.
  164. $this->assertTrue( strpos( $image['file'], $image_w . 'x' . $image_h ) > 0 );
  165. }
  166. /**
  167. * @ticket 17626
  168. * @ticket 34087
  169. * @requires function imagejpeg
  170. */
  171. public function test_get_intermediate_sizes_should_match_size_with_off_by_one_aspect_ratio() {
  172. // Original is 600x400. 300x201 is close enough to match.
  173. $width = 300;
  174. $height = 201;
  175. add_image_size( 'off-by-one', $width, $height, true );
  176. $file = DIR_TESTDATA . '/images/waffles.jpg';
  177. $id = $this->_make_attachment( $file, 0 );
  178. $original = wp_get_attachment_metadata( $id );
  179. $image_h = $height;
  180. $image_w = round( ( $image_h / $original['height'] ) * $original['width'] );
  181. // Look for a size by array that exists.
  182. // Note: Staying larger than 300px to miss default medium crop.
  183. $image = image_get_intermediate_size( $id, array( 0, $height ) );
  184. $this->assertTrue( strpos( $image['file'], $width . 'x' . $height ) > 0 );
  185. }
  186. /**
  187. * @ticket 34384
  188. * @requires function imagejpeg
  189. */
  190. public function test_get_intermediate_size_with_small_size_array() {
  191. // Add a hard cropped size that matches the aspect ratio we're going to test.
  192. add_image_size( 'test-size', 200, 100, true );
  193. $file = DIR_TESTDATA . '/images/waffles.jpg';
  194. $id = $this->_make_attachment( $file, 0 );
  195. // Request a size by array that doesn't exist and is smaller than the 'thumbnail'.
  196. $image = image_get_intermediate_size( $id, array( 50, 25 ) );
  197. // We should get the 'test-size' file and not the thumbnail.
  198. $this->assertTrue( strpos( $image['file'], '200x100' ) > 0 );
  199. }
  200. /**
  201. * @ticket 34384
  202. * @requires function imagejpeg
  203. */
  204. public function test_get_intermediate_size_with_small_size_array_fallback() {
  205. $file = DIR_TESTDATA . '/images/waffles.jpg';
  206. $id = $this->_make_attachment( $file, 0 );
  207. $original = wp_get_attachment_metadata( $id );
  208. $thumbnail_file = $original['sizes']['thumbnail']['file'];
  209. // Request a size by array that doesn't exist and is smaller than the 'thumbnail'.
  210. $image = image_get_intermediate_size( $id, array( 50, 25 ) );
  211. // We should get the 'thumbnail' file as a fallback.
  212. $this->assertSame( $image['file'], $thumbnail_file );
  213. }
  214. }