Skip to content

Commit

Permalink
Merge pull request #207 from WordPress/feature/204-original-images-fo…
Browse files Browse the repository at this point in the history
…r-webp

Use `original` image to generate all subsizes
  • Loading branch information
felixarntz committed Mar 14, 2022
2 parents 97573d8 + 2904c34 commit b082f7d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
22 changes: 14 additions & 8 deletions modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,16 @@ function webp_uploads_generate_image_size( $attachment_id, $size, $mime ) {
'crop' => false,
);

if ( isset( $metadata['sizes'][ $size ]['width'] ) ) {
$size_data['width'] = $metadata['sizes'][ $size ]['width'];
} elseif ( isset( $sizes[ $size ]['width'] ) ) {
if ( isset( $sizes[ $size ]['width'] ) ) {
$size_data['width'] = $sizes[ $size ]['width'];
} elseif ( isset( $metadata['sizes'][ $size ]['width'] ) ) {
$size_data['width'] = $metadata['sizes'][ $size ]['width'];
}

if ( isset( $metadata['sizes'][ $size ]['height'] ) ) {
$size_data['height'] = $metadata['sizes'][ $size ]['height'];
} elseif ( isset( $sizes[ $size ]['height'] ) ) {
if ( isset( $sizes[ $size ]['height'] ) ) {
$size_data['height'] = $sizes[ $size ]['height'];
} elseif ( isset( $metadata['sizes'][ $size ]['height'] ) ) {
$size_data['height'] = $metadata['sizes'][ $size ]['height'];
}

if ( isset( $sizes[ $size ]['crop'] ) ) {
Expand Down Expand Up @@ -258,11 +258,11 @@ function webp_uploads_get_supported_image_mime_transforms() {
* @return array|WP_Error An array with the file and filesize if the image was created correctly otherwise a WP_Error
*/
function webp_uploads_generate_additional_image_source( $attachment_id, array $size_data, $mime, $destination_file_name = null ) {
$image_path = get_attached_file( $attachment_id );
$image_path = wp_get_original_image_path( $attachment_id );

// File does not exist.
if ( ! file_exists( $image_path ) ) {
return new WP_Error( 'image_file_size_not_found', __( 'The provided size does not have a valid image file.', 'performance-lab' ) );
return new WP_Error( 'original_image_file_not_found', __( 'The original image file does not exists, subsizes are created out of the original image.', 'performance-lab' ) );
}

$editor = wp_get_image_editor( $image_path );
Expand All @@ -288,6 +288,12 @@ function webp_uploads_generate_additional_image_source( $attachment_id, array $s
return new WP_Error( 'image_wrong_dimensions', __( 'At least one of the dimensions must be a positive number.', 'performance-lab' ) );
}

$image_meta = wp_get_attachment_metadata( $attachment_id );
// If stored EXIF data exists, rotate the source image before creating sub-sizes.
if ( ! empty( $image_meta['image_meta'] ) ) {
$editor->maybe_exif_rotate();
}

$editor->resize( $width, $height, $crop );

if ( null === $destination_file_name ) {
Expand Down
31 changes: 30 additions & 1 deletion tests/modules/images/webp-uploads/webp-uploads-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function it_should_prevent_to_create_an_image_size_when_attached_file_doe

$result = webp_uploads_generate_image_size( $attachment_id, 'medium', 'image/webp' );
$this->assertTrue( is_wp_error( $result ) );
$this->assertSame( 'image_file_size_not_found', $result->get_error_code() );
$this->assertSame( 'original_image_file_not_found', $result->get_error_code() );
}

/**
Expand Down Expand Up @@ -722,4 +722,33 @@ public function it_should_create_a_file_in_the_specified_location_with_the_speci
$this->assertStringEndsWith( 'image.webp', $result['file'] );
$this->assertFileExists( '/tmp/image.webp' );
}

/**
* Use the original image to generate all the image sizes
*
* @test
*/
public function it_should_use_the_original_image_to_generate_all_the_image_sizes() {
// Use a 1500 threshold.
add_filter(
'big_image_size_threshold',
function () {
return 1500;
}
);

$attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/paint.jpeg' );
$metadata = wp_get_attachment_metadata( $attachment_id );

$this->assertArrayHasKey( '1536x1536', $metadata['sizes'] );
foreach ( $metadata['sizes'] as $size ) {
$this->assertStringContainsString( $size['width'], $size['sources']['image/webp']['file'] );
$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 ),
$size['sources']['image/jpeg']['file']
);
}
}
}
Binary file added tests/testdata/modules/images/paint.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b082f7d

Please sign in to comment.