Plugin / PDF Image Generator

Mizuho Ogino

Description

Description

By uploading a PDF attachment, this plugin convert the cover page to jpeg and attach it as a post thumbnail file. It also allows displaying a thumbnail icon and inserting PDF link with a cover image into the editor.

Available only for WordPress 4.0+, also only on the server which ImageMagick and GhostScript are installed.

This plugin hooks to the media editor and generates the first page image of PDF by using ImageMagick with GhostScript. It requires no setup, just activate the plugin via the admin install page. Allow to set a PDF as an image.

How it works

This Plugin replaces and extends the following features.

  • Generate cover image of PDF by using ImageMagick. Generated Image has different size variations and is attached to PDF.
  • Register Generated Image as Featured Image (post-thumbnail) of PDF.
  • Display Generated Image as icon instead of Mime-type Icon in Admin Page.
  • Hide Generated Image file itself in Media Library. (v1.2 or later)
  • Replace link text with JPG when inserting PDF to Text Editor.
  • Delete Generated Image when deleting PDF from Media Library.
  • Allow to manage and output Generated Image and PDF on manually in template file. (see Other Notes.)
  • Allow to set PDF as Featured Image and to use functions in the same way as image file. (v1.2 or later, see Other Notes.)
  • Allow to control maximum size of Generated Image and other default settings in Plugin Page. (v1.3.4 or later)

Generated Items

A generated image file is registered as post children of a PDF file and it has different size variations. Files build a tree like below.

  • my-file.pdf (Your PDF)
    • my-file-pdf.jpg (Generated Cover Image of Your PDF)
      • my-file-pdf-1024×768.jpg (Large Size)
      • my-file-pdf-300×225.jpg (Medium Size)
      • my-file-pdf-150×150.jpg (Thumbnail size)
      • …(And Your Custom File Sizes)

Insert HTML into the editor

Select a PDF file in the Media Uploader and insert it into the the editor. An output HTML is automatically rewritten like below.

<a class="link-to-pdf" title="dummy-pdf" href="http://exmaple.com/wp-content/uploads/2015/01/dummy-pdf.pdf" target="_blank"><img width="227" height="320" class="size-medium wp-image-9999 thumb-of-pdf" src="http://exmaple.com/wp-content/uploads/2015/01/dummy-pdf-pdf-227x320.jpg" alt="thumbnail-of-dummy-pdf" /></a>

*If you are using a document viewer plugin (like GDE) and want insert html which is made by it, select “Default (Title)” of “media” selector.

Get thumbnail in template file

A generated image ID is stored in [‘_thumbnail_id’] custom field of a PDF attachment post. An image ID is exportable by using get_post_thumbnail_id($your_pdf_id) or get_post_meta($your_pdf_id, ‘_thumbnail_id’, true) functions in a template file.

$pdf_id = 'your PDF file ID';
if ( $thumbnail_id = get_post_thumbnail_id( $pdf_id ) ) {
echo '<a class="pdf-link image-link" href="'.wp_get_attachment_url( $pdf_id ).'" title="'.esc_attr( get_the_title( $pdf_id ) ).'" target="_blank">'.wp_get_attachment_image ( $thumbnail_id, 'medium' ).'</a>';
}

Or more simply, you can call images and link to PDF files by wp_get_attachment_image($your_pdf_id, $size) and wp_get_attachment_link($your_pdf_id, $size) functions. (v1.2 or later) *If the plugin is deactivated, this function just will return empty.

$pdf_id = 'your PDF file ID';
echo wp_get_attachment_image ( $pdf_id, 'medium' );
echo wp_get_attachment_link ( $pdf_id, 'medium' );

If you call all PDFs that are attached to the post. You can get them by get_posts function.

Examples.
Dynamically get PDFs that are attached to the post.

<?php
$pdfs = get_posts( 'posts_per_page=-1&post_type=attachment&post_mime_type=application/pdf&post_parent='.$post->ID );
if( $pdfs ): foreach( $pdfs as $pdf ):
  $thumbnail_id = get_post_thumbnail_id( $pdf->ID );
  if( $thumbnail_id ):
    echo '<li>';
    echo '<a href="'wp_get_attachment_url( $pdf->ID )'" class="link-to-pdf">';
    echo wp_get_attachment_image ( $thumbnail_id, 'medium' );
    echo '</a>';
    echo '</li>';
  endif;
endforeach; endif;
?>

Set PDF thumbnail as Featured Image

The plugin support you to set PDF as Featured Image. (v1.2 or later)
Strictly explaining, when you set a PDF as Featured Image, the plugin automatically set the thumbnail which is registered with this PDF.
You can call thumbnail by using get_the_post_thumbnail function.

echo get_the_post_thumbnail ( $post->ID, 'medium' );

May you want pdf file from the post thumbnail.

$thumb_id = get_post_thumbnail_id ( $post->ID );
$pdf_id = get_post( $thumb_id )->post_parent;
if ( $pdf_id && get_post_mime_type ( $pdf_id ) === 'application/pdf' ){
  $pdf = get_post($pdf_id);
  echo '<a class="link-to-pdf" href="'.wp_get_attachment_url($pdf_id).'" title="'.esc_html($pdf->post_title).'" target="_blank">'.get_the_post_thumbnail().'</a>'."\n";
}

Display attachment data with in caption

The plugin allows you to insert [caption] short-code into the post content area as when insert an image. If you want to display attachment title, description, file type, file size and so on, use img_caption_shortcode filter in your functions.php.
Here’s an example code…
function add_attachment_data_in_caption( $empty, $attr, $content ) {
$attr = shortcode_atts( array( ‘id’=>”, ‘align’=>’alignnone’, ‘width’=>”, ‘caption’=>” ), $attr );
if ( 1 > (int) $attr[‘width’] || empty( $attr[‘caption’] ) ) return ”;
if ( $attr[‘id’] ) {
$attr[‘id’] = ‘id=”‘ . esc_attr( $attr[‘id’] ) . ‘” ‘;
$attachment_id = explode(‘_’, $attr[‘id’]);
$attachment_id = $attachment_id[1];// get attachment id
if( get_post_mime_type ( $attachment_id ) === ‘application/pdf’ ){
$attachment = get_post( $attachment_id );
$bytes = filesize( get_attached_file( $attachment->ID ) );
if ($bytes >= 1073741824) $bytes = number_format($bytes / 1073741824, 2). ‘ GB’;
elseif ($bytes >= 1048576) $bytes = number_format($bytes / 1048576, 2). ‘ MB’;
elseif ($bytes >= 1024) $bytes = number_format($bytes / 1024, 2). ‘ KB’;
elseif ($bytes > 1) $bytes = $bytes. ‘ bytes’;
elseif ($bytes == 1) $bytes = $bytes. ‘ byte’;
else $bytes = ‘0 bytes’;
$attr[‘caption’] =
‘title : ‘ .$attachment->post_title. ‘
‘ . // title
‘caption : ‘ .$attr[‘caption’]. ‘
‘ .// caption
‘size : ‘ .$bytes. ‘
‘ . // file size
‘filetype : ‘ .get_post_mime_type ( $attachment_id ). ‘
‘ . // file type
‘description : ‘ .$attachment->post_content. ‘
‘; // description
}
}

    return 
      '<div ' .$attr['id']. 'class="wp-caption ' .esc_attr( $attr['align'] ). '" style="max-width: ' .( 10 + (int) $attr['width'] ). 'px;">' .
      do_shortcode( $content ). '<p class="wp-caption-text">' .$attr['caption']. '</p>' .
      '</div>';
}
add_filter('img_caption_shortcode', 'add_attachment_data_in_caption', 10, 3);

Generate thumbnails of all PDFs in the media library

You can generate thumbnails of any already-uploaded PDFs.
Activate the plugin and Click “Generate Thumbnails Now” link in “settings”. (v1.3.3 or later)

Change attachment link attributes

function pigen_filter_attachment_link ( $html, $attach_id, $attach_url, $attach_output ){
  $attach_title = get_the_title( $attach_id );
  $html = '<a class="link-to-pdf" href="'.$attach_url.'" rel="attachment wp-att-' .esc_attr($attach_id). '" title="'.esc_attr( $attach_title ).'" target="_blank">' .$attach_output. '</a>';
  return $html;
};
add_filter( 'pigen_filter_attachment_link', 'pigen_filter_attachment_link', 10, 4 );

Change thumbnail attributes

function pigen_filter_attachment_output ( $attach_output, $thumbnail_id, $thumbnail, $size, $align ){
  $attach_output =  '<img src="'. $thumbnail[0] .'" alt="'.get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true ).'" width="'. $thumbnail[1] .'" height="'. $thumbnail[2] .'" class="'.( $align ? 'align' .$align. ' ' : '' ). 'size-' .esc_attr( $size ). ' wp-image-'.$thumbnail_id.'" />';
  return $attach_output;
};
add_filter( 'pigen_filter_attachment_output', 'pigen_filter_attachment_output', 10, 5 );

Modify imageMagick’s Settings

Filters allow you to add your own modify imageMagick’s behaviour by hooking.

Sample usage for imageMagick user.

function pigen_filter_convert_file_basename( $file_basename ){
  $file_basename = str_replace( '.jpg', '.png', $file_basename );
  return $file_basename;
};
add_filter( 'pigen_filter_convert_file_basename', 'pigen_filter_convert_file_basename' );

function pigen_filter_convert_imageMagick( $imageMagick, $before_name, $after_name, $max_width, $max_height ){
  $imageMagick = "convert -density 150 -quality 80 -background black -flatten {$max_width}x{$max_height} {$before_name} {$after_name}";
  return $imageMagick;
};
add_filter( 'pigen_filter_convert_imageMagick', 'pigen_filter_convert_imageMagick', 10, 5 );

Sample usage for imagick extension user.

function pigen_filter_convert_file_basename( $file_basename ){
  $file_basename = str_replace( '.jpg', '.png', $file_basename );
  return $file_basename;
};
add_filter( 'pigen_filter_convert_file_basename', 'pigen_filter_convert_file_basename' );

function pigen_filter_convert_imagick( $imagick ){
  $imagick->setImageBackgroundColor( 'black' );
  $imagick->setCompressionQuality( 80 );
  $imagick->setImageFormat( 'png' );
return $imagick;
};
add_filter( 'pigen_filter_convert_imagick', 'pigen_filter_convert_imagick' );

Automatically save image/PDF as featured image

Automatically set PDF thumbnail as featured image.

function save_pdf_thumb_as_featuredimage ( $post_id ) { 
    if ( wp_is_post_revision( $post_id ) ) return; 
    if ( get_post_type( $post_id ) !== 'post' ) return; // set your post type
    if ( get_post_meta( $post_id, '_thumbnail_id', true ) ) return; // post already has featured image
    $attaches = get_posts ( 'post_parent='.$post_id.'&numberposts=-1&post_type=attachment&post_mime_type=application/pdf&orderby=menu_order&order=ASC' );
    if ( $attaches ): foreach( $attaches as $attach ):
        if ( $thumb_id = get_post_meta( $attach->ID, '_thumbnail_id', true ) ){ // if pdf has thumbnail
            update_post_meta( $post_id, '_thumbnail_id', $thumb_id );
            break;
        }
    endforeach; endif;
}
add_action( 'save_post', 'save_pdf_thumb_as_featuredimage' );

Automatically set first image/PDF as featured image.

function save_thumbnail_as_featuredimage ( $post_id ) { 
    if ( wp_is_post_revision( $post_id ) ) return;
    if ( get_post_type( $post_id ) !== 'post' ) return; // set your post type
    if ( get_post_meta( $post_id, '_thumbnail_id', true ) ) return; // post already has featured image
    $args = array(
        'post_parent' => $post_id,
        'post_type' => 'attachment',
        'numberposts' => -1,
        'post_status' => null,
        'orderby' => 'menu_order date',
        'order' => 'ASC ASC'
    );
    $attaches = get_posts($args);
    if ( $attaches ): foreach( $attaches as $attach ):
        if ( $attach->post_mime_type == 'application/pdf' ){
            if ( $thumb_id = get_post_meta( $attach->ID, '_thumbnail_id', true ) ){ // if pdf has thumbnail
                update_post_meta( $post_id, '_thumbnail_id', $thumb_id );
                break;
            }
        } elseif ( preg_match("/^image\//", $attach->post_mime_type ) ) {
            update_post_meta( $post_id, '_thumbnail_id', $attach->ID );
            break;
        }
    endforeach; endif;
}
add_action( 'save_post', 'save_thumbnail_as_featuredimage' );

Ratings

4.5
19 reviews

Rating breakdown

Details Information

Version

1.5.6

First Released

13 Jan, 2015

Total Downloads

108,805

Wordpress Version

4.0 or higher

Tested up to:

4.7.15

Require PHP Version:

-

Tags

Contributors

Languages

DIRECTORY DISCLAIMER

The information provided in this THEME/PLUGIN DIRECTORY is made available for information purposes only, and intended to serve as a resource to enable visitors to select a relevant theme or plugin. wpSocket gives no warranty of any kind, express or implied with regard to the information, including without limitation any warranty that the particular theme or plugin that you select is qualified on your situation.

The information in the individual theme or plugin displayed in the Directory is provided by the owners and contributors themselves. wpSocket gives no warranty as to the accuracy of the information and will not be liable to you for any loss or damage suffered by you as a consequence of your reliance on the information.

Links to respective sites are offered to assist in accessing additional information. The links may be outdated or broken. Connect to outside sites at your own risk. The Theme/Plugin Directory does not endorse the content or accuracy of any listing or external website.

While information is made available, no guarantee is given that the details provided are correct, complete or up-to-date.

wpSocket is not related to the theme or plugin, and also not responsible and expressly disclaims all liability for, damages of any kind, arising out of the use, reference to, or reliance on, any information or business listed throughout our site.

Keep Leading Your Followers!
Share it for them.