WordPress.org

Make WordPress Core


Ignore:
Timestamp:
03/08/2014 06:13:27 AM (6 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.