diff --git a/wp-admin/includes/image.php b/wp-admin/includes/image.php index 84e4c4c070..87b819180b 100644 --- a/wp-admin/includes/image.php +++ b/wp-admin/includes/image.php @@ -76,7 +76,9 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) { $metadata = array(); $support = false; - if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) { + $mime_type = get_post_mime_type( $attachment ); + + if ( preg_match( '!^image/!', $mime_type ) && file_is_displayable_image( $file ) ) { $imagesize = getimagesize( $file ); $metadata['width'] = $imagesize[0]; $metadata['height'] = $imagesize[1]; @@ -201,6 +203,44 @@ 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', + ); + + $sizes = array(); + + foreach ( $fallback_sizes as $s ) { + $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" ); + } + } + + if ( ! is_wp_error( $editor ) ) { // No support for this type of file + $uploaded = $editor->save( $file, 'image/jpeg' ); + unset( $editor ); + + // 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; + } + } + } + } // Remove the blob of binary data from the array. if ( $metadata ) { diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php index 1ccb9980d2..c79e312444 100644 --- a/wp-admin/includes/media.php +++ b/wp-admin/includes/media.php @@ -2766,7 +2766,17 @@ function edit_form_image_editor( $post ) { echo wp_video_shortcode( $attr ); - else : + elseif ( isset( $thumb_url[0] ) ): + + ?> +
+ image = new Imagick( $this->file ); + $this->image = new Imagick(); + $file_parts = pathinfo( $this->file ); + + // By default, PDFs are rendered in a very low resolution. + // We want the thumbnail to be readable, so increase the rendering dpi. + if ( 'pdf' == strtolower( $file_parts['extension'] ) ) { + $this->image->setResolution( 128, 128 ); + } + + // Reading image after Imagick instantiation because `setResolution` + // only applies correctly before the image is read. + $this->image->readImage( $this->file ); if ( ! $this->image->valid() ) return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file); diff --git a/wp-includes/media-template.php b/wp-includes/media-template.php index c6699216e9..95fbb9ab4b 100644 --- a/wp-includes/media-template.php +++ b/wp-includes/media-template.php @@ -290,9 +290,9 @@ function wp_print_media_templates() {