4

I define custom image sizes in my theme like this :

add_action( 'after_setup_theme', 'mwm_custom_image_sizes' );
function mwm_custom_image_sizes() {
    add_theme_support( 'post-thumbnails' );
    add_image_size( 'portrait_medium', 200, 286, array( 'center', 'top' ) );
    etc...
}

When I upload an image, the defaut and custom image sizes are created as expected :

enter image description here

But when I upload a PDF file, only the default image sizes for the cover are generated :

jj

How can I tell Wordpress to generate PDF cover images for all image sizes that are defined ?

1 Answer 1

4

We have the fallback_intermediate_image_sizes filter within wp_generate_attachment_metadata():

 **
 * Filters the image sizes generated for non-image mime types.
 *
 * @since 4.7.0
 *
 * @param string[] $fallback_sizes An array of image size names.
 * @param array    $metadata       Current attachment metadata.
 */
$fallback_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata );

where the default sizes are:

$fallback_sizes = array(
    'thumbnail',
    'medium',
    'large',
);

See core ticket #39231.

3
  • 2
    Thanks, as you suggested adding that filter and passing my custome sizes to the $fallback_sizes array did the trick.
    – mike23
    Commented Apr 27, 2023 at 10:00
  • 1
    Good to hear it worked for you mike23
    – birgire
    Commented Apr 27, 2023 at 11:27
  • Thank you, @birgire @mike23 - I had the same question around adding custom image sizes for PDF thumbnails (i.e. non-image mime types) and I used the fallback_intermediate_image_sizes in this way: add_filter( 'fallback_intermediate_image_sizes', 'add_custom_sizes_for_posters', 10, 2 ); function add_custom_sizes_for_posters( $fallback_sizes, $metadata ) { return array( 'poster-thumb', 'poster-large' ); } With the "poster-" names corresponding to custom image sizes I added by add_image_size
    – nickpish
    Commented Nov 8, 2023 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.