Make WordPress Core

Changeset 27472


Ignore:
Timestamp:
03/08/2014 06:13:27 AM (11 years ago)
Author:
nacin
Message:

Allow $crop in add_image_size() to receive crop anchors (top, left, right, bottom, center).

This also applies to set_post_thumbnail_size() and image_resize_dimensions().

props bradt, wonderboymusic, DH-Shredder.
fixes #19393.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/image.php

    r27202 r27472  
    9898                $sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
    9999            if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) )
    100                 $sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes
     100                $sizes[$s]['crop'] = $_wp_additional_image_sizes[$s]['crop']; // For theme-added sizes
    101101            else
    102102                $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
  • trunk/src/wp-includes/media.php

    r27445 r27472  
    184184 * Register a new image size.
    185185 *
     186 * Cropping behavior for the image size is dependent on the value of $crop:
     187 * 1. If false (default), images will be scaled, not cropped.
     188 * 2. If an array in the form of array( x_crop_position, y_crop_position ):
     189 *    - x_crop_position accepts 'left' 'center', or 'right'.
     190 *    - y_crop_position accepts 'top', 'center', or 'bottom'.
     191 *    Images will be cropped to the specified dimensions within the defined crop area.
     192 * 3. If true, images will be cropped to the specified dimensions using center positions.
     193 *
    186194 * @since 2.9.0
    187195 *
    188  * @param string $name   The image size name.
    189  * @param int    $width  The image's width.
    190  * @param int    $height The image's width.
    191  * @param bool   $width  Whether to crop the image to fit the dimensions. Default false.
     196 * @param string     $name   Image size identifier.
     197 * @param int        $width  Image width in pixels.
     198 * @param int        $height Image height in pixels.
     199 * @param bool|array $crop   Optional. Whether to crop images to specified height and width or resize.
     200 *                           An array can specify positioning of the crop area. Default false.
     201 * @return bool|array False, if no image was created. Metadata array on success.
    192202 */
    193203function add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
    194204    global $_wp_additional_image_sizes;
     205
    195206    $_wp_additional_image_sizes[ $name ] = array(
    196207        'width'  => absint( $width ),
    197208        'height' => absint( $height ),
    198         'crop'   => (bool) $crop,
     209        'crop'   => $crop,
    199210    );
    200211}
     
    234245
    235246/**
    236  * Registers an image size for the post thumbnail
     247 * Registers an image size for the post thumbnail.
    237248 *
    238249 * @since 2.9.0
     250 * @see add_image_size() for details on cropping behavior.
     251 *
     252 * @param int        $width  Image width in pixels.
     253 * @param int        $height Image height in pixels.
     254 * @param bool|array $crop   Optional. Whether to crop images to specified height and width or resize.
     255 *                           An array can specify positioning of the crop area. Default false.
     256 * @return bool|array False, if no image was created. Metadata array on success.
    239257 */
    240258function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) {
     
    343361
    344362/**
    345  * Retrieve calculated resized dimensions for use in WP_Image_Editor.
    346  *
    347  * Calculate dimensions and coordinates for a resized image that fits within a
    348  * specified width and height. If $crop is true, the largest matching central
    349  * portion of the image will be cropped out and resized to the required size.
     363 * Retrieve calculated resize dimensions for use in WP_Image_Editor.
     364 *
     365 * Calculates dimensions and coordinates for a resized image that fits
     366 * within a specified width and height.
     367 *
     368 * Cropping behavior is dependent on the value of $crop:
     369 * 1. If false (default), images will not be cropped.
     370 * 2. If an array in the form of array( x_crop_position, y_crop_position ):
     371 *    - x_crop_position accepts 'left' 'center', or 'right'.
     372 *    - y_crop_position accepts 'top', 'center', or 'bottom'.
     373 *    Images will be cropped to the specified dimensions within the defined crop area.
     374 * 3. If true, images will be cropped to the specified dimensions using center positions.
    350375 *
    351376 * @since 2.5.0
    352  * @uses apply_filters() Calls 'image_resize_dimensions' on $orig_w, $orig_h, $dest_w, $dest_h and
    353  *      $crop to provide custom resize dimensions.
    354  *
    355  * @param int $orig_w Original width.
    356  * @param int $orig_h Original height.
    357  * @param int $dest_w New width.
    358  * @param int $dest_h New height.
    359  * @param bool $crop Optional, default is false. Whether to crop image or resize.
    360  * @return bool|array False on failure. Returned array matches parameters for imagecopyresampled() PHP function.
     377 * @uses apply_filters() Calls 'image_resize_dimensions' on $orig_w, $orig_h, $dest_w,
     378 *                       $dest_h and $crop to provide custom resize dimensions.
     379 *
     380 * @param int        $orig_w Original width in pixels.
     381 * @param int        $orig_h Original height in pixels.
     382 * @param int        $dest_w New width in pixels.
     383 * @param int        $dest_h New height in pixels.
     384 * @param bool|array $crop   Optional. Whether to crop image to specified height and width or resize.
     385 *                           An array can specify positioning of the crop area. Default false.
     386 * @return bool|array False on failure. Returned array matches parameters for `imagecopyresampled()`.
    361387 */
    362388function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) {
     
    392418        $crop_h = round($new_h / $size_ratio);
    393419
    394         $s_x = floor( ($orig_w - $crop_w) / 2 );
    395         $s_y = floor( ($orig_h - $crop_h) / 2 );
     420        if ( ! is_array( $crop ) || count( $crop ) !== 2 ) {
     421            $crop = array( 'center', 'center' );
     422        }
     423
     424        list( $x, $y ) = $crop;
     425
     426        if ( 'left' === $x ) {
     427            $s_x = 0;
     428        } elseif ( 'right' === $x ) {
     429            $s_x = $orig_w - $crop_w;
     430        } else {
     431            $s_x = floor( ( $orig_w - $crop_w ) / 2 );
     432        }
     433
     434        if ( 'top' === $y ) {
     435            $s_y = 0;
     436        } elseif ( 'bottom' === $y ) {
     437            $s_y = $orig_h - $crop_h;
     438        } else {
     439            $s_y = floor( ( $orig_h - $crop_h ) / 2 );
     440        }
    396441    } else {
    397442        // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
  • trunk/tests/phpunit/tests/image/dimensions.php

    r25002 r27472  
    129129    }
    130130
     131    /**
     132     * @ticket 19393
     133     */
     134    function test_crop_anchors() {
     135        // landscape: crop 640x480 to fit 400x500: 400x400 taken from a 480x480 crop
     136        // src_x = 0 (left), src_y = 0 (top)
     137        $out = image_resize_dimensions(640, 480, 400, 500, array( 'left', 'top' ) );
     138        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     139        $this->assertEquals( array(0, 0, 0, 0, 400, 480, 400, 480), $out );
     140
     141        // portrait: resize 480x640 to fit 400x400: 400x400 taken from a 480x480 crop
     142        // src_x = 0 (left), src_y = 0 (top)
     143        $out = image_resize_dimensions(480, 640, 400, 500, array( 'left', 'top' ) );
     144        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     145        $this->assertEquals( array(0, 0, 0, 0, 400, 500, 480, 600), $out );
     146
     147        // landscape: crop 640x480 to fit 400x500: 400x400 taken from a 480x480 crop
     148        // src_x = 240 (left), src_y = 0 (due to landscape crop)
     149        $out = image_resize_dimensions(640, 480, 400, 500, array( 'right', 'bottom' ) );
     150        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     151        $this->assertEquals( array(0, 0, 240, 0, 400, 480, 400, 480), $out );
     152
     153        // portrait: resize 480x640 to fit 400x400: 400x400 taken from a 480x480 crop
     154        // src_x = 0 (due to portrait crop), src_y = 40 (bottom)
     155        $out = image_resize_dimensions(480, 640, 400, 500, array( 'right', 'bottom' ) );
     156        // dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h
     157        $this->assertEquals( array(0, 0, 0, 40, 400, 500, 480, 600), $out );
     158    }
     159
    131160}
Note: See TracChangeset for help on using the changeset viewer.