Skip to content
12 changes: 12 additions & 0 deletions src/wp-admin/includes/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
53 changes: 53 additions & 0 deletions tests/phpunit/tests/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -6445,6 +6445,59 @@ public function test_wp_img_tag_add_auto_sizes( string $input, string $expected
);
}

/**
* @ticket 62305
*
* @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_image_converted_to_other_format_has_correct_filename( 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 );

// Verify that the selected editor supports WebP output.
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.webp' );
$this->assertSame( '33772.jpg', $image_meta['original_image'], 'The original image name is expected to be stored in the meta data' );
}

/**
* Data provider for test_image_metadata_get_converted_image_in_file_key().
*
* @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.
*
Expand Down