Skip to content
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

Update webp_uploads_pre_generate_additional_image_source filter to allow returning file size #334

Merged
merged 6 commits into from Jun 1, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 9 additions & 11 deletions modules/images/webp-uploads/helper.php
Expand Up @@ -67,7 +67,6 @@ function webp_uploads_get_upload_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, $image_size, array $size_data, $mime, $destination_file_name = null ) {

/**
* Filter to allow the generation of additional image sources, in which a defined mime type
* can be transformed and create additional mime types for the file.
Expand All @@ -84,19 +83,23 @@ function webp_uploads_generate_additional_image_source( $attachment_id, $image_s
* @return array|null|WP_Error An array with the file and filesize if the image was created correctly otherwise a WP_Error
*/
$image = apply_filters( 'webp_uploads_pre_generate_additional_image_source', null, $attachment_id, $image_size, $size_data, $mime );

if ( is_wp_error( $image ) ) {
return $image;
}

if (
is_array( $image )
&& ! empty( $image['file'] )
&& ! empty( $image['path'] )
is_array( $image ) &&
! empty( $image['file'] ) &&
(
! empty( $image['path'] ) ||
array_key_exists( 'filesize', $image )
)
) {
return array(
'file' => $image['file'],
'filesize' => filesize( $image['path'] ),
'filesize' => array_key_exists( 'filesize', $image )
? $image['filesize']
: filesize( $image['path'] ),
eugene-manuilov marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand All @@ -110,22 +113,18 @@ function webp_uploads_generate_additional_image_source( $attachment_id, $image_s
}

$image_path = wp_get_original_image_path( $attachment_id );

// File does not exist.
if ( ! file_exists( $image_path ) ) {
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, array( 'mime_type' => $mime ) );

if ( is_wp_error( $editor ) ) {
return $editor;
}

$height = isset( $size_data['height'] ) ? (int) $size_data['height'] : 0;
$width = isset( $size_data['width'] ) ? (int) $size_data['width'] : 0;
$crop = isset( $size_data['crop'] ) && $size_data['crop'];

if ( $width <= 0 && $height <= 0 ) {
return new WP_Error( 'image_wrong_dimensions', __( 'At least one of the dimensions must be a positive number.', 'performance-lab' ) );
}
Expand All @@ -144,7 +143,6 @@ function webp_uploads_generate_additional_image_source( $attachment_id, $image_s
}

$image = $editor->save( $destination_file_name, $mime );

if ( is_wp_error( $image ) ) {
return $image;
}
Expand Down
37 changes: 36 additions & 1 deletion tests/modules/images/webp-uploads/helper-tests.php
Expand Up @@ -270,7 +270,41 @@ function () {
$this->assertArrayHasKey( 'filesize', $result );
$this->assertArrayHasKey( 'file', $result );
$this->assertStringEndsWith( 'image.webp', $result['file'] );
$this->assertFileExists( '/tmp/image.webp' );
}

/**
* Tests the webp_uploads_pre_generate_additional_image_source filter returning filesize property.
*
* @test
*/
public function it_should_use_filesize_when_filter_webp_uploads_pre_generate_additional_image_source_returns_filesize() {
remove_all_filters( 'webp_uploads_pre_generate_additional_image_source' );

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

add_filter(
'webp_uploads_pre_generate_additional_image_source',
function () {
return array(
'file' => 'image.webp',
'filesize' => 777,
);
}
);

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

$result = webp_uploads_generate_additional_image_source( $attachment_id, 'medium', $size_data, 'image/webp', '/tmp/image.jpg' );

$this->assertIsArray( $result );
$this->assertArrayHasKey( 'filesize', $result );
$this->assertEquals( 777, $result['filesize'] );
$this->assertArrayHasKey( 'file', $result );
$this->assertStringEndsWith( 'image.webp', $result['file'] );
}

/**
Expand Down Expand Up @@ -300,4 +334,5 @@ function () {
$this->assertWPError( $result );
$this->assertSame( 'image_additional_generated_error', $result->get_error_code() );
}

}