Skip to content

Commit 6a07d7a

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@59202 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4ccb5b4 commit 6a07d7a

File tree

2 files changed

+102
-10
lines changed

2 files changed

+102
-10
lines changed

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -826,9 +826,10 @@ function wp_restore_image( $post_id ) {
826826
}
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
}
834835
}
@@ -839,6 +840,14 @@ function wp_restore_image( $post_id ) {
839840
$meta['file'] = _wp_relative_upload_path( $restored_file );
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

844853
foreach ( $default_sizes as $default_size ) {
@@ -997,8 +1006,9 @@ function wp_save_image( $post_id ) {
9971006
}
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;
10041014
}
@@ -1018,19 +1028,21 @@ function wp_save_image( $post_id ) {
10181028

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
}
10261037

10271038
$success = ( $path === $new_path ) || update_attached_file( $post_id, $new_path );
10281039

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 ) ) {
10361048
$sizes = get_intermediate_image_sizes();

tests/phpunit/tests/ajax/wpAjaxImageEditor.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,84 @@ public function testImageEditOverwriteConstant() {
114114
$this->assertSame( array(), $files_that_should_not_exist );
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
}

0 commit comments

Comments
 (0)