<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[33807] trunk: Improve the reliability of the crop returned by `image_get_intermediate_size()`.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/33807">33807</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/33807","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>wonderboymusic</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-08-30 02:40:40 +0000 (Sun, 30 Aug 2015)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Improve the reliability of the crop returned by `image_get_intermediate_size()`.

Add a bunch of unit tests to `tests/image/intermediate_size.php`.

Props joemcgill, ericlewis, kitchin, SergeyBiryukov, chipbennett.
Fixes <a href="https://core.trac.wordpress.org/ticket/17626">#17626</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesmediaphp">trunk/src/wp-includes/media.php</a></li>
<li><a href="#trunktestsphpunittestsimageintermediate_sizephp">trunk/tests/phpunit/tests/image/intermediate_size.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesmediaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/media.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/media.php   2015-08-30 02:18:18 UTC (rev 33806)
+++ trunk/src/wp-includes/media.php     2015-08-30 02:40:40 UTC (rev 33807)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -607,35 +607,42 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        // get the best one for a specified set of dimensions
</span><span class="cx" style="display: block; padding: 0 10px">        if ( is_array($size) && !empty($imagedata['sizes']) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $areas = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $candidates = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $imagedata['sizes'] as $_size => $data ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // already cropped to width or height; so use this size
-                       if ( ( $data['width'] == $size[0] && $data['height'] <= $size[1] ) || ( $data['height'] == $size[1] && $data['width'] <= $size[0] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // If there's an exact match to an existing image size, short circuit.
+                       if ( $data['width'] == $size[0] && $data['height'] == $size[1] ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                 $file = $data['file'];
</span><span class="cx" style="display: block; padding: 0 10px">                                list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
</span><span class="cx" style="display: block; padding: 0 10px">                                return compact( 'file', 'width', 'height' );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // add to lookup table: area => size
-                       $areas[$data['width'] * $data['height']] = $_size;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // If it's not an exact match but it's at least the dimensions requested.
+                       if ( $data['width'] >= $size[0] && $data['height'] >= $size[1] ) {
+                               $candidates[ $data['width'] * $data['height'] ] = $_size;
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( !$size || !empty($areas) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               if ( ! empty( $candidates ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         // find for the smallest image not smaller than the desired size
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        ksort($areas);
-                       foreach ( $areas as $_size ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 ksort( $candidates );
+                       foreach ( $candidates as $_size ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                 $data = $imagedata['sizes'][$_size];
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( $data['width'] >= $size[0] || $data['height'] >= $size[1] ) {
-                                       // Skip images with unexpectedly divergent aspect ratios (crops)
-                                       // First, we calculate what size the original image would be if constrained to a box the size of the current image in the loop
-                                       $maybe_cropped = image_resize_dimensions($imagedata['width'], $imagedata['height'], $data['width'], $data['height'], false );
-                                       // If the size doesn't match within one pixel, then it is of a different aspect ratio, so we skip it, unless it's the thumbnail size
-                                       if ( 'thumbnail' != $_size && ( !$maybe_cropped || ( $maybe_cropped[4] != $data['width'] && $maybe_cropped[4] + 1 != $data['width'] ) || ( $maybe_cropped[5] != $data['height'] && $maybe_cropped[5] + 1 != $data['height'] ) ) )
-                                               continue;
-                                       // If we're still here, then we're going to use this size
-                                       $file = $data['file'];
-                                       list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
-                                       return compact( 'file', 'width', 'height' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               // Skip images with unexpectedly divergent aspect ratios (crops)
+                               // First, we calculate what size the original image would be if constrained to a box the size of the current image in the loop
+                               $maybe_cropped = image_resize_dimensions($imagedata['width'], $imagedata['height'], $data['width'], $data['height'], false );
+                               // If the size doesn't match within one pixel, then it is of a different aspect ratio, so we skip it, unless it's the thumbnail size
+                               if ( 'thumbnail' != $_size &&
+                                 ( ! $maybe_cropped
+                                   || ( $maybe_cropped[4] != $data['width'] && $maybe_cropped[4] + 1 != $data['width'] )
+                                   || ( $maybe_cropped[5] != $data['height'] && $maybe_cropped[5] + 1 != $data['height'] )
+                                 ) ) {
+                                 continue;
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                // If we're still here, then we're going to use this size
+                               $file = $data['file'];
+                               list( $width, $height ) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
+                               return compact( 'file', 'width', 'height' );
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span></span></pre></div>
<a id="trunktestsphpunittestsimageintermediate_sizephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/image/intermediate_size.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/image/intermediate_size.php     2015-08-30 02:18:18 UTC (rev 33806)
+++ trunk/tests/phpunit/tests/image/intermediate_size.php       2015-08-30 02:40:40 UTC (rev 33807)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -10,6 +10,40 @@
</span><span class="cx" style="display: block; padding: 0 10px">                parent::tearDown();
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Upload files and create attachements for testing
+        */
+       private function _make_attachment( $file, $parent_post_id = 0 ) {
+               $contents = file_get_contents( $file );
+               $upload = wp_upload_bits( basename( $file ), null, $contents );
+
+               $type = '';
+               if ( ! empty( $upload['type'] ) ) {
+                       $type = $upload['type'];
+               } else {
+                       $mime = wp_check_filetype( $upload['file'] );
+                       if ( $mime ) {
+                               $type = $mime['type'];
+                       }
+               }
+
+               $attachment = array(
+                       'post_title' => basename( $upload['file'] ),
+                       'post_content' => '',
+                       'post_type' => 'attachment',
+                       'post_parent' => $parent_post_id,
+                       'post_mime_type' => $type,
+                       'guid' => $upload['url'],
+               );
+
+               // Save the data
+               $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id );
+               wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
+
+               $this->ids[] = $id;
+               return $id;
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         function test_make_intermediate_size_no_size() {
</span><span class="cx" style="display: block; padding: 0 10px">                $image = image_make_intermediate_size( DIR_TESTDATA . '/images/a2-small.jpg', 0, 0, false );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -49,4 +83,164 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                unlink( DIR_TESTDATA . '/images/a2-small-100x75.jpg' );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+       * @ticket 17626
+       */
+       function test_get_intermediate_sizes_by_name() {
+               add_image_size( 'test-size', 330, 220, true );
+
+               $file = DIR_TESTDATA . '/images/waffles.jpg';
+               $id = $this->_make_attachment( $file, 0 );
+
+               // look for a size by name
+               $image = image_get_intermediate_size( $id, 'test-size' );
+
+               // test for the expected string because the array will by definition
+               // return with the correct height and width attributes
+               $this->assertNotFalse( strpos( $image['file'], '330x220' ) );
+
+               // cleanup
+               remove_image_size( 'test-size' );
+       }
+
+       /**
+       * @ticket 17626
+       */
+       function test_get_intermediate_sizes_by_array_exact() {
+               // Only one dimention match shouldn't return false positive (see: 17626)
+               add_image_size( 'test-size', 330, 220, true );
+               add_image_size( 'false-height', 330, 400, true );
+               add_image_size( 'false-width', 600, 220, true );
+
+               $file = DIR_TESTDATA . '/images/waffles.jpg';
+               $id = $this->_make_attachment( $file, 0 );
+
+               // look for a size by array that exists
+               // note: staying larger than 300px to miss default medium crop
+               $image = image_get_intermediate_size( $id, array( 330, 220 ) );
+
+               // test for the expected string because the array will by definition
+               // return with the correct height and width attributes
+               $this->assertNotFalse( strpos( $image['file'], '330x220' ) );
+
+               // cleanup
+               remove_image_size( 'test-size' );
+               remove_image_size( 'false-height' );
+               remove_image_size( 'false-width' );
+       }
+
+       /**
+       * @ticket 17626
+       */
+       function test_get_intermediate_sizes_by_array_nearest() {
+               // If an exact size is not found, it should be returned
+               // If not, find nearest size that is larger (see: 17626)
+               add_image_size( 'test-size', 450, 300, true );
+               add_image_size( 'false-height', 330, 100, true );
+               add_image_size( 'false-width', 150, 220, true );
+
+               $file = DIR_TESTDATA . '/images/waffles.jpg';
+               $id = $this->_make_attachment( $file, 0 );
+
+               // look for a size by array that doesn't exist
+               // note: staying larger than 300px to miss default medium crop
+               $image = image_get_intermediate_size( $id, array( 330, 220 ) );
+
+               // you have to test for the string because the image will by definition
+               // return with the correct height and width attributes
+               $this->assertNotFalse( strpos( $image['file'], '450x300' ) );
+
+               // cleanup
+               remove_image_size( 'test-size' );
+               remove_image_size( 'false-height' );
+               remove_image_size( 'false-width' );
+       }
+
+       /**
+       * @ticket 17626
+       */
+       function test_get_intermediate_sizes_by_array_nearest_false() {
+               // If an exact size is not found, it should be returned
+               // If not, find nearest size that is larger, otherwise return false (see: 17626)
+               add_image_size( 'false-height', 330, 100, true );
+               add_image_size( 'false-width', 150, 220, true );
+
+               $file = DIR_TESTDATA . '/images/waffles.jpg';
+               $id = $this->_make_attachment( $file, 0 );
+
+               // look for a size by array that doesn't exist
+               // note: staying larger than 300px to miss default medium crop
+               $image = image_get_intermediate_size( $id, array( 330, 220 ) );
+
+               // you have to test for the string because the image will by definition
+               // return with the correct height and width attributes
+               $this->assertFalse( $image );
+
+               // cleanup
+               remove_image_size( 'false-height' );
+               remove_image_size( 'false-width' );
+       }
+
+       /**
+       * @ticket 17626
+       */
+       function test_get_intermediate_sizes_by_array_zero_height() {
+               // Generate random width
+               $random_w = rand( 300, 400 );
+
+               // Only one dimention match shouldn't return false positive (see: 17626)
+               add_image_size( 'test-size', $random_w, 0, false );
+               add_image_size( 'false-height', $random_w, 100, true );
+
+               $file = DIR_TESTDATA . '/images/waffles.jpg';
+               $id = $this->_make_attachment( $file, 0 );
+
+               $original = wp_get_attachment_metadata( $id );
+               $image_w = $random_w;
+               $image_h = round( ( $image_w / $original['width'] ) * $original['height'] );
+
+               // look for a size by array that exists
+               // note: staying larger than 300px to miss default medium crop
+               $image = image_get_intermediate_size( $id, array( $random_w, 0 ) );
+
+               // test for the expected string because the array will by definition
+               // return with the correct height and width attributes
+               $this->assertNotFalse( strpos( $image['file'], $image_w . 'x' . $image_h ) );
+
+               // cleanup
+               remove_image_size( 'test-size' );
+               remove_image_size( 'false-height' );
+       }
+
+       /**
+       * @ticket 17626
+       */
+       function test_get_intermediate_sizes_by_array_zero_width() {
+               // Generate random height
+               $random_h = rand( 200, 300 );
+
+               // Only one dimention match shouldn't return false positive (see: 17626)
+               add_image_size( 'test-size', 0, $random_h, false );
+               add_image_size( 'false-height', 300, $random_h, true );
+
+               $file = DIR_TESTDATA . '/images/waffles.jpg';
+               $id = $this->_make_attachment( $file, 0 );
+
+               $original = wp_get_attachment_metadata( $id );
+               $image_h = $random_h;
+               $image_w = round( ( $image_h / $original['height'] ) * $original['width'] );
+
+               // look for a size by array that exists
+               // note: staying larger than 300px to miss default medium crop
+               $image = image_get_intermediate_size( $id, array( 0, $random_h ) );
+
+               // test for the expected string because the array will by definition
+               // return with the correct height and width attributes
+               $this->assertNotFalse( strpos( $image['file'], $image_w . 'x' . $image_h ) );
+
+               // cleanup
+               remove_image_size( 'test-size' );
+               remove_image_size( 'false-height' );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>