Skip to content

Add the original image's extension to the WebP file name to ensure it is unique #444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 8, 2022
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
10 changes: 4 additions & 6 deletions modules/images/webp-uploads/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,11 @@ function webp_uploads_generate_additional_image_source( $attachment_id, $image_s
$editor->resize( $width, $height, $crop );

if ( null === $destination_file_name ) {
$ext = pathinfo( $image_path, PATHINFO_EXTENSION );
$suffix = $editor->get_suffix();
$suffix .= "-{$ext}";
$extension = explode( '|', $allowed_mimes[ $mime ] );
$destination_file_name = $editor->generate_filename( null, null, $extension[0] );
}

// Skip creation of duplicate WebP image if an image file already exists in the directory.
if ( file_exists( $destination_file_name ) ) {
return new WP_Error( 'webp_image_file_present', __( 'The WebP image already exists.', 'performance-lab' ) );
$destination_file_name = $editor->generate_filename( $suffix, null, $extension[0] );
}

$image = $editor->save( $destination_file_name, $mime );
Expand Down
3 changes: 2 additions & 1 deletion modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id )

$original_directory = pathinfo( $file, PATHINFO_DIRNAME );
$filename = pathinfo( $file, PATHINFO_FILENAME );
$ext = pathinfo( $file, PATHINFO_EXTENSION );
$allowed_mimes = array_flip( wp_get_mime_types() );

// Create the sources for the full sized image.
Expand All @@ -89,7 +90,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id )
}

$extension = explode( '|', $allowed_mimes[ $targeted_mime ] );
$destination = trailingslashit( $original_directory ) . "{$filename}.{$extension[0]}";
$destination = trailingslashit( $original_directory ) . "{$filename}-{$ext}.{$extension[0]}";
$image = webp_uploads_generate_additional_image_source( $attachment_id, 'full', $original_size_data, $targeted_mime, $destination );

if ( is_wp_error( $image ) ) {
Expand Down
40 changes: 38 additions & 2 deletions tests/modules/images/webp-uploads/helper-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ public function it_should_create_an_image_with_the_default_suffix_in_the_same_lo
$this->assertIsArray( $result );
$this->assertArrayHasKey( 'filesize', $result );
$this->assertArrayHasKey( 'file', $result );
$this->assertStringEndsWith( '300x300.webp', $result['file'] );
$this->assertFileExists( "{$directory}{$name}-300x300.webp" );
$this->assertStringEndsWith( '300x300-jpeg.webp', $result['file'] );
$this->assertFileExists( "{$directory}{$name}-300x300-jpeg.webp" );
}

/**
Expand Down Expand Up @@ -507,4 +507,40 @@ private function mock_empty_action( $action ) {
remove_all_actions( $action );
do_action( $action );
}

/**
* Add the original image's extension to the WebP file name to ensure it is unique
*
* @dataProvider data_provider_same_image_name
*
* @test
*/
public function it_should_add_original_image_extension_to_the_webp_file_name_to_ensure_it_is_unique( $jpeg_image, $jpg_image ) {
$jpeg_image_attachment_id = $this->factory->attachment->create_upload_object( $jpeg_image );
$jpg_image_attachment_id = $this->factory->attachment->create_upload_object( $jpg_image );

$size_data = array(
'width' => 300,
'height' => 300,
'crop' => true,
);

$jpeg_image_result = webp_uploads_generate_additional_image_source( $jpeg_image_attachment_id, 'medium', $size_data, 'image/webp' );
$jpg_image_result = webp_uploads_generate_additional_image_source( $jpg_image_attachment_id, 'medium', $size_data, 'image/webp' );

$this->assertIsArray( $jpeg_image_result );
$this->assertIsArray( $jpg_image_result );
$this->assertStringEndsWith( '300x300-jpeg.webp', $jpeg_image_result['file'] );
$this->assertStringEndsWith( '300x300-jpg.webp', $jpg_image_result['file'] );
$this->assertNotSame( $jpeg_image_result['file'], $jpg_image_result['file'] );
}

public function data_provider_same_image_name() {
return array(
array(
TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/image.jpeg',
TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/image.jpg',
),
);
}
}
4 changes: 2 additions & 2 deletions tests/modules/images/webp-uploads/load-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ function () {
$this->assertStringEndsWith( '-scaled.jpg', get_attached_file( $attachment_id ) );
$this->assertImageHasSizeSource( $attachment_id, 'medium', 'image/webp' );
$this->assertStringEndsNotWith( '-scaled.webp', $metadata['sizes']['medium']['sources']['image/webp']['file'] );
$this->assertStringEndsWith( '-300x200.webp', $metadata['sizes']['medium']['sources']['image/webp']['file'] );
$this->assertStringEndsWith( '-300x200-jpg.webp', $metadata['sizes']['medium']['sources']['image/webp']['file'] );
}

/**
Expand Down Expand Up @@ -539,7 +539,7 @@ function () {
$this->assertStringContainsString( $size['height'], $size['sources']['image/webp']['file'] );
$this->assertStringContainsString(
// Remove the extension from the file.
substr( $size['sources']['image/webp']['file'], 0, -4 ),
substr( $size['sources']['image/webp']['file'], 0, -10 ),
$size['sources']['image/jpeg']['file']
);
}
Expand Down
Binary file added tests/testdata/modules/images/image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/testdata/modules/images/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.