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

Fix: PDF thumbnails to WEBP/AVIF #1868

Draft
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 6 additions & 5 deletions plugins/webp-uploads/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ function webp_uploads_get_upload_image_mime_transforms(): array {
$output_format = webp_uploads_mime_type_supported( 'image/avif' ) ? webp_uploads_get_image_output_format() : 'webp';

$default_transforms = array(
'image/jpeg' => array( 'image/' . $output_format ),
'image/webp' => array( 'image/' . $output_format ),
'image/avif' => array( 'image/avif' ),
'image/png' => array( 'image/' . $output_format ),
'image/jpeg' => array( 'image/' . $output_format ),
'image/webp' => array( 'image/' . $output_format ),
'image/avif' => array( 'image/avif' ),
'image/png' => array( 'image/' . $output_format ),
'application/pdf' => array( 'image/' . $output_format ),
);

// Check setting for whether to generate both JPEG and the modern output format.
Expand Down Expand Up @@ -144,7 +145,7 @@ function webp_uploads_generate_additional_image_source( int $attachment_id, stri
return new WP_Error( 'image_mime_type_not_supported', __( 'The provided mime type is not supported.', 'webp-uploads' ) );
}

$image_path = wp_get_original_image_path( $attachment_id );
$image_path = 'application/pdf' !== get_post_mime_type( $attachment_id ) ? wp_get_original_image_path( $attachment_id ) : get_attached_file( $attachment_id );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor point: This is easier to read and it would facilitate ensuring test coverage is present for both code paths. Also, in the future maybe there will be more such special cases.

Suggested change
$image_path = 'application/pdf' !== get_post_mime_type( $attachment_id ) ? wp_get_original_image_path( $attachment_id ) : get_attached_file( $attachment_id );
if ( 'application/pdf' === get_post_mime_type( $attachment_id ) {
$image_path = get_attached_file( $attachment_id );
} else {
$image_path = wp_get_original_image_path( $attachment_id ); }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking something like this:

$image_path = wp_attachment_is_image( $attachment_id ) ? wp_get_original_image_path( $attachment_id ) : get_attached_file( $attachment_id );

This way we can use wp_get_original_image_path() only when attatchment is an image and in all other cases we can use get_attached_file ()

This is also more in accordance with latest suggestions made on the issue thread: #1766 (comment)

if ( false === $image_path || ! 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.', 'webp-uploads' ) );
}
Expand Down
28 changes: 20 additions & 8 deletions plugins/webp-uploads/tests/test-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,17 +364,19 @@ public function test_it_should_return_default_transforms_when_filter_returns_non
if ( webp_uploads_mime_type_supported( 'image/avif' ) ) {
$this->set_image_output_type( 'avif' );
$default_transforms = array(
'image/jpeg' => array( 'image/avif' ),
'image/webp' => array( 'image/avif' ),
'image/avif' => array( 'image/avif' ),
'image/png' => array( 'image/avif' ),
'image/jpeg' => array( 'image/avif' ),
'image/webp' => array( 'image/avif' ),
'image/avif' => array( 'image/avif' ),
'image/png' => array( 'image/avif' ),
'application/pdf' => array( 'image/avif' ),
);
} else {
$default_transforms = array(
'image/jpeg' => array( 'image/webp' ),
'image/webp' => array( 'image/webp' ),
'image/avif' => array( 'image/avif' ),
'image/png' => array( 'image/webp' ),
'image/jpeg' => array( 'image/webp' ),
'image/webp' => array( 'image/webp' ),
'image/avif' => array( 'image/avif' ),
'image/png' => array( 'image/webp' ),
'application/pdf' => array( 'image/webp' ),
);
}

Expand Down Expand Up @@ -450,6 +452,16 @@ public function test_it_should_return_jpeg_and_webp_transforms_when_option_gener
}
}

/**
* Returns true if 'application/pdf' is included in the MIME transforms array.
*/
public function test_it_should_include_pdf_in_mime_transforms(): void {
$transforms = webp_uploads_get_upload_image_mime_transforms();

$this->assertArrayHasKey( 'application/pdf', $transforms );
$this->assertContains( 'image/webp', $transforms['application/pdf'] );
}

/**
* @dataProvider data_provider_image_filesize
*
Expand Down
Loading