WordPress.org

Make WordPress Core

Changeset 23096


Ignore:
Timestamp:
12/06/2012 06:25:39 AM (6 years ago)
Author:
markjaquith
Message:

Present the correct downsized image dimensions in the Media modal when inserting. Introduces a context parameter for image_constrain_size_for_editor() instead of relying on is_admin(). props jond3r, nacin. fixes #22738

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/media.php

    r23095 r23096  
    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;
     37
     38    if ( ! $context )
     39        $context = is_admin() ? 'edit' : 'display';
    3640
    3741    if ( is_array($size) ) {
     
    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    }
     
    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 );
     
    13791383                // Nothing from the filter, so consult image metadata if we have it.
    13801384                $size_meta = $meta['sizes'][ $size ];
     1385
     1386                // We have the actual image size, but might need to further constrain it if content_width is narrower.
     1387                // This is not necessary for thumbnails and medium size.
     1388                if ( 'thumbnail' == $size || 'medium' == $size ) {
     1389                    $width = $size_meta['width'];
     1390                    $height = $size_meta['height'];
     1391                } else {
     1392                    list( $width, $height ) = image_constrain_size_for_editor( $size_meta['width'], $size_meta['height'], $size, 'edit' );
     1393                }
     1394
    13811395                $sizes[ $size ] = array(
    1382                     'height'      => $size_meta['height'],
    1383                     'width'       => $size_meta['width'],
     1396                    'height'      => $height,
     1397                    'width'       => $width,
    13841398                    'url'         => $base_url . $size_meta['file'],
    1385                     'orientation' => $size_meta['height'] > $size_meta['width'] ? 'portrait' : 'landscape',
     1399                    'orientation' => $height > $width ? 'portrait' : 'landscape',
    13861400                );
    13871401            }
Note: See TracChangeset for help on using the changeset viewer.