Make WordPress Core

Ticket #19393: 19393.4.diff

File 19393.4.diff, 5.4 KB (added by nacin, 11 years ago)
  • src/wp-admin/includes/image.php

     
    9797                        else
    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
    103103                }
  • src/wp-includes/media.php

     
    185185 *
    186186 * @since 2.9.0
    187187 *
    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.
     188
     189 * Cropping behavior for the image size is dependent on the value of $crop:
     190 * 1. If false (default), images will be scaled, not cropped.
     191 * 2. If an array in the form of array( x_crop_position, y_crop_position ):
     192 *    - x_crop_position accepts 'left' 'center', or 'right'.
     193 *    - y_crop_position accepts 'top', 'center', or 'bottom'.
     194 *    Images will be cropped to the specified dimensions within the defined crop area.
     195 * 3. If true, images will be cropped to the specified dimensions using center positions.
     196 *
     197 * @since 2.5.0
     198 *
     199 * @param string     $name   Image size identifier.
     200 * @param int        $width  Image width in pixels.
     201 * @param int        $height Image height in pixels.
     202 * @param bool|array $crop   Optional. Whether to crop images to specified height and width or resize.
     203 *                           An array can specify positioning of the crop area. Default false.
     204 * @return bool|array False, if no image was created. Metadata array on success.
    192205 */
    193206function add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
    194207        global $_wp_additional_image_sizes;
     208
    195209        $_wp_additional_image_sizes[ $name ] = array(
    196210                'width'  => absint( $width ),
    197211                'height' => absint( $height ),
    198                 'crop'   => (bool) $crop,
     212                'crop'   => $crop,
    199213        );
    200214}
    201215
     
    342356}
    343357
    344358/**
    345  * Retrieve calculated resized dimensions for use in WP_Image_Editor.
     359 * Retrieve calculated resize dimensions for use in WP_Image_Editor.
    346360 *
    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.
     361 * Calculates dimensions and coordinates for a resized image that fits
     362 * within a specified width and height.
    350363 *
     364 * Cropping behavior is dependent on the value of $crop:
     365 * 1. If false (default), images will not be cropped.
     366 * 2. If an array in the form of array( x_crop_position, y_crop_position ):
     367 *    - x_crop_position accepts 'left' 'center', or 'right'.
     368 *    - y_crop_position accepts 'top', 'center', or 'bottom'.
     369 *    Images will be cropped to the specified dimensions within the defined crop area.
     370 * 3. If true, images will be cropped to the specified dimensions using center positions.
     371 *
    351372 * @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.
    354373 *
    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.
     374 * @uses apply_filters() Calls 'image_resize_dimensions' on $orig_w, $orig_h, $dest_w,
     375 *                       $dest_h and $crop to provide custom resize dimensions.
     376 *
     377 * @param int        $orig_w Original width in pixels.
     378 * @param int        $orig_h Original height in pixels.
     379 * @param int        $dest_w New width in pixels.
     380 * @param int        $dest_h New height in pixels.
     381 * @param bool|array $crop   Optional. Whether to crop image to specified height and width or resize.
     382 *                           An array can specify positioning of the crop area. Default false.
     383 * @return bool|array False on failure. Returned array matches parameters for `imagecopyresampled()`.
    361384 */
    362385function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) {
    363386
     
    391414                $crop_w = round($new_w / $size_ratio);
    392415                $crop_h = round($new_h / $size_ratio);
    393416
    394                 $s_x = floor( ($orig_w - $crop_w) / 2 );
    395                 $s_y = floor( ($orig_h - $crop_h) / 2 );
     417                if ( ! is_array( $crop ) || count( $crop ) !== 2 ) {
     418                        $crop = array( 'center', 'center' );
     419                }
     420
     421                list( $x, $y ) = $crop;
     422
     423                if ( 'left' === $x ) {
     424                        $s_x = 0;
     425                } elseif ( 'right' === $x ) {
     426                        $s_x = $orig_w - $crop_w;
     427                } else {
     428                        $s_x = floor( ( $orig_w - $crop_w ) / 2 );
     429                }
     430
     431                if ( 'top' === $y ) {
     432                        $s_y = 0;
     433                } elseif ( 'bottom' === $y ) {
     434                        $s_y = $orig_h - $crop_h;
     435                } else {
     436                        $s_y = floor( ( $orig_h - $crop_h ) / 2 );
     437                }
    396438        } else {
    397439                // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
    398440                $crop_w = $orig_w;