=== PDF Image Generator === Contributors: fishpie Donate link: http://web.contempo.jp/donate?pigen Tags: pdf, thumbnail, jpg, image, upload, convert, attachment Plugin URI: http://web.contempo.jp/weblog/tips/p1522 Requires at least: 4.0 Tested up to: 4.3.1 Stable tag: 1.2.2 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html Generate thumbnail of PDF document by using ImageMagick. Insert PDF link with image into editor. Allow PDF to use as featured image. == 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 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. = Features = This Plugins replaces and extends the following features.
== Screenshots ==
1. No setup, just upload a PDF file and insert it into the editor.
2. An inserted PDF image is editable (align, sizes, etc…) just like an ordinary image file.
3. Also a PDF file is settable as a featured image and usable functions in the same way as image file.
4. In the plugin page, control default settings and generate thumbnails of already-uploaded PDFs.
== Installation ==
1. Copy the 'pdf-image-generator' folder into your plugins folder.
2. Activate the plugin via the 'Plugins' admin page. The plugin requires no setup or modifying core wordpress files.
See the 'Other Notes' tab for more info.
== Other Notes ==
= Set thumbnail in template file =
The image ID is stored in ['_thumbnail_id'] custom field of PDF attachment post. When to output the image ID in template files, write codes like below.
$pdf_id = 'your pdf file ID';
if ( $thumbnail_id = get_post_meta( $pdf_id, '_thumbnail_id', true ) ) {
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>';
}
You can also directly use wp_get_attachment_image function to a pdf as an image (from v1.2). *If the plugin is deactivated, this function just will return false.
$pdf_id = 'your pdf file ID';
echo wp_get_attachment_image ( $pdf_id, 'medium' );
= Set PDF thumb as Featured Image =
Allow to set a pdf as a featured image and to use get_the_post_thumbnail function directly.(from v1.2) *If the plugin is deactivated, this function just will return false.
echo get_the_post_thumbnail ( $post->ID, ’medium' );
= Display attachment data with in caption =
The plugin allows you to insert [caption] shortcode into the post content area as when insert an image. If you want 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. '<br/>' . // title
'caption : ' .$attr['caption']. '<br/>' .// caption
'size : ' .$bytes. '<br/>' . // file size
'filetype : ' .get_post_mime_type ( $attachment_id ). '<br/>' . // file type
'description : ' .$attachment->post_content. '<br/>'; // 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".
== ChangeLog ==
= 1.2.2 =
14.Oct 2015. Debug.
= 1.2.1 =
12.Oct 2015. Add the plugin page, customizable options, and Japanese language files.
= 1.2.0 =
09.Oct 2015. Thumbnail files themselves now hidden in the media library. Allow user to set a pdf as a featured image and to use wp_get_attachment_image function to a pdf file.
= 1.1.6 =
28.June 2015. Disabled pigeon_change_icon function only in the static media library.
= 1.1.5 =
13.May 2015. Automatically add caption short-code when the caption field filled.
= 1.1.4 =
02.May 2015. Remove the [0] from the image file name.
= 1.1.3 =
24.Apr 2015. Change the way to check in an activation if exec() is enabled or disabled on a server.
= 1.1.2 =
4.Apr 2015. Remove the process of generating a test file.
= 1.1.1 =
14.Mar 2015. Fixed colorspace bug, modified register_activation_hook and error messages.
= 1.1 =
3.Feb 2015. Added support for imagick API, and added uninstall.php.
= 1.0.1 =
17.Jan 2015. Added Verifying ImageMagick installation
= 1.0 =
12.Jan 2015. First public version Release
= 0.1 =
26.Sep 2014. Initial Release