Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions features/media-regenerate.feature
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,43 @@ Feature: Regenerate WordPress attachments
Success: Regenerated 1 of 1 images.
"""

@require-wp-5.3
Scenario: Regenerating a specific image size should not regenerate the scaled version of big images
Given download:
| path | url |
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
And I run `wp option update uploads_use_yearmonth_folders 0`

When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My imported attachment" --porcelain`
Then save STDOUT as {LARGE_ATTACHMENT_ID}
And the wp-content/uploads/large-image-scaled.jpg file should exist
And the wp-content/uploads/large-image-300x225.jpg file should exist

# Save a checksum of the scaled image before any regeneration.
When I run `md5sum wp-content/uploads/large-image-scaled.jpg`
Then save STDOUT as {SCALED_CHECKSUM}

# Add a filter that would produce drastically different results if the scaled image were regenerated.
Given a wp-content/mu-plugins/media-settings.php file:
"""
<?php
add_filter( 'jpeg_quality', function() { return 1; } );
"""

# Regenerate only the medium size - scaled image should not be touched.
When I run `wp media regenerate {LARGE_ATTACHMENT_ID} --image_size=medium --yes`
Then STDOUT should contain:
"""
Regenerated "medium" thumbnail for "My imported attachment"
"""

# Verify the scaled image was NOT regenerated (checksum should be unchanged).
When I run `md5sum wp-content/uploads/large-image-scaled.jpg`
Then STDOUT should be:
"""
{SCALED_CHECKSUM}
"""

@require-wp-4.7.3 @require-extension-imagick
Scenario: Regenerate a specific image size for a PDF attachment
Given download:
Expand Down
15 changes: 13 additions & 2 deletions src/Media_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,10 +665,21 @@ private function process_regeneration( $id, $skip_delete, $only_missing, $delete
++$successes;
return;
}

$site_icon_filter = $this->add_site_icon_filter( $id );

$metadata = wp_generate_attachment_metadata( $id, $fullsizepath );
// When regenerating a specific image size, use the file that WordPress normally
// serves (the scaled version for big images), not the original pre-scaled file.
// This prevents wp_generate_attachment_metadata() from re-creating the scaled
// version or auto-rotating the original during specific-size regeneration.
$generate_file = $fullsizepath;
if ( $image_size && ! $is_pdf ) {
$wp_attached_file = \get_attached_file( $id );
if ( $wp_attached_file && file_exists( $wp_attached_file ) ) {
$generate_file = $wp_attached_file;
}
}

$metadata = wp_generate_attachment_metadata( $id, $generate_file );

if ( $site_icon_filter ) {
remove_filter( 'intermediate_image_sizes_advanced', $site_icon_filter );
Expand Down