Media: Remove dimension suffix from full size converted HEIC images.

Removes the dimension suffix, eg `-1000x1000` from the file name of full size images automatically converted from HEIC to JPEGs by WordPress. Introduces unit tests for the default conversion of images and customized conversion settings via the `image_editor_output_format` filter.

Follow up to [58849], [58942], [59317], [59346], [59366]

Props mukesh27, peterwilsoncc, azaozz, apermo, flixos90, ironprogrammer.
Fixes #62359.
See #53645, #62305.




git-svn-id: https://develop.svn.wordpress.org/trunk@59379 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson 2024-11-10 21:40:00 +00:00
parent 8e4b6f7b6d
commit e1f052ec31
3 changed files with 114 additions and 3 deletions

View File

@ -341,6 +341,18 @@ function wp_create_image_subsizes( $file, $attachment_id ) {
*/
if ( $scale_down ) {
$saved = $editor->save( $editor->generate_filename( 'scaled' ) );
} elseif ( $convert ) {
/*
* Generate a new file name for the converted image.
*
* As the image file name will be unique due to the changed file extension,
* it does not need a suffix to be unique. However, the generate_filename method
* does not allow for an empty suffix, so the "-converted" suffix is required to
* be added and subsequently removed.
*/
$converted_file_name = $editor->generate_filename( 'converted' );
$converted_file_name = preg_replace( '/(-converted\.)([a-z0-9]+)$/i', '.$2', $converted_file_name );
$saved = $editor->save( $converted_file_name );
} else {
$saved = $editor->save();
}

View File

@ -1564,10 +1564,10 @@ class Tests_Functions extends WP_UnitTestCase {
}
$expected = array(
50,
50,
1180,
1180,
IMAGETYPE_HEIC,
'width="50" height="50"',
'width="1180" height="1180"',
'mime' => 'image/heic',
);
$result = wp_getimagesize( $file );

View File

@ -6445,6 +6445,105 @@ EOF;
);
}
/**
* Ensure an HEIC image is converted to a JPEG.
*
* @ticket 62305
* @ticket 62359
*
* @dataProvider data_image_converted_to_other_format_has_correct_filename
*
* @param bool $apply_big_image_size_threshold True if filter needs to apply, otherwise false.
*/
public function test_heic_image_upload_is_converted_to_jpeg( bool $apply_big_image_size_threshold ) {
$temp_dir = get_temp_dir();
$file = $temp_dir . '/test-image.heic';
$scaled_suffix = $apply_big_image_size_threshold ? '-scaled' : '';
copy( DIR_TESTDATA . '/images/test-image.heic', $file );
$editor = wp_get_image_editor( $file );
// Skip if the editor does not support HEIC.
if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/heic' ) ) {
$this->markTestSkipped( 'HEIC is not supported by the selected image editor.' );
}
$attachment_id = self::factory()->attachment->create_object(
array(
'post_mime_type' => 'image/heic',
'file' => $file,
)
);
if ( $apply_big_image_size_threshold ) {
add_filter( 'big_image_size_threshold', array( $this, 'add_big_image_size_threshold' ) );
}
$image_meta = wp_generate_attachment_metadata( $attachment_id, $file );
$this->assertStringEndsNotWith( '.heic', $image_meta['file'], 'The file extension is expected to change.' );
$this->assertSame( "test-image{$scaled_suffix}.jpg", basename( $image_meta['file'] ), "The file name is expected to be test-image{$scaled_suffix}.jpg" );
$this->assertSame( 'test-image.heic', $image_meta['original_image'], 'The original image name is expected to be stored in the meta data.' );
$this->assertSame( 'image/jpeg', wp_get_image_mime( $image_meta['file'] ), 'The image mime type is expected to be image/jpeg.' );
}
/**
* Ensure a JPEG is converted to WebP when applied via a filter.
*
* @ticket 62305
* @ticket 62359
*
* @dataProvider data_image_converted_to_other_format_has_correct_filename
*
* @param bool $apply_big_image_size_threshold True if filter needs to apply, otherwise false.
*/
public function test_jpeg_image_converts_to_webp_when_filtered( bool $apply_big_image_size_threshold ) {
$temp_dir = get_temp_dir();
$file = $temp_dir . '/33772.jpg';
$scaled_suffix = $apply_big_image_size_threshold ? '-scaled' : '';
copy( DIR_TESTDATA . '/images/33772.jpg', $file );
$editor = wp_get_image_editor( $file );
// Skip if the editor does not support WebP.
if ( is_wp_error( $editor ) || ! $editor->supports_mime_type( 'image/webp' ) ) {
$this->markTestSkipped( 'WebP is not supported by the selected image editor.' );
}
$attachment_id = self::factory()->attachment->create_object(
array(
'post_mime_type' => 'image/jpeg',
'file' => $file,
)
);
if ( $apply_big_image_size_threshold ) {
add_filter( 'big_image_size_threshold', array( $this, 'add_big_image_size_threshold' ) );
}
// Generate all sizes as WebP.
add_filter( 'image_editor_output_format', array( $this, 'image_editor_output_webp' ) );
$image_meta = wp_generate_attachment_metadata( $attachment_id, $file );
$this->assertStringEndsNotWith( '.jpg', $image_meta['file'], 'The file extension is expected to change.' );
$this->assertSame( "33772{$scaled_suffix}.webp", basename( $image_meta['file'] ), "The file name is expected to be 33772{$scaled_suffix}.webp." );
$this->assertSame( '33772.jpg', $image_meta['original_image'], 'The original image name is expected to be stored in the meta data.' );
$this->assertSame( 'image/webp', wp_get_image_mime( $image_meta['file'] ), 'The image mime type is expected to be image/webp.' );
}
/**
* Data provider for test_image_converted_to_other_format_has_correct_filename().
*
* @return array[]
*/
public function data_image_converted_to_other_format_has_correct_filename() {
return array(
'do not scale image' => array( false ),
'scale image' => array( true ),
);
}
/**
* Helper method to keep track of the last context returned by the 'wp_get_attachment_image_context' filter.
*