Skip to content

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 68.65%. Comparing base (6274111) to head (2306f64).

Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #1868      +/-   ##
==========================================
+ Coverage   68.64%   68.65%   +0.01%     
==========================================
  Files          90       90              
  Lines        7969     7972       +3     
==========================================
+ Hits         5470     5473       +3     
  Misses       2499     2499              
Flag Coverage Δ
multisite 68.65% <100.00%> (+0.01%) ⬆️
single 35.36% <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.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@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
}

$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
@phanduynam

This comment was marked as spam.

@westonruter
Copy link
Member

@AhmarZaidi Hey! Are you planning to pick this up?

@westonruter westonruter removed this from the webp-uploads n.e.x.t milestone Aug 23, 2025
@AhmarZaidi AhmarZaidi marked this pull request as ready for review August 25, 2025 11:18
Copy link

github-actions bot commented Aug 25, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: AhmarZaidi <[email protected]>
Co-authored-by: westonruter <[email protected]>
Co-authored-by: phanduynam <[email protected]>
Co-authored-by: adamsilverstein <[email protected]>
Co-authored-by: 1ucay <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@AhmarZaidi
Copy link
Contributor Author

AhmarZaidi commented Aug 25, 2025

Hey @westonruter

I've updates the branch and marked it as ready for review, let me know if any changes are required.

@adamsilverstein
Copy link
Member

@AhmarZaidi thanks for this PR, great idea! I'm curious if you have tested on a system that uses GD instead of Imagick. Since GD doesn't support extracting PDF thumbnails, I want to make sure adding the mapping doesn't have any unintended consequences.

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
4 participants