Plugin / PDF Thumbnails
Description
Description
This plugin hooks into the media manager and generates a thumbnail everytime a
PDF is uploaded. Generated thumbnail is an image of the first page in the
uploaded document and is named PDFNAME-thumbnail, where PDFNAME is replaced
by uploaded document filename.
Generated thumbnails are equivalent to featured
images so that common thumbnail
functions like get_post_thumbnail_id() can be used for PDF attachments. See
Post Thumbnails for information
on how you can use thumbnails efficiently.
Integration with the javascript media API is not yet implemented, therefore, you
may need to reload the page before you can see generated thumbnail after an
upload.
Shortcodes
It is possible to display a thumbnail linking to an uploaded PDF using the [pdf_thumbnails_link]
shortcode. The following attributes are supported:
id– Attachment ID (required)size– Thumbnail size (optional)title– Anchor title attribute (optional)target– Anchor target attribute (optional)download– Anchor download attribute (optional)
Example 1: Display link to PDF with ID = 172 using default thumbnail size
[pdf_thumbnails_link id="172"]
Example 2: Display link to PDF with ID = 172 using thumbnail size (default 150×150)
[pdf_thumbnails_link id="172" size="thumbnail"]
Thanks to mirgcire for providing the first
version of the [pdf_thumbnails_link] shortcode.
Developer API
In most cases it should be sufficient to use built-in thumbnail functions from
the WordPress API (get_post_thumbnail and similar). If you need to modify the
way thumbnails are generated, you can override image generation with
the pdf_thumbnails_generate_image_blob filter.
Example 1: Increase resolution for all generated PDF thumbnails
// $blob is the current image blob (defaults to null, can be used for chaining)
// $filename is the PDF filename
add_action('pdf_thumbnails_generate_image_blob', function ($blob, $filename) {
$imagick = new Imagick();
$imagick->setResolution(200,200);
$imagick->readImage($filename);
$imagick->setIteratorIndex(0);
$imagick->setImageFormat('jpg');
return $imagick->getImageBlob();
}, 10, 2);
It is possible to modify generated thumbnail links using the pdf_thumbnails_link_shortcode
filter. The following attributes are available:
$html– Generated HTML code to be displayed$attachmentId– Sanitized ID of the PDF attachment$size– Sanitized thumbnail size$atts– Shortcode attributes (not sanitized)$content– Shortcode content (not sanitized)
Example 2: Wrap thumbnail link in figure and append caption
add_filter('pdf_thumbnails_link_shortcode', function ($html, $attachmentId, $size, $atts, $content) {
return "<figure>$html <caption>Click to open image $attachmentId</caption></figure>";
}, 10, 5);
TODO
Add generated image to media browser after upload.
Outline of an implementation based on the javascript media API:
// New uploads
wp.Uploader.queue.on('add', function (attachment) {
if (attachment.subtype !== 'pdf') {
return;
}
findThumbnailFor(attachment.ID).then(function (data) {
// Add attachment thumbnail to browser
var attachment = wp.media.model.Attachment.get(id)
attachment.fetch().done(function () {
wp.media.editor.open().state().get('library').add(generated attachment)
});
});
});
Filter: ajax_query_attachments_args
Ratings
Rating breakdown
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.