Make WordPress Core

Changeset 59202


Ignore:
Timestamp:
10/09/2024 11:30:05 PM (23 months ago)
Author:
peterwilsoncc
Message:

Media: Update file size meta data when editing images.

Fixes an error in which the file size meta data retained the original upload's values follow a user editing the images in the media screen.

The original images' file sizes are now stored in the backup image data to allow for them to be restored when a user restores the original image.

Props ankit-k-gupta, antpb, audrasjb, chaion07, gauravsingh7, joedolson, oglekler, pls78, rajinsharwar, sayedulsayem, vertisoft.
Fixes #59684.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/image-edit.php

    r58946 r59202  
    827827                        } elseif ( isset( $meta['width'], $meta['height'] ) ) {
    828828                                $backup_sizes[ "full-$suffix" ] = array(
    829                                         'width'  => $meta['width'],
    830                                         'height' => $meta['height'],
    831                                         'file'   => $parts['basename'],
     829                                        'width'    => $meta['width'],
     830                                        'height'   => $meta['height'],
     831                                        'filesize' => $meta['filesize'],
     832                                        'file'     => $parts['basename'],
    832833                                );
    833834                        }
     
    840841                $meta['width']  = $data['width'];
    841842                $meta['height'] = $data['height'];
     843                if ( isset( $data['filesize'] ) ) {
     844                        /*
     845                         * Restore the original filesize if it was backed up.
     846                         *
     847                         * See https://core.trac.wordpress.org/ticket/59684.
     848                         */
     849                        $meta['filesize'] = $data['filesize'];
     850                }
    842851        }
    843852
     
    9981007        }
    9991008
     1009        $saved_image = wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id );
    10001010        // Save the full-size file, also needed to create sub-sizes.
    1001         if ( ! wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ) ) {
     1011        if ( ! $saved_image ) {
    10021012                $return->error = esc_js( __( 'Unable to save the image.' ) );
    10031013                return $return;
     
    10191029                if ( $tag ) {
    10201030                        $backup_sizes[ $tag ] = array(
    1021                                 'width'  => $meta['width'],
    1022                                 'height' => $meta['height'],
    1023                                 'file'   => $basename,
     1031                                'width'    => $meta['width'],
     1032                                'height'   => $meta['height'],
     1033                                'filesize' => $meta['filesize'],
     1034                                'file'     => $basename,
    10241035                        );
    10251036                }
     
    10291040                $meta['file'] = _wp_relative_upload_path( $new_path );
    10301041
    1031                 $size           = $img->get_size();
    1032                 $meta['width']  = $size['width'];
    1033                 $meta['height'] = $size['height'];
     1042                $size             = $img->get_size();
     1043                $meta['width']    = $size['width'];
     1044                $meta['height']   = $size['height'];
     1045                $meta['filesize'] = $saved_image['filesize'];
    10341046
    10351047                if ( $success && ( 'nothumb' === $target || 'all' === $target ) ) {
  • trunk/tests/phpunit/tests/ajax/wpAjaxImageEditor.php

    r57244 r59202  
    115115                }
    116116        }
     117
     118        /**
     119         * Ensure the filesize is updated after editing an image.
     120         *
     121         * Tests that the image meta data file size is updated after editing an image,
     122         * this includes both the full size image and all the generated sizes.
     123         *
     124         * @ticket 59684
     125         */
     126        public function test_filesize_updated_after_editing_an_image() {
     127                require_once ABSPATH . 'wp-admin/includes/image-edit.php';
     128
     129                $filename = DIR_TESTDATA . '/images/canola.jpg';
     130                $contents = file_get_contents( $filename );
     131
     132                $upload              = wp_upload_bits( wp_basename( $filename ), null, $contents );
     133                $id                  = $this->_make_attachment( $upload );
     134                $original_image_meta = wp_get_attachment_metadata( $id );
     135
     136                $_REQUEST['action']  = 'image-editor';
     137                $_REQUEST['context'] = 'edit-attachment';
     138                $_REQUEST['postid']  = $id;
     139                $_REQUEST['target']  = 'all';
     140                $_REQUEST['do']      = 'save';
     141                $_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]';
     142
     143                wp_save_image( $id );
     144
     145                $post_edit_meta = wp_get_attachment_metadata( $id );
     146
     147                $pre_file_sizes         = array_combine( array_keys( $original_image_meta['sizes'] ), array_column( $original_image_meta['sizes'], 'filesize' ) );
     148                $pre_file_sizes['full'] = $original_image_meta['filesize'];
     149
     150                $post_file_sizes         = array_combine( array_keys( $post_edit_meta['sizes'] ), array_column( $post_edit_meta['sizes'], 'filesize' ) );
     151                $post_file_sizes['full'] = $post_edit_meta['filesize'];
     152
     153                foreach ( $pre_file_sizes as $size => $size_filesize ) {
     154                        // These are asserted individually as each image size needs to be checked separately.
     155                        $this->assertNotSame( $size_filesize, $post_file_sizes[ $size ], "Filesize for $size should have changed after editing an image." );
     156                }
     157        }
     158
     159        /**
     160         * Ensure the filesize is restored after restoring the original image.
     161         *
     162         * Tests that the image meta data file size is restored after restoring the original image,
     163         * this includes both the full size image and all the generated sizes.
     164         *
     165         * @ticket 59684
     166         */
     167        public function test_filesize_restored_after_restoring_original_image() {
     168                require_once ABSPATH . 'wp-admin/includes/image-edit.php';
     169
     170                $filename = DIR_TESTDATA . '/images/canola.jpg';
     171                $contents = file_get_contents( $filename );
     172
     173                $upload              = wp_upload_bits( wp_basename( $filename ), null, $contents );
     174                $id                  = $this->_make_attachment( $upload );
     175                $original_image_meta = wp_get_attachment_metadata( $id );
     176
     177                $_REQUEST['action']  = 'image-editor';
     178                $_REQUEST['context'] = 'edit-attachment';
     179                $_REQUEST['postid']  = $id;
     180                $_REQUEST['target']  = 'all';
     181                $_REQUEST['do']      = 'save';
     182                $_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]';
     183
     184                wp_save_image( $id );
     185                wp_restore_image( $id );
     186
     187                $post_restore_meta = wp_get_attachment_metadata( $id );
     188
     189                $pre_file_sizes         = array_combine( array_keys( $original_image_meta['sizes'] ), array_column( $original_image_meta['sizes'], 'filesize' ) );
     190                $pre_file_sizes['full'] = $original_image_meta['filesize'];
     191
     192                $post_restore_file_sizes         = array_combine( array_keys( $post_restore_meta['sizes'] ), array_column( $post_restore_meta['sizes'], 'filesize' ) );
     193                $post_restore_file_sizes['full'] = $post_restore_meta['filesize'];
     194
     195                $this->assertSameSetsWithIndex( $pre_file_sizes, $post_restore_file_sizes, 'Filesize should have restored after restoring the original image.' );
     196        }
    117197}
Note: See TracChangeset for help on using the changeset viewer.