performance icon indicating copy to clipboard operation
performance copied to clipboard

Fix: PDF thumbnails to WEBP/AVIF

Open AhmarZaidi opened this issue 10 months ago • 2 comments

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

https://github.com/user-attachments/assets/9a4f940f-3860-43e3-9aa2-4e252e10ff34

AhmarZaidi avatar Feb 12 '25 12:02 AhmarZaidi

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

AhmarZaidi avatar Feb 12 '25 12:02 AhmarZaidi

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: 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%) :arrow_up:
single 35.36% <100.00%> (+0.02%) :arrow_up:

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

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

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

codecov[bot] avatar Feb 12 '25 12:02 codecov[bot]

This issue has been resolved, please close it admin

phanduynam avatar Apr 03 '25 13:04 phanduynam

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

westonruter avatar Aug 23 '25 20:08 westonruter

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.

github-actions[bot] avatar Aug 25 '25 11:08 github-actions[bot]

Hey @westonruter

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

AhmarZaidi avatar Aug 25 '25 11:08 AhmarZaidi

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

adamsilverstein avatar Aug 26 '25 20:08 adamsilverstein