Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm developing a website that needs to allow users, within Gravity Forms, to submit a PDF file and have a JPG thumbnail created of the first page of the PDF that is then displayed in the created post.

My plan was to have a function that would allow for this, and add the JPG link to a custom field, so it can then be displayed on the post page.

Unfortunately, while I understand the concept, and have a script I used to use to convert PDFs to JPG format in a non-Wordpress site, I'm not sure how to implement this in Wordpress, or if it's better to create this as a plugin instead of a function.

Can someone provide some direction either where I can start with this concept?

CODE: For reference, here is the specific part of the code I had from the previous scripting, but it's about 6 years old:

function make_thumbs($uploadedFpId,$issueFileprefix) {
    $issuePathprefix = $_SERVER['DOCUMENT_ROOT'].'/fp/'.$issueFileprefix;
    exec('convert -colorspace RGB -density 100x100 '.$issuePathprefix.'.pdf[0] -quality 90 '.$issuePathprefix.'.jpg');
    exec('convert '.$issuePathprefix.'.jpg  -resize 100x146 '.$issuePathprefix.'_sm.jpg'); // make thumbnail
    exec('convert '.$issuePathprefix.'.jpg  -resize 500x1000 '.$issuePathprefix.'.jpg'); // make correct size of preview
}

UPDATE: I found the following code (right here) , which seems to point further into the right direction. The catch is that I still don't see how to handle this right after upload, and also pull in the uploaded file path, and then I still need to save it to the post custom field:

$img = wp_get_image_editor( ‘my/temp/uploads/path/mypdf.pdf’ );
$img->resize( 50, 50, true );
$filename = $img->generate_filename( ‘thumb’, ‘wp/wp-content/uploads/thumbs/’, ‘jpg’ );
$img->save($filename, ‘image/jpeg’);
share|improve this question
2  
1  
Thanks, that was very helpful. Definitely seems to make more sense in this case to use a plugin, since it's tied to files. Technically though, it also relates to the theme display, since this is for a thumbnail, but likely wiser to stick to a plugin it seems. –  Andrew Nov 7 '13 at 19:13
1  
You might want to share the code of your script to get some more help on how to implement it with your wordpress installation. –  ialocin Nov 7 '13 at 19:18
    
From your edit I'm guessing you really just needed a hint on how to start. But also for reference reasons, there are two possibly helpful question on Stack Overflow I want to share: How do I convert a PDF document to a preview image in PHP? and Creating PDF thumbnail in PHP and caching it. –  ialocin Nov 7 '13 at 21:24
    
Thanks, @ialocin. That does help. The problem I have is that there's lots of code to do the conversion out there, but I don't seen any examples of where I could start to bring that into a plugin. The closest I've come to finding a solution is here: make.wordpress.org/core/2012/12/06/wp_image_editor-is-incoming but it specifically deals with image editing, and I want to initiate this plugin or function before this, since it's a PDF and can't be edited in the WP_Image_Editor as far as I can tell. –  Andrew Nov 7 '13 at 22:23

1 Answer 1

up vote 3 down vote accepted

If I'm not totally mistaken, the code you have given on your update won't work, because the file/mime type pdf isn't supported by the WP_image_editor class called by wp_get_image_editor(). Creating a thumbnail from a uploaded pdf file can be achieved though.

Below code gives you a insight in a possibility how to do it, I commented all the important things to make it better understandable for you. The thumbnail creation gets automated by hooking into the wp_generate_attachment_metadata the hook to the function and checking for the mime type of the uploaded file.

Code:

add_filter( 'wp_generate_attachment_metadata', 'wpse121600_create_pdf_thumbnail', 10, 2 );
function wpse121600_create_pdf_thumbnail( $metadata, $attachment_id ) {

    //Get the attachment/post object
    $attachment_obj = get_post( $attachment_id );

    //Check for mime type pdf
    if( 'application/pdf' == get_post_mime_type( $attachment_obj ) ) {

        //Get attachment URL http://yousite.org/wp-content/uploads/yourfile.pdf
        $attachment_url = wp_get_attachment_url($attachment_id);
        //Get attachment path /some/folder/on/server/wp-content/uploads/yourfile.pdf
        $attachment_path = get_attached_file($attachment_id );

        //By adding [0] the first page gets selected, important because otherwise multi paged files wont't work
        $pdf_source = $attachment_path.'[0]';

        //Thumbnail format
        $tn_format = 'jpg';
        //Thumbnail output as path + format
        $thumb_out = $attachment_path.'.'.$tn_format;
        //Thumbnail URL
        $thumb_url = $attachment_url.'.'.$tn_format;

        //Setup various variables
        //Assuming A4 - portrait - 1.00x1.41
        $width = '250';
        $height = '353';
        $quality = '90';
        $dpi = '300';
        $resize = $width.'x'.$height;
        $density = $dpi.'x'.$dpi;

        //For configuration/options see: http://www.imagemagick.org/script/command-line-options.php
        $a_exec = "convert -adaptive-resize $width -density $dpi -quality $quality $pdf_source $thumb_out";
        $r_exec = "convert -resize $width -density $dpi -quality $quality $pdf_source $thumb_out";
        $t_exec = "convert -thumbnail $width -density $dpi -quality $quality $pdf_source $thumb_out";
        $s_exec = "convert -scale $width $pdf_source $thumb_out";

        //Create the thumbnail with choosen option
        exec($r_exec);

        //Add thumbnail URL as metadata of pdf attachment
        $metadata['thumbnail'] = $thumb_url;

    }

    return $metadata;

}

This of course doesn't take care of all your wishes regarding your plugin, but it should get you started. The code could, for example, be used with another method of creating the thumbnails - referring to the links I posted in the comment. Besides that several additional features are imaginable, like inserting the thumbnails into the media library, but that's just to give you an outlook.

share|improve this answer
    
That looks fantastic, and--yes--it's a brilliant place to start. It covers almost all of the core things I need, so thank-you so much for going above and beyond on this. –  Andrew Nov 8 '13 at 4:00
    
I hate to bug you more on this, but I can't seem to get this to work for me, and I realize that the issue is implementing it to work on any new PDF upload. I was reading this post: wordpress.stackexchange.com/questions/4307/…. I'm assuming I need to hook into wp_handle_upload(), but I'm not sure. Then, it looks like I need wp_insert_attachment() to create the thumbnails. I don't expect you to write the code, but can you point me on implementing? –  Andrew Apr 9 at 14:22
    
Also worth mentioning that I am also trying to hook this into Gravity Forms and the upload field option there for front-end users. Perhaps I should address that in a separate discussion though. –  Andrew Apr 9 at 14:32
    
No worries, you aren't bugging me. The code in the answer is somewhat conceptual and was tailored for you to understand and get started - I never implemented that myself. You definitely should ask a new question, where you show the code you came up with and debugging information. Post the link her, so I get notified. @Andrew –  ialocin Apr 9 at 14:41
    
Thanks very much. Appreciate the tips and guidance. I have just enough information to be dangerous, but I'm a long way from being even moderately knowledgeable. –  Andrew Apr 9 at 14:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.