WordPress.org

Make WordPress Core

Opened 12 months ago

Closed 11 months ago

Last modified 10 months ago

#38594 closed enhancement (fixed)

pdf preview sizes not filterable

Reported by: nosilver4u Owned by: joemcgill
Milestone: 4.7 Priority: normal
Severity: normal Version: 4.7
Component: Media Keywords: has-patch 2nd-opinion
Focuses: Cc:

Description

When checking the new PDF previews in 4.7, and trying to ensure that EWWW Image Optimizer is compatible with the changes, I discovered that there does not seem to be a way to modify the sizes that are generated.

With image uploads, the intermediate_image_sizes_advanced filter is available to modify the sizes being generated (or take other action, and EWWW does both).

From what I can see, the thumb generation section for PDF files in wp-admin/includes/image.php has absolutely no actions/filters for developers to modify the default behavior. I don't know if adding the intermediate_image_sizes_advanced filter is the correct way to do it, or if a new filter is in order here, but either would be greatly appreciated!

Attachments (2)

38594.diff (1.0 KB) - added by joemcgill 12 months ago.
38594.2.diff (2.1 KB) - added by joemcgill 11 months ago.

Download all attachments as: .zip

Change History (18)

#1 @joemcgill
12 months ago

  • Keywords reporter-feedback needs-patch added
  • Owner set to joemcgill
  • Status changed from new to reviewing

Thanks @nosilver4u. As you mentioned, I don't think we would want to use the same intermediate_image_sizes_advanced filter here since the intent (at least for now) is to produce a limited number of intermediate sizes for the purpose of showing image representations of the PDFs in the media library, but I could see wanting a filter here.

Could you give more detail about what you mean by "or take other action"? I'm curious what other types of actions you would be wanting do take at that point in the metadata generation process, and if you could hook into wp_generate_attachment_metadata as an alternative.

This ticket was mentioned in Slack in #core-media by joemcgill. View the logs.


12 months ago

#3 @nosilver4u
12 months ago

I do want to emphasize that EWWW does make use of the ability to filter the resizes generated. EWWW Image Optimizer gives users the ability to disable the creation of certain resizes, and it would be nice to extend this to the realm of pdf previews as well.

The other thing that several IO plugins have is the ability to disable optimization of specific resizes, and this is where we get into the 'take other action' part of the deal:
EWWW IO extends the WP_Image_Editor class to make sure all images saved by WP_Image_Editor are optimized. To ensure that the specified resizes do not get optimized accidentally by the WP_Image_Editor extension, EWWW uses the intermediate_image_sizes_advanced filter to remove the wp_image_editors filter, and then restores the wp_image_editors filter during the wp_generate_attachment_metadata filter.

I wanted to do it as late as possible in the processing, so that any images that other plugins might generate along the way still get optimized (both before AND after the creation of resizes). So they are basically just pass-through filters toggling EWWW's WP_Image_Editor class. The only example I can think of off-hand is retina images, but there are probably others too.

@joemcgill
12 months ago

#4 @joemcgill
12 months ago

  • Keywords has-patch 2nd-opinion added; needs-patch removed

38594.diff demonstrates how we might apply a separate filter, fallback_intermediate_image_sizes (name is debatable) for modifying which image sizes are generated for PDFs (or other future potential files). Also note that we would want to check whether any sizes exist before running the multi-resize process so someone could filter the sizes to an empty array or false to disable PDF thumbnails altogether.

This ticket was mentioned in Slack in #core-media by joemcgill. View the logs.


12 months ago

#6 @nosilver4u
12 months ago

The 'fallback' term is a bit odd, since there isn't anything it is 'falling back' from, right? Perhaps something that indicates these are 'preview' image sizes?

#7 @mikeschroder
12 months ago

+1 for this approach.

This ticket was mentioned in Slack in #core-media by mike. View the logs.


12 months ago

#9 @joemcgill
12 months ago

  • Keywords reporter-feedback 2nd-opinion removed
  • Milestone changed from Awaiting Review to 4.7
  • Status changed from reviewing to accepted

Let's do something like 38594.diff for 4.7. I picked the name fallback_intermediate_image_sizes since these thumbnails are fallbacks for media types that aren't images, but am open to better suggestions.

This ticket was mentioned in Slack in #core-media by joemcgill. View the logs.


12 months ago

#11 @helen
11 months ago

  • Keywords commit added

@joemcgill
11 months ago

#12 @joemcgill
11 months ago

  • Keywords 2nd-opinion added; commit removed

38594.2.diff modifies the fallback logic so the fallback_intermediate_image_sizes filter could be used to short circuit loading PDFs into an image editor if no fallback sizes have been defined.

However, a downside to this approach is potentially limiting folks who want to generate the full size but not any intermediate sizes as @mikeschroder mentioned in our discussion on Slack. I'm not convinced we need to add an extra filter to support that use case, since someone could create approximation of a full size if they needed to using this same filter.

This ticket was mentioned in Slack in #core-media by joemcgill. View the logs.


11 months ago

#14 @joemcgill
11 months ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 39246:

Media: Make PDF preview sizes filterable.

This adds a new filter, fallback_intermediate_image_sizes, which
can be used to modify the array of image sizes created for previewing
PDFs in the media library and checks for the existence of sizes before
processing any image representations of a PDF.

Fixes #38594.

#15 @joemcgill
10 months ago

In 39617:

Media: Allow PDF fallbacks filter to process custom sizes.

This fixes an oversight in [39246], which added a hook for filtering
the array of sizes used for PDF thumbnails, but failed to provide a way
for sizes added through add_image_size() to be processed.

Props gitlost.
Fixes #39231. See #38594.

#16 @pento
10 months ago

In 39633:

Media: Allow PDF fallbacks filter to process custom sizes.

This fixes an oversight in [39246], which added a hook for filtering the array of sizes used for PDF thumbnails, but failed to provide a way for sizes added through add_image_size() to be processed.

Merge of [39617] to the 4.7 branch.

Props gitlost.
Fixes #39231. See #38594.

Note: See TracTickets for help on using tickets.