Make WordPress Core

Ticket #22738: 22738.3.diff

File 22738.3.diff, 2.9 KB (added by nacin, 11 years ago)
  • wp-includes/media.php

     
    2929 * @param int $width Width of the image
    3030 * @param int $height Height of the image
    3131 * @param string|array $size Size of what the result image should be.
     32 * @param context Could be 'display' (like in a theme) or 'edit' (like inserting into a neditor)
    3233 * @return array Width and height of what the result image should resize to.
    3334 */
    34 function image_constrain_size_for_editor($width, $height, $size = 'medium') {
     35function image_constrain_size_for_editor($width, $height, $size = 'medium', $context = null ) {
    3536        global $content_width, $_wp_additional_image_sizes;
    3637
     38        if ( ! $context )
     39                $context = is_admin() ? 'edit' : 'display';
     40
    3741        if ( is_array($size) ) {
    3842                $max_width = $size[0];
    3943                $max_height = $size[1];
     
    6468        } elseif ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) {
    6569                $max_width = intval( $_wp_additional_image_sizes[$size]['width'] );
    6670                $max_height = intval( $_wp_additional_image_sizes[$size]['height'] );
    67                 if ( intval($content_width) > 0 && is_admin() ) // Only in admin. Assume that theme authors know what they're doing.
     71                if ( intval($content_width) > 0 && 'edit' == $context ) // Only in admin. Assume that theme authors know what they're doing.
    6872                        $max_width = min( intval($content_width), $max_width );
    6973        }
    7074        // $size == 'full' has no constraint
     
    7377                $max_height = $height;
    7478        }
    7579
    76         list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size );
     80        list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size, $context );
    7781
    7882        return wp_constrain_dimensions( $width, $height, $max_width, $max_height );
    7983}
     
    13751379
    13761380                                // Nothing from the filter, so consult image metadata if we have it.
    13771381                                $size_meta = $meta['sizes'][ $size ];
     1382
     1383                                // We have the actual image size, but might need to further constrain it if content_width is narrower.
     1384                                // This is not necessary for thumbnails and medium size.
     1385                                if ( 'thumbnail' == $size || 'medium' == $size ) {
     1386                                        $width = $size_meta['width'];
     1387                                        $height = $size_meta['height'];
     1388                                } else {
     1389                                        list( $width, $height ) = image_constrain_size_for_editor( $size_meta['width'], $size_meta['height'], $size, 'edit' );
     1390                                }
     1391
    13781392                                $sizes[ $size ] = array(
    1379                                         'height'      => $size_meta['height'],
    1380                                         'width'       => $size_meta['width'],
     1393                                        'height'      => $height,
     1394                                        'width'       => $width,
    13811395                                        'url'         => $base_url . $size_meta['file'],
    1382                                         'orientation' => $size_meta['height'] > $size_meta['width'] ? 'portrait' : 'landscape',
     1396                                        'orientation' => $height > $width ? 'portrait' : 'landscape',
    13831397                                );
    13841398                        }
    13851399                }