I'm working with some great code that was provided recently to create an image thumbnail from a PDF upload, and I'm now trying to implement this in the functions file so that it can be applied to any new uploads, so the thumbnail can be displayed within posts.

The trick I'm trying to accomplish though is to use the function on Gravity Forms file uploads, using a custom field in a public form where users can upload a PDF file. Based on another discussion, I thought that this might have to be accomplished using wp_handle_upload() and then wp_insert_attachment() to insert the created file in the media library. Does anyone have any tips on this? I imagined that the function would fire when the form was submitted/when the post was created by the form.

Here's the original code provided by ialocin:

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

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


    return $metadata;


UPDATE: So far, because of my lack of experience, I haven't figured this out. At this point I know I need to use [gform_after_submission][4] or [gform_pre_submission][5] and [gform_upload_path][6] could be useful too, and then I need wp_insert_attachment when the JPG has been created, but I haven't figured out how to make all of this work together.

I'm going to do more research and see what I can figure out, but I wanted to share that for now.

  • I don't know if »great code« is accurate, but I guess you are kind because I helped you. Besides that, I assumed you did you own script/implementation based on the information and you are having problems with it. This way it is a bit problematic, because third-party related questions are off-topic. I'm staying neutral in this case and maybe you are lucky and someone with knowledge about gravity forms can quickly help you out with it. – Nicolai Apr 9 '14 at 15:28
  • Actually, assuming - never used it - Gravity Forms uses the core functions, you probably only have to incorporate the part where the thumbnail gets added to the media library with wp_insert_attachment(). Use the search on here this has been asked an answered multiple times. And, as you are changing the approach a bit, you might have to rework some minor parts. Did you verify that the thumbnail and the metadata gets created? – Nicolai Apr 9 '14 at 15:46
  • Thanks - checking into what you recommended. The more I work on this though, the more problematic is seems. Gravity forms is adding the file to its own upload folder, and saves it to its own upload folder in the uploads directory. It seems to bypass the Wordpress upload setup. From what I see though, I can possibly use gform_after_submission (gravityhelp.com/documentation/page/Gform_after_submission) to start the process. I just need to find how to grab the image location. – Andrew Apr 9 '14 at 19:46
  • Actually, this could help too: gform_upload_path allows you to change where the file is located: gravityhelp.com/documentation/page/Gform_upload_path. Just need to figure out now how to put all of this together. – Andrew Apr 9 '14 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.