[WordPress] 外掛分享: PDF Thumbnails

首頁外掛目錄 › PDF Thumbnails
1,000+
安裝啟用
★★★★
4.6/5 分(9 則評價)
3432 天前
最後更新
問題解決
v2.2.0 上架:2014-04-06

內容簡介

此外掛在媒體管理員中加入 hook,每當上傳 PDF 時就會產生縮圖。所產生的縮圖為所上傳文件中的第一頁圖像,並以 PDFNAME-thumbnail 命名,其中 PDFNAME 會被替換為所上傳文件名稱。

產生的縮圖等同於 特色圖像,因此可以使用常見的縮圖函數,例如 get_post_thumbnail_id(),而這些函數也可以用於 PDF 附件。有關如何有效使用縮圖的資訊,請參閱 推薦圖片功能。

目前尚未整合 JavaScript 媒體 API,因此上傳後可能需要重新載入頁面,才能看到產生的縮圖。

Shortcodes

使用 [pdf_thumbnails_link] shortcode 可以顯示連結到上傳 PDF 的縮圖。以下支援的屬性:

id – 附件 ID(必填)
size – 縮略圖尺寸(可選)
title – 錨點標題屬性(選用)
target – 錨點目標屬性(選用)
download – 下載屬性(選用)

範例 1:使用預設縮圖尺寸顯示 ID = 172 的 PDF 連結

[pdf_thumbnails_link id="172"]

範例 2:使用縮圖尺寸(預設值為 150x150)顯示 ID = 172 的 PDF 連結

[pdf_thumbnails_link id="172" size="thumbnail"]

感謝 mirgcire 提供第一個 [pdf_thumbnails_link] shortcode。

Developer API

在大多數情況下,使用 WordPress API 的內建縮圖函數(例如 get_post_thumbnail 和類似函數)即可滿足需求。如果需要更改縮圖生成方式,可以使用 pdf_thumbnails_generate_image_blob filter 覆寫圖像生成。

範例 1:增加所有生成 PDF 縮圖的解析度

// $blob 為目前的圖像 blob(預設為 null,可用於串連)
// $filename 為 PDF 檔案名稱
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);

也可以使用 pdf_thumbnails_link_shortcode filter 修改產生的縮圖連結,以下是可用的屬性:

$html – 生成的 HTML 代碼以顯示
$attachmentId – 已清理的 PDF 附件 ID
$size – 使用的縮略圖尺寸

外掛標籤

開發者團隊

⬇ 下載最新版 (v2.2.0) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「PDF Thumbnails」→ 直接安裝(推薦)
📦 歷史版本下載

原文外掛簡介

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 "

$html

Click to open image $attachmentId

";
}, 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

延伸相關外掛

文章
Filter
Mastodon