Make WordPress Core

Ticket #19393: 19393.2.diff

File 19393.2.diff, 4.6 KB (added by kirasong, 10 years ago)

Refreshed and updated docs for image_resize_dimensions() to reflect optional array argument.

  • src/wp-admin/includes/image.php

    diff --git src/wp-admin/includes/image.php src/wp-admin/includes/image.php
    index 23cce46..07c4f8e 100644
    function wp_generate_attachment_metadata( $attachment_id, $file ) { 
    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

    diff --git src/wp-includes/media.php src/wp-includes/media.php
    index e62753a..9982955 100644
    function image_downsize($id, $size = 'medium') { 
    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 * In addition to true/false, the $crop parameter takes an array of the format
     189 *  array( x_crop_position, y_crop_position )
     190 *  x_crop_position can be 'left', 'center', 'right'
     191 *  y_crop_position can be 'top', 'center', 'bottom'
     192 *
     193 * @param string $name Image size identifier.
     194 * @param int $width Image width.
     195 * @param int $height Image height.
     196 * @param bool|array $crop Optional, default is false. Whether to crop image to specified height and width or resize. An array can specify positioning of the crop area.
     197 * @return bool|array False, if no image was created. Metadata array on success.
    192198 */
    193199function add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
    194200        global $_wp_additional_image_sizes;
     201
    195202        $_wp_additional_image_sizes[ $name ] = array(
    196203                'width'  => absint( $width ),
    197204                'height' => absint( $height ),
    198                 'crop'   => (bool) $crop,
     205                'crop'   => $crop,
    199206        );
    200207}
    201208
    function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, 
    345352 * Retrieve calculated resized dimensions for use in WP_Image_Editor.
    346353 *
    347354 * 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.
     355 * specified width and height.
     356 *
     357 * If $crop is true, the largest matching central portion of the image will
     358 * be cropped out and resized to the required size.
     359 *
     360 * In addition to true/false, the $crop parameter takes an array of the format
     361 *  array( x_crop_position, y_crop_position )
     362 *  x_crop_position can be 'left', 'center', 'right'
     363 *  y_crop_position can be 'top', 'center', 'bottom'
    350364 *
    351365 * @since 2.5.0
    352366 * @uses apply_filters() Calls 'image_resize_dimensions' on $orig_w, $orig_h, $dest_w, $dest_h and
    function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, 
    356370 * @param int $orig_h Original height.
    357371 * @param int $dest_w New width.
    358372 * @param int $dest_h New height.
    359  * @param bool $crop Optional, default is false. Whether to crop image or resize.
     373 * @param bool|array $crop Optional, default is false. Whether to crop image to specified height and width or resize. An array can specify positioning of the crop area.
    360374 * @return bool|array False on failure. Returned array matches parameters for imagecopyresampled() PHP function.
    361375 */
    362376function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) {
    function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = fal 
    391405                $crop_w = round($new_w / $size_ratio);
    392406                $crop_h = round($new_h / $size_ratio);
    393407
    394                 $s_x = floor( ($orig_w - $crop_w) / 2 );
    395                 $s_y = floor( ($orig_h - $crop_h) / 2 );
     408                if ( ! is_array( $crop ) || count( $crop ) !== 2 ) {
     409                        $crop = apply_filters( 'image_resize_crop_default', array( 'center', 'center' ) );
     410                }
     411
     412                @list( $x, $y ) = $crop;
     413
     414                switch ( $x ) {
     415                        case 'left':
     416                                $s_x = 0;
     417                                break;
     418                        case 'right':
     419                                $s_x = $orig_w - $crop_w;
     420                                break;
     421                        default:
     422                                $s_x = floor( ( $orig_w - $crop_w ) / 2 );
     423                                break;
     424                }
     425
     426                switch ( $y ) {
     427                        case 'top':
     428                                $s_y = 0;
     429                                break;
     430                        case 'bottom':
     431                                $s_y = $orig_h - $crop_h;
     432                                break;
     433                        default:
     434                                $s_y = floor( ( $orig_h - $crop_h ) / 2 );
     435                                break;
     436                }
    396437        } else {
    397438                // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
    398439                $crop_w = $orig_w;