Skip to content

Commit

Permalink
Media: Update file size meta data when editing images.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
peterwilsoncc committed Oct 9, 2024
1 parent 4ccb5b4 commit 6a07d7a
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/wp-admin/includes/image-edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -826,9 +826,10 @@ function wp_restore_image( $post_id ) {
}
} elseif ( isset( $meta['width'], $meta['height'] ) ) {
$backup_sizes[ "full-$suffix" ] = array(
'width' => $meta['width'],
'height' => $meta['height'],
'file' => $parts['basename'],
'width' => $meta['width'],
'height' => $meta['height'],
'filesize' => $meta['filesize'],
'file' => $parts['basename'],
);
}
}
Expand All @@ -839,6 +840,14 @@ function wp_restore_image( $post_id ) {
$meta['file'] = _wp_relative_upload_path( $restored_file );
$meta['width'] = $data['width'];
$meta['height'] = $data['height'];
if ( isset( $data['filesize'] ) ) {
/*
* Restore the original filesize if it was backed up.
*
* See https://core.trac.wordpress.org/ticket/59684.
*/
$meta['filesize'] = $data['filesize'];
}
}

foreach ( $default_sizes as $default_size ) {
Expand Down Expand Up @@ -997,8 +1006,9 @@ function wp_save_image( $post_id ) {
}
}

$saved_image = wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id );
// Save the full-size file, also needed to create sub-sizes.
if ( ! wp_save_image_file( $new_path, $img, $post->post_mime_type, $post_id ) ) {
if ( ! $saved_image ) {
$return->error = esc_js( __( 'Unable to save the image.' ) );
return $return;
}
Expand All @@ -1018,19 +1028,21 @@ function wp_save_image( $post_id ) {

if ( $tag ) {
$backup_sizes[ $tag ] = array(
'width' => $meta['width'],
'height' => $meta['height'],
'file' => $basename,
'width' => $meta['width'],
'height' => $meta['height'],
'filesize' => $meta['filesize'],
'file' => $basename,
);
}

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

$meta['file'] = _wp_relative_upload_path( $new_path );

$size = $img->get_size();
$meta['width'] = $size['width'];
$meta['height'] = $size['height'];
$size = $img->get_size();
$meta['width'] = $size['width'];
$meta['height'] = $size['height'];
$meta['filesize'] = $saved_image['filesize'];

if ( $success && ( 'nothumb' === $target || 'all' === $target ) ) {
$sizes = get_intermediate_image_sizes();
Expand Down
80 changes: 80 additions & 0 deletions tests/phpunit/tests/ajax/wpAjaxImageEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,84 @@ public function testImageEditOverwriteConstant() {
$this->assertSame( array(), $files_that_should_not_exist );
}
}

/**
* Ensure the filesize is updated after editing an image.
*
* Tests that the image meta data file size is updated after editing an image,
* this includes both the full size image and all the generated sizes.
*
* @ticket 59684
*/
public function test_filesize_updated_after_editing_an_image() {
require_once ABSPATH . 'wp-admin/includes/image-edit.php';

$filename = DIR_TESTDATA . '/images/canola.jpg';
$contents = file_get_contents( $filename );

$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$id = $this->_make_attachment( $upload );
$original_image_meta = wp_get_attachment_metadata( $id );

$_REQUEST['action'] = 'image-editor';
$_REQUEST['context'] = 'edit-attachment';
$_REQUEST['postid'] = $id;
$_REQUEST['target'] = 'all';
$_REQUEST['do'] = 'save';
$_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]';

wp_save_image( $id );

$post_edit_meta = wp_get_attachment_metadata( $id );

$pre_file_sizes = array_combine( array_keys( $original_image_meta['sizes'] ), array_column( $original_image_meta['sizes'], 'filesize' ) );
$pre_file_sizes['full'] = $original_image_meta['filesize'];

$post_file_sizes = array_combine( array_keys( $post_edit_meta['sizes'] ), array_column( $post_edit_meta['sizes'], 'filesize' ) );
$post_file_sizes['full'] = $post_edit_meta['filesize'];

foreach ( $pre_file_sizes as $size => $size_filesize ) {
// These are asserted individually as each image size needs to be checked separately.
$this->assertNotSame( $size_filesize, $post_file_sizes[ $size ], "Filesize for $size should have changed after editing an image." );
}
}

/**
* Ensure the filesize is restored after restoring the original image.
*
* Tests that the image meta data file size is restored after restoring the original image,
* this includes both the full size image and all the generated sizes.
*
* @ticket 59684
*/
public function test_filesize_restored_after_restoring_original_image() {
require_once ABSPATH . 'wp-admin/includes/image-edit.php';

$filename = DIR_TESTDATA . '/images/canola.jpg';
$contents = file_get_contents( $filename );

$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$id = $this->_make_attachment( $upload );
$original_image_meta = wp_get_attachment_metadata( $id );

$_REQUEST['action'] = 'image-editor';
$_REQUEST['context'] = 'edit-attachment';
$_REQUEST['postid'] = $id;
$_REQUEST['target'] = 'all';
$_REQUEST['do'] = 'save';
$_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]';

wp_save_image( $id );
wp_restore_image( $id );

$post_restore_meta = wp_get_attachment_metadata( $id );

$pre_file_sizes = array_combine( array_keys( $original_image_meta['sizes'] ), array_column( $original_image_meta['sizes'], 'filesize' ) );
$pre_file_sizes['full'] = $original_image_meta['filesize'];

$post_restore_file_sizes = array_combine( array_keys( $post_restore_meta['sizes'] ), array_column( $post_restore_meta['sizes'], 'filesize' ) );
$post_restore_file_sizes['full'] = $post_restore_meta['filesize'];

$this->assertSameSetsWithIndex( $pre_file_sizes, $post_restore_file_sizes, 'Filesize should have restored after restoring the original image.' );
}
}

0 comments on commit 6a07d7a

Please sign in to comment.