Make WordPress Core

Ticket #17626: 17626k.diff

File 17626k.diff, 3.6 KB (added by kitchin, 11 years ago)

Two-line mod to 17626.diff

  • wp-includes/media.php

     
    576576
    577577        // get the best one for a specified set of dimensions
    578578        if ( is_array($size) && !empty($imagedata['sizes']) ) {
     579                $candidate_sizes = array();
    579580                foreach ( $imagedata['sizes'] as $_size => $data ) {
    580                         // already cropped to width or height; so use this size
    581                         if ( ( $data['width'] == $size[0] && $data['height'] <= $size[1] ) || ( $data['height'] == $size[1] && $data['width'] <= $size[0] ) ) {
     581                        // If there's an exact match to an existing image size, short circuit.
     582                        if ( ( $data['width'] == $size[0] && $data['height'] == $size[1] ) ) {
    582583                                $file = $data['file'];
    583584                                list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
    584585                                return compact( 'file', 'width', 'height' );
    585586                        }
    586                         // add to lookup table: area => size
    587                         $areas[$data['width'] * $data['height']] = $_size;
     587                        // If it's not an exact match but it's at least the dimensions requested.
     588                        if ( $data['width'] >= $size[0] && $data['height'] >= $size[1] ) {
     589                                $candidate_sizes[$data['width'] * $data['height']] = $_size;
     590                        }
    588591                }
    589                 if ( !$size || !empty($areas) ) {
     592
     593                if ( $candidate_sizes ) {
    590594                        // find for the smallest image not smaller than the desired size
    591                         ksort($areas);
    592                         foreach ( $areas as $_size ) {
     595                        ksort($candidate_sizes);
     596                        foreach ( $candidate_sizes as $_size ) {
    593597                                $data = $imagedata['sizes'][$_size];
    594                                 if ( $data['width'] >= $size[0] || $data['height'] >= $size[1] ) {
    595                                         // Skip images with unexpectedly divergent aspect ratios (crops)
    596                                         // First, we calculate what size the original image would be if constrained to a box the size of the current image in the loop
    597                                         $maybe_cropped = image_resize_dimensions($imagedata['width'], $imagedata['height'], $data['width'], $data['height'], false );
    598                                         // 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
    599                                         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'] ) ) )
    600                                                 continue;
    601                                         // If we're still here, then we're going to use this size
    602                                         $file = $data['file'];
    603                                         list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
    604                                         return compact( 'file', 'width', 'height' );
    605                                 }
     598                                // Skip images with unexpectedly divergent aspect ratios (crops)
     599                                // First, we calculate what size the original image would be if constrained to a box the size of the current image in the loop
     600                                $maybe_cropped = image_resize_dimensions($imagedata['width'], $imagedata['height'], $data['width'], $data['height'], false );
     601                                // 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
     602                                if ( 'thumbnail' != $_size &&
     603                                        ( !$maybe_cropped
     604                                          || ( $maybe_cropped[4] != $data['width'] && $maybe_cropped[4] + 1 != $data['width'] )
     605                                          || ( $maybe_cropped[5] != $data['height'] && $maybe_cropped[5] + 1 != $data['height'] )
     606                                        ) )
     607                                        continue;
     608                                // If we're still here, then we're going to use this size
     609                                $file = $data['file'];
     610                                list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
     611                                return compact( 'file', 'width', 'height' );
    606612                        }
    607613                }
    608614        }