mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-12 04:40:26 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@38949 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -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 ) {
|
||||
|
||||
Reference in New Issue
Block a user