Ticket #19393: wp-image-crop-position.patch

File wp-image-crop-position.patch, 2.7 KB (added by bradt, 18 months ago)
  • wp-includes/media.php

     
    179179 * Registers a new image size 
    180180 * 
    181181 * @since 2.9.0 
     182 *  
     183 * In addition to true/false, the $crop parameter takes an array of the format 
     184 * array( x_crop_position, y_crop_position ) 
     185 * x_crop_position can be 'left', 'center', 'right' 
     186 * y_crop_position can be 'top', 'center', 'bottom' 
     187 *  
     188 * @param string $name Image size identifier. 
     189 * @param int $width Image width. 
     190 * @param int $height Image height. 
     191 * @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. 
     192 * @return bool|array False, if no image was created. Metadata array on success. 
    182193 */ 
    183194function add_image_size( $name, $width = 0, $height = 0, $crop = false ) { 
    184195        global $_wp_additional_image_sizes; 
    185         $_wp_additional_image_sizes[$name] = array( 'width' => absint( $width ), 'height' => absint( $height ), 'crop' => (bool) $crop ); 
     196        $_wp_additional_image_sizes[$name] = array( 'width' => absint( $width ), 'height' => absint( $height ), 'crop' => $crop ); 
    186197} 
    187198 
    188199/** 
     
    362373                $crop_w = round($new_w / $size_ratio); 
    363374                $crop_h = round($new_h / $size_ratio); 
    364375 
    365                 $s_x = floor( ($orig_w - $crop_w) / 2 ); 
    366                 $s_y = floor( ($orig_h - $crop_h) / 2 ); 
     376        if ( !is_array( $crop ) || count( $crop ) != 2 ) { 
     377                        $crop = apply_filters( 'image_resize_crop_default', array( 'center', 'center' ) ); 
     378                } 
     379                 
     380                switch ( $crop[0] ) { 
     381                        case 'left': $s_x = 0; break; 
     382                        case 'right': $s_x = $orig_w - $crop_w; break; 
     383                        default: $s_x = floor( ( $orig_w - $crop_w ) / 2 ); 
     384                } 
     385 
     386                switch ( $crop[1] ) { 
     387                        case 'top': $s_y = 0; break; 
     388                        case 'bottom': $s_y = $orig_h - $crop_h; break; 
     389                        default: $s_y = floor( ( $orig_h - $crop_h ) / 2 ); 
     390                } 
    367391        } else { 
    368392                // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box 
    369393                $crop_w = $orig_w; 
  • wp-admin/includes/image.php

     
    114114                        else 
    115115                                $sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options 
    116116                        if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) ) 
    117                                 $sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes 
     117                                $sizes[$s]['crop'] = $_wp_additional_image_sizes[$s]['crop']; // For theme-added sizes 
    118118                        else 
    119119                                $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options 
    120120                }