WordPress.org

Make WordPress Core

Ticket #46777: image-resize-dimensions-add-attachment-id.patch

File image-resize-dimensions-add-attachment-id.patch, 8.7 KB (added by arkimedia, 5 months ago)

Image Resize Dimensions Attachment ID Patch

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

    diff --git a/src/wp-admin/includes/image-edit.php b/src/wp-admin/includes/image-edit.php
    index e1ae86b607..e9de8b73dd 100644
    a b function wp_save_image( $post_id ) { 
    931931                        );
    932932                }
    933933
    934                 $meta['sizes'] = array_merge( $meta['sizes'], $img->multi_resize( $_sizes ) );
     934                $meta['sizes'] = array_merge( $meta['sizes'], $img->multi_resize( $_sizes, $post_id ) );
    935935        }
    936936
    937937        unset( $img );
  • src/wp-admin/includes/image.php

    diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php
    index d0a2954e5f..60467d1451 100644
    a b function wp_generate_attachment_metadata( $attachment_id, $file ) { 
    142142                        $editor = wp_get_image_editor( $file );
    143143
    144144                        if ( ! is_wp_error( $editor ) ) {
    145                                 $metadata['sizes'] = $editor->multi_resize( $sizes );
     145                                $metadata['sizes'] = $editor->multi_resize( $sizes, $attachment_id );
    146146                        }
    147147                } else {
    148148                        $metadata['sizes'] = array();
    function wp_generate_attachment_metadata( $attachment_id, $file ) { 
    282282                                        unset( $uploaded['path'] );
    283283
    284284                                        if ( ! is_wp_error( $editor ) ) {
    285                                                 $metadata['sizes']         = $editor->multi_resize( $sizes );
     285                                                $metadata['sizes']         = $editor->multi_resize( $sizes, $attachment_id );
    286286                                                $metadata['sizes']['full'] = $uploaded;
    287287                                        }
    288288                                }
  • src/wp-includes/class-wp-image-editor-gd.php

    diff --git a/src/wp-includes/class-wp-image-editor-gd.php b/src/wp-includes/class-wp-image-editor-gd.php
    index 72fb04ee72..c88d5e4c30 100644
    a b class WP_Image_Editor_GD extends WP_Image_Editor { 
    149149         * @param  int|null $max_w Image width.
    150150         * @param  int|null $max_h Image height.
    151151         * @param  bool     $crop
     152         * @param  int          $attachment_id
    152153         * @return true|WP_Error
    153154         */
    154         public function resize( $max_w, $max_h, $crop = false ) {
     155        public function resize( $max_w, $max_h, $crop = false, $attachment_id = 0 ) {
    155156                if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) ) {
    156157                        return true;
    157158                }
    158159
    159                 $resized = $this->_resize( $max_w, $max_h, $crop );
     160                $resized = $this->_resize( $max_w, $max_h, $crop, $attachment_id );
    160161
    161162                if ( is_resource( $resized ) ) {
    162163                        imagedestroy( $this->image );
    class WP_Image_Editor_GD extends WP_Image_Editor { 
    174175         * @param int $max_w
    175176         * @param int $max_h
    176177         * @param bool|array $crop
     178         * @param int $attachment_id
    177179         * @return resource|WP_Error
    178180         */
    179         protected function _resize( $max_w, $max_h, $crop = false ) {
    180                 $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
     181        protected function _resize( $max_w, $max_h, $crop = false, $attachment_id = 0 ) {
     182                $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop, $attachment_id );
    181183                if ( ! $dims ) {
    182184                        return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ), $this->file );
    183185                }
    class WP_Image_Editor_GD extends WP_Image_Editor { 
    212214         *         @type int  $width  Image width. Optional if `$height` is specified.
    213215         *         @type int  $height Image height. Optional if `$width` is specified.
    214216         *         @type bool $crop   Optional. Whether to crop the image. Default false.
     217         *         @type int  $attachment_id. Optional. Attachment id to resize.
    215218         *     }
    216219         * }
    217220         * @return array An array of resized images' metadata by size.
    218221         */
    219         public function multi_resize( $sizes ) {
     222        public function multi_resize( $sizes, $attachment_id = 0 ) {
    220223                $metadata  = array();
    221224                $orig_size = $this->size;
    222225
    class WP_Image_Editor_GD extends WP_Image_Editor { 
    236239                                $size_data['crop'] = false;
    237240                        }
    238241
    239                         $image     = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
     242                        $image     = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'], $attachment_id );
    240243                        $duplicate = ( ( $orig_size['width'] == $size_data['width'] ) && ( $orig_size['height'] == $size_data['height'] ) );
    241244
    242245                        if ( ! is_wp_error( $image ) && ! $duplicate ) {
  • src/wp-includes/class-wp-image-editor-imagick.php

    diff --git a/src/wp-includes/class-wp-image-editor-imagick.php b/src/wp-includes/class-wp-image-editor-imagick.php
    index abb215038b..a088ed86fa 100644
    a b class WP_Image_Editor_Imagick extends WP_Image_Editor { 
    249249         * @param  int|null $max_w Image width.
    250250         * @param  int|null $max_h Image height.
    251251         * @param  bool     $crop
     252         * @param  int          $attachment_id Attachment id to resize.
    252253         * @return bool|WP_Error
    253254         */
    254         public function resize( $max_w, $max_h, $crop = false ) {
     255        public function resize( $max_w, $max_h, $crop = false, $attachment_id = 0 ) {
    255256                if ( ( $this->size['width'] == $max_w ) && ( $this->size['height'] == $max_h ) ) {
    256257                        return true;
    257258                }
    258259
    259                 $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop );
     260                $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop, $attachment_id );
    260261                if ( ! $dims ) {
    261262                        return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ) );
    262263                }
    class WP_Image_Editor_Imagick extends WP_Image_Editor { 
    425426         *         @type int  $width  Image width. Optional if `$height` is specified.
    426427         *         @type int  $height Image height. Optional if `$width` is specified.
    427428         *         @type bool $crop   Optional. Whether to crop the image. Default false.
     429         *         @type int  $attachment_id. Optional. Attachment id to resize.
    428430         *     }
    429431         * }
    430432         * @return array An array of resized images' metadata by size.
    431433         */
    432         public function multi_resize( $sizes ) {
     434        public function multi_resize( $sizes, $attachment_id = 0 ) {
    433435                $metadata   = array();
    434436                $orig_size  = $this->size;
    435437                $orig_image = $this->image->getImage();
    class WP_Image_Editor_Imagick extends WP_Image_Editor { 
    454456                                $size_data['crop'] = false;
    455457                        }
    456458
    457                         $resize_result = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] );
     459                        $resize_result = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'], $attachment_id );
    458460                        $duplicate     = ( ( $orig_size['width'] == $size_data['width'] ) && ( $orig_size['height'] == $size_data['height'] ) );
    459461
    460462                        if ( ! is_wp_error( $resize_result ) && ! $duplicate ) {
  • src/wp-includes/class-wp-image-editor.php

    diff --git a/src/wp-includes/class-wp-image-editor.php b/src/wp-includes/class-wp-image-editor.php
    index 26dd78d230..f6252f615b 100644
    a b abstract class WP_Image_Editor { 
    110110         *         @type int  $width  Image width.
    111111         *         @type int  $height Image height.
    112112         *         @type bool $crop   Optional. Whether to crop the image. Default false.
     113         *         @type int $attachment_id. Optional. Attachment id to resize.
    113114         *     }
    114115         * }
    115116         * @return array An array of resized images metadata by size.
    116117         */
    117         abstract public function multi_resize( $sizes );
     118        abstract public function multi_resize( $sizes, $attachment_id = 0 );
    118119
    119120        /**
    120121         * Crops Image.
  • src/wp-includes/media.php

    diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php
    index bb2e0b8e27..a74e7bd9d3 100644
    a b function wp_constrain_dimensions( $current_width, $current_height, $max_width = 
    491491 * @param int        $dest_h New height in pixels.
    492492 * @param bool|array $crop   Optional. Whether to crop image to specified width and height or resize.
    493493 *                           An array can specify positioning of the crop area. Default false.
     494 * @param int            $attachment_id. Attachment id to resize.
    494495 * @return false|array False on failure. Returned array matches parameters for `imagecopyresampled()`.
    495496 */
    496 function image_resize_dimensions( $orig_w, $orig_h, $dest_w, $dest_h, $crop = false ) {
     497function image_resize_dimensions( $orig_w, $orig_h, $dest_w, $dest_h, $crop = false, $attachment_id = 0 ) {
    497498
    498499        if ( $orig_w <= 0 || $orig_h <= 0 ) {
    499500                return false;
    function image_resize_dimensions( $orig_w, $orig_h, $dest_w, $dest_h, $crop = fa 
    519520         * @param bool|array $crop   Whether to crop image to specified width and height or resize.
    520521         *                           An array can specify positioning of the crop area. Default false.
    521522         */
    522         $output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop );
     523        $output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop, $attachment_id );
    523524        if ( null !== $output ) {
    524525                return $output;
    525526        }