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

Conversation

AhmarZaidi
Copy link
Contributor

Summary

This PR adds support for generating Modern Format (webp/aif) image thumbnails from PDF files.

Fixes #1766

Relevant technical choices

  • Added application/pdf mimetype to the default transforms list and use get_attached_file() function for getting the original pdf file path if mime type is application/pdf to bypass $image_path = wp_get_original_image_path( $attachment_id );
  • Add unit tests for web uploads helper functions to check if 'application/pdf' is included in the MIME transforms array.

Screen records

Screen.Recording.2025-02-12.at.8.48.56.AM.mov

- Add application/pdf as default transforms
- Fix image_path issue to handle files other than images
- Add unit test to check pdf is included in transforms
@AhmarZaidi
Copy link
Contributor Author

AhmarZaidi commented Feb 12, 2025

As mentioned in the issue comment, the solution doesn't seem work when we test it on the dev environment run using npm run wp-env start and shows security policy error:

WP_Error Object
(
    [errors] => Array
    (
        [invalid_image] => Array
        (
            [0] => attempt to perform an operation not allowed by the security policy `PDF' @ error/constitute.c/IsCoderAuthorized/426
        )
    )

    [error_data] => Array
    (
        [invalid_image] => /var/www/html/wp-content/uploads/2025/02/test.pdf
    )

    [additional_data:protected] => Array ()
)

This appears to be an issue with ImageMagick policy used in the docker WordPress environment. Solutions suggest changing the ImageMagick policy

Image

Copy link

codecov bot commented Feb 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 66.72%. Comparing base (42bd7a8) to head (7b3facd).

Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #1868      +/-   ##
==========================================
+ Coverage   66.70%   66.72%   +0.01%     
==========================================
  Files          88       88              
  Lines        7029     7032       +3     
==========================================
+ Hits         4689     4692       +3     
  Misses       2340     2340              
Flag Coverage Δ
multisite 66.72% <100.00%> (+0.01%) ⬆️
single 37.22% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@westonruter westonruter added [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) [Type] Enhancement A suggestion for improvement of an existing feature labels Feb 12, 2025
@@ -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)

@westonruter westonruter added this to the webp-uploads n.e.x.t milestone Feb 13, 2025
@@ -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 = wp_attachment_is_image( $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.

Let's still avoid the ternary for readability and to ensure we have code coverage for both conditions:

Suggested change
$image_path = wp_attachment_is_image( $attachment_id ) ? wp_get_original_image_path( $attachment_id ) : get_attached_file( $attachment_id );
if ( wp_attachment_is_image( $attachment_id ) ) {
$image_path = wp_get_original_image_path( $attachment_id );
} else {
$image_path = get_attached_file( $attachment_id );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) [Type] Enhancement A suggestion for improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PDF thumbnails to WEBP/AVIF
2 participants