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-includes/media.php

    r38812 r38949  
    184184 */
    185185function image_downsize( $id, $size = 'medium' ) {
    186 
    187     if ( !wp_attachment_is_image($id) )
    188         return false;
     186    $is_image = wp_attachment_is_image( $id );
    189187
    190188    /**
     
    210208    $is_intermediate = false;
    211209    $img_url_basename = wp_basename($img_url);
     210
     211    // If the file isn't an image, attempt to replace its URL with a rendered image from its meta.
     212    // Otherwise, a non-image type could be returned.
     213    if ( ! $is_image ) {
     214        if ( ! empty( $meta['sizes'] ) ) {
     215            $img_url = str_replace( $img_url_basename, $meta['sizes']['full']['file'], $img_url );
     216            $img_url_basename = $meta['sizes']['full']['file'];
     217            $width = $meta['sizes']['full']['width'];
     218            $height = $meta['sizes']['full']['height'];
     219        } else {
     220            return false;
     221        }
     222    }
    212223
    213224    // try for a new style intermediate size
     
    685696    if ( is_array( $size ) ) {
    686697        $candidates = array();
     698
     699        if ( ! isset( $imagedata['file'] ) && isset( $imagedata['sizes']['full'] ) ) {
     700            $imagedata['height'] = $imagedata['sizes']['full']['height'];
     701            $imagedata['width']  = $imagedata['sizes']['full']['width'];
     702        }
    687703
    688704        foreach ( $imagedata['sizes'] as $_size => $data ) {
     
    739755
    740756    // include the full filesystem path of the intermediate file
    741     if ( empty($data['path']) && !empty($data['file']) ) {
     757    if ( empty( $data['path'] ) && ! empty( $data['file'] ) && ! empty( $imagedata['file'] ) ) {
    742758        $file_url = wp_get_attachment_url($post_id);
    743759        $data['path'] = path_join( dirname($imagedata['file']), $data['file'] );
     
    31243140        $response['nonces']['delete'] = wp_create_nonce( 'delete-post_' . $attachment->ID );
    31253141
    3126     if ( $meta && 'image' === $type ) {
     3142    if ( $meta && ! empty( $meta['sizes'] ) ) {
    31273143        $sizes = array();
    31283144
     
    31723188        }
    31733189
    3174         $sizes['full'] = array( 'url' => $attachment_url );
    3175 
    3176         if ( isset( $meta['height'], $meta['width'] ) ) {
    3177             $sizes['full']['height'] = $meta['height'];
    3178             $sizes['full']['width'] = $meta['width'];
    3179             $sizes['full']['orientation'] = $meta['height'] > $meta['width'] ? 'portrait' : 'landscape';
    3180         }
    3181 
    3182         $response = array_merge( $response, array( 'sizes' => $sizes ), $sizes['full'] );
    3183     } elseif ( $meta && 'video' === $type ) {
     3190        if ( 'image' === $type ) {
     3191            $sizes['full'] = array( 'url' => $attachment_url );
     3192
     3193            if ( isset( $meta['height'], $meta['width'] ) ) {
     3194                $sizes['full']['height'] = $meta['height'];
     3195                $sizes['full']['width'] = $meta['width'];
     3196                $sizes['full']['orientation'] = $meta['height'] > $meta['width'] ? 'portrait' : 'landscape';
     3197            }
     3198
     3199            $response = array_merge( $response, $sizes['full'] );
     3200        } elseif ( $meta['sizes']['full']['file'] ) {
     3201            $sizes['full'] = array(
     3202                'url'         => $base_url . $meta['sizes']['full']['file'],
     3203                'height'      => $meta['sizes']['full']['height'],
     3204                'width'       => $meta['sizes']['full']['width'],
     3205                'orientation' => $meta['sizes']['full']['height'] > $meta['sizes']['full']['width'] ? 'portrait' : 'landscape'
     3206            );
     3207        }
     3208
     3209        $response = array_merge( $response, array( 'sizes' => $sizes ) );
     3210    }
     3211
     3212    if ( $meta && 'video' === $type ) {
    31843213        if ( isset( $meta['width'] ) )
    31853214            $response['width'] = (int) $meta['width'];
Note: See TracChangeset for help on using the changeset viewer.