[wp-trac] [WordPress Trac] #31050: Better PDF Upload Management

WordPress Trac noreply at wordpress.org
Mon Jan 19 00:28:45 UTC 2015


#31050: Better PDF Upload Management
--------------------------------+-----------------------------
 Reporter:  celloexpressions    |      Owner:
     Type:  feature request     |     Status:  new
 Priority:  normal              |  Milestone:  Awaiting Review
Component:  Media               |    Version:
 Severity:  normal              |   Keywords:  needs-patch
  Focuses:  ui, administration  |
--------------------------------+-----------------------------
 Over the past several releases, WordPress has added improved support for
 more and more media types, particularly images, audio, and video. All
 other file types get reasonably decent handling, with file type icons in
 the admin and link-insertion into content for the front-end.

 But a particularly notable gap is with PDF files. Even though it's
 generally preferred to put content directly into html/site format, there
 are still many unavoidable situations where users must post PDF files.

 In my recent [http://celloexpressions.com/blog/new-sheet-music-library-
 the-story/ sheet music library project], I discovered that PDFs can easily
 be converted to images using ImageMagick and WP_Image_Editor. If we create
 thumbnail images of PDFs on upload, we could use those images instead of
 the generic document icon in the admin media library. Additionally, we
 could explore improved means of displaying PDFs on the front-end. For
 example, this could be accomplished by offering the user the ability to
 insert a thumbnail-image-preview, linked to the PDF file, instead of a
 plain-text link.

 This is the basic code for generating a thumbnail of a PDF on upload, and
 storing it as post meta in a plugin:
 {{{
 /**
  * When a PDF is uploaded, generate a thumbnail image and save it in the
 attachment meta.
  */
 add_filter( 'wp_generate_attachment_metadata',
 'prefix_generate_pdf_thumbnail_metadata', 10, 2 );
 function prefix_generate_pdf_thumbnail_metadata( $metadata, $attachment_id
 ) {
         $attachment = get_post( $attachment_id );
         if ( 'application/pdf' === get_post_mime_type( $attachment ) ) {
                 // Create a png from the pdf.
                 $file = get_attached_file( $attachment_id );
                 $pdf = wp_get_image_editor( $file );
                 if ( ! is_wp_error( $pdf ) ) { // Most likely cause for
 error is that ImageMagick is not available.
                         $filename = $pdf->generate_filename( 'image',
 null, 'png' );
                         $uploaded = $pdf->save( $filename, 'image/png' );
                         if ( ! is_wp_error( $uploaded ) ) {
                                 $upload_dir = wp_upload_dir();
                                 update_post_meta( $attachment_id,
 'pdf_thumbnail_url', $upload_dir['url'] . '/' . $uploaded['file'] );
                         }
                 }
         }
         return $metadata;
 }
 }}}

 I'd like to propose with this ticket that we explore creating thumbnails
 of PDFs when they're uploaded and use them for improved administration and
 display purposes, when the server environment supports ImageMagick, of
 course.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/31050>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list