From f922c64efa27fdd42ace31f099c3d715a1cc5abc Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Tue, 15 Nov 2016 13:25:46 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@39246 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/image.php | 42 +++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php index 87b819180b..5f7b583d5d 100644 --- a/src/wp-admin/includes/image.php +++ b/src/wp-admin/includes/image.php @@ -205,38 +205,50 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) { } // Try to create image thumbnails for PDFs else if ( 'application/pdf' === $mime_type ) { - $editor = wp_get_image_editor( $file ); - $fallback_sizes = array( 'thumbnail', 'medium', 'large', ); + /** + * Filters the image sizes generated for non-image mime types. + * + * @since 4.7.0 + * + * @param array $fallback_sizes An array of image size names. + */ + $fallback_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata ); + $sizes = array(); foreach ( $fallback_sizes as $s ) { - $sizes[$s]['width'] = get_option( "{$s}_size_w" ); - $sizes[$s]['height'] = get_option( "{$s}_size_h" ); + $sizes[ $s ]['width'] = get_option( "{$s}_size_w" ); + $sizes[ $s ]['height'] = get_option( "{$s}_size_h" ); // Force thumbnails to be soft crops. if ( ! 'thumbnail' === $s ) { - $sizes[$s]['crop'] = get_option( "{$s}_crop" ); + $sizes[ $s ]['crop'] = get_option( "{$s}_crop" ); } } - if ( ! is_wp_error( $editor ) ) { // No support for this type of file - $uploaded = $editor->save( $file, 'image/jpeg' ); - unset( $editor ); + // Only load PDFs in an image editor if we're processing sizes. + if ( ! empty( $sizes ) ) { + $editor = wp_get_image_editor( $file ); - // Resize based on the full size image, rather than the source. - if ( ! is_wp_error( $uploaded ) ) { - $editor = wp_get_image_editor( $uploaded['path'] ); - unset( $uploaded['path'] ); + if ( ! is_wp_error( $editor ) ) { // No support for this type of file + $uploaded = $editor->save( $file, 'image/jpeg' ); + unset( $editor ); - if ( ! is_wp_error( $editor ) ) { - $metadata['sizes'] = $editor->multi_resize( $sizes ); - $metadata['sizes']['full'] = $uploaded; + // Resize based on the full size image, rather than the source. + if ( ! is_wp_error( $uploaded ) ) { + $editor = wp_get_image_editor( $uploaded['path'] ); + unset( $uploaded['path'] ); + + if ( ! is_wp_error( $editor ) ) { + $metadata['sizes'] = $editor->multi_resize( $sizes ); + $metadata['sizes']['full'] = $uploaded; + } } } }