WordPress.org

Make WordPress Core

Changeset 39246


Ignore:
Timestamp:
11/15/2016 01:25:46 PM (19 months ago)
Author:
joemcgill
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/image.php

    r38949 r39246  
    206206    // Try to create image thumbnails for PDFs
    207207    else if ( 'application/pdf' === $mime_type ) {
    208         $editor = wp_get_image_editor( $file );
    209 
    210208        $fallback_sizes = array(
    211209            'thumbnail',
     
    214212        );
    215213
     214        /**
     215         * Filters the image sizes generated for non-image mime types.
     216         *
     217         * @since 4.7.0
     218         *
     219         * @param array $fallback_sizes An array of image size names.
     220         */
     221        $fallback_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata );
     222
    216223        $sizes = array();
    217224
    218225        foreach ( $fallback_sizes as $s ) {
    219             $sizes[$s]['width']  = get_option( "{$s}_size_w" );
    220             $sizes[$s]['height'] = get_option( "{$s}_size_h" );
     226            $sizes[ $s ]['width']  = get_option( "{$s}_size_w" );
     227            $sizes[ $s ]['height'] = get_option( "{$s}_size_h" );
    221228
    222229            // Force thumbnails to be soft crops.
    223230            if ( ! 'thumbnail' === $s ) {
    224                 $sizes[$s]['crop'] = get_option( "{$s}_crop" );
    225             }
    226         }
    227 
    228         if ( ! is_wp_error( $editor ) ) { // No support for this type of file
    229             $uploaded = $editor->save( $file, 'image/jpeg' );
    230             unset( $editor );
    231 
    232             // Resize based on the full size image, rather than the source.
    233             if ( ! is_wp_error( $uploaded ) ) {
    234                 $editor = wp_get_image_editor( $uploaded['path'] );
    235                 unset( $uploaded['path'] );
    236 
    237                 if ( ! is_wp_error( $editor ) ) {
    238                     $metadata['sizes'] = $editor->multi_resize( $sizes );
    239                     $metadata['sizes']['full'] = $uploaded;
     231                $sizes[ $s ]['crop'] = get_option( "{$s}_crop" );
     232            }
     233        }
     234
     235        // Only load PDFs in an image editor if we're processing sizes.
     236        if ( ! empty( $sizes ) ) {
     237            $editor = wp_get_image_editor( $file );
     238
     239            if ( ! is_wp_error( $editor ) ) { // No support for this type of file
     240                $uploaded = $editor->save( $file, 'image/jpeg' );
     241                unset( $editor );
     242
     243                // Resize based on the full size image, rather than the source.
     244                if ( ! is_wp_error( $uploaded ) ) {
     245                    $editor = wp_get_image_editor( $uploaded['path'] );
     246                    unset( $uploaded['path'] );
     247
     248                    if ( ! is_wp_error( $editor ) ) {
     249                        $metadata['sizes'] = $editor->multi_resize( $sizes );
     250                        $metadata['sizes']['full'] = $uploaded;
     251                    }
    240252                }
    241253            }
Note: See TracChangeset for help on using the changeset viewer.