WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/26/2016 07:27:51 AM (3 years ago)
Author:
mikeschroder
Message:

Media: Add support for rendering PDF thumbnails.

When support for PDFs is available, on upload,
render 'Thumbnail', 'Medium', 'Large', and 'Full' sizes of
the first page, and save them in attachment meta.

Use these renders within Add Media, Media Gallery and List views,
Attachment Details, Post/Attachment Edit screens, and Attachment pages.

Support available by default via Imagick -> ImageMagick -> Ghostscript,
but can be provided by any WP_Image_Editor that supports PDFs.

Props adamsilverstein, azaozz, celloexpressions, desrosj, dglingren, ericlewis, ipstenu, joemcgill, joyously, markoheijnen, melchoyce, mikeschroder, tomauger.
Fixes #31050.

File:
1 edited

Legend:

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

    r38333 r38949  
    7777    $metadata = array();
    7878    $support = false;
    79     if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
     79    $mime_type = get_post_mime_type( $attachment );
     80
     81    if ( preg_match( '!^image/!', $mime_type ) && file_is_displayable_image( $file ) ) {
    8082        $imagesize = getimagesize( $file );
    8183        $metadata['width'] = $imagesize[0];
     
    202204        }
    203205    }
     206    // Try to create image thumbnails for PDFs
     207    else if ( 'application/pdf' === $mime_type ) {
     208        $editor = wp_get_image_editor( $file );
     209
     210        $fallback_sizes = array(
     211            'thumbnail',
     212            'medium',
     213            'large',
     214        );
     215
     216        $sizes = array();
     217
     218        foreach ( $fallback_sizes as $s ) {
     219            $sizes[$s]['width']  = get_option( "{$s}_size_w" );
     220            $sizes[$s]['height'] = get_option( "{$s}_size_h" );
     221
     222            // Force thumbnails to be soft crops.
     223            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;
     240                }
     241            }
     242        }
     243    }
    204244
    205245    // Remove the blob of binary data from the array.
Note: See TracChangeset for help on using the changeset viewer.