Skip to content

Commit c1f1fa0

Browse files
Copilotswissspidy
andcommitted
Fix: preserve user-edited images during regeneration of big images
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 6e05d11 commit c1f1fa0

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

features/media-regenerate.feature

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,54 @@ Feature: Regenerate WordPress attachments
16861686
"""
16871687
And the return code should be 1
16881688

1689+
@require-wp-5.3
1690+
Scenario: Regenerate a large image that was edited by the user preserves the edits
1691+
Given download:
1692+
| path | url |
1693+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
1694+
And I run `wp option update uploads_use_yearmonth_folders 0`
1695+
1696+
When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My imported large attachment" --porcelain`
1697+
Then save STDOUT as {ATTACHMENT_ID}
1698+
And the wp-content/uploads/large-image.jpg file should exist
1699+
And the wp-content/uploads/large-image-scaled.jpg file should exist
1700+
1701+
# Simulate a user edit by copying the scaled file as an "edited" version,
1702+
# updating the attached file metadata, and setting backup sizes.
1703+
When I run `wp eval '
1704+
$id = {ATTACHMENT_ID};
1705+
$meta = wp_get_attachment_metadata( $id );
1706+
$old_file = get_attached_file( $id );
1707+
$edited_file = preg_replace( "/(\.[^.]+)$/", "-e0000000000000$1", $old_file );
1708+
copy( $old_file, $edited_file );
1709+
$edited_relative = _wp_relative_upload_path( $edited_file );
1710+
update_post_meta( $id, "_wp_attached_file", $edited_relative );
1711+
$backup = array();
1712+
foreach ( $meta["sizes"] as $size => $size_data ) {
1713+
$backup[ $size . "-orig" ] = $size_data;
1714+
}
1715+
update_post_meta( $id, "_wp_attachment_backup_sizes", $backup );
1716+
'`
1717+
Then the wp-content/uploads/large-image-scaled-e0000000000000.jpg file should exist
1718+
1719+
When I run `wp post meta get {ATTACHMENT_ID} _wp_attached_file`
1720+
Then STDOUT should contain:
1721+
"""
1722+
large-image-scaled-e0000000000000.jpg
1723+
"""
1724+
1725+
When I run `wp media regenerate {ATTACHMENT_ID} --yes`
1726+
Then STDOUT should contain:
1727+
"""
1728+
Regenerated thumbnails for "My imported large attachment"
1729+
"""
1730+
1731+
When I run `wp post meta get {ATTACHMENT_ID} _wp_attached_file`
1732+
Then STDOUT should contain:
1733+
"""
1734+
large-image-scaled-e0000000000000.jpg
1735+
"""
1736+
16891737
Scenario: Only delete missing image sizes
16901738
Given download:
16911739
| path | url |

src/Media_Command.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,10 @@ private function calculate_transformation( $orientation ) {
13511351
* @return string|false Filepath of the attachment, or false if not found.
13521352
*/
13531353
private function get_attached_file( $attachment_id ) {
1354-
if ( function_exists( 'wp_get_original_image_path' ) ) {
1354+
// If the image has been edited by the user, use the edited file (tracked
1355+
// via _wp_attachment_backup_sizes) rather than the original pre-scaled image.
1356+
$backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
1357+
if ( empty( $backup_sizes ) && function_exists( 'wp_get_original_image_path' ) ) {
13551358
$filepath = wp_get_original_image_path( $attachment_id );
13561359

13571360
if ( false !== $filepath ) {

0 commit comments

Comments
 (0)