=== 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.3.2 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html Generate automatically cover image of PDF by using ImageMagick. Allow user to insert link with image into editor and set 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 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 Plugins replaces and extends the following features. = Generated Items = Generated JPGs are registered as post children of the pdf. Files build a tree like below. = Insert HTML into the editor = Select PDF file by media uploader and insert it into the editor. The output HTML is automatically rewrote like below. thumbnail-of-dummy-pdf == 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. You can set PDF as Featured Image and use functions for PDF in the same way as an image file. 4. You can control default settings and generate thumbnails of already-uploaded PDFs in the plugin page. == Installation == 1. Copy the 'pdf-image-generator' folder into your plugins folder. 2. Activate the plugin via the 'Plugins' menu. 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 = 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. $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>'; } 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 (from v1.2.) *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' ); = Set PDF thumb as Featured Image = The plugin allows you 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 empty. 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 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. '<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.3.2 = 20.Oct 2015. Add an option for select the functions of imageMagick. Debug default alignment of insert media. Modified local languages. = 1.3.1 = 18.Oct 2015. Debug. = 1.3 = 17.Oct 2015. Defined classes in the plugin file. Add Attachment Display Settings when trying to insert PDF. = 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