[WordPress] 外掛分享: PDF Image Generator

首頁外掛目錄 › PDF Image Generator
WordPress 外掛 PDF Image Generator 的封面圖片
⚠ 此外掛已下架 — 不再更新維護,建議勿安裝。
10,000+
安裝啟用
★★★★
4.4/5 分(21 則評價)
3244 天前
最後更新
問題解決
WordPress 4.0+ v1.5.6 上架:2015-01-13

內容簡介

這個外掛可以透過上傳 PDF 附件,將封面轉換為 JPEG,並作為文章縮略圖檔案附加。它還允許顯示縮略圖標誌和插入 PDF 鏈接與封面圖像到編輯器。

僅支援 WordPress 4.0+ 版本,而且需要安裝 ImageMagick 和 GhostScript,才能在伺服器上運行。

此外掛將鉤住媒體編輯器,並使用 ImageMagick 與 GhostScript 生成 PDF 的第一頁圖像。該外掛需要使用者啟用,無需進行任何配置。允許將 PDF 設置為圖片使用。

外掛的工作原理包括以下功能:

- 使用 ImageMagick 生成 PDF 的封面圖像。生成的圖像具有不同的大小變形並附加到 PDF 上。
- 將生成的圖像註冊為 PDF 的特色圖像(文章縮略圖)。
- 在管理頁面上使用產生的圖像作為圖示,而非 MIME 類型圖示。
- 在媒體庫中隱藏產生的圖像文件。(v1.2 或更高版本)
- 插入 PDF 到文字編輯器時,將鏈接文本替換為 JPG。
- 從媒體庫中刪除 PDF 時刪除產生的圖像。
- 允許在模板文件中手動管理和輸出產生的圖像和 PDF。(參見其他注意事項。)
- 允許將 PDF 設置為特色圖像,並像圖像文件一樣使用功能。(v1.2 或更高版本,參見其他注意事項。)
- 允許在外掛設置頁面中控制產生的圖像的最大大小和其他默認設置。(v1.3.4 或更高版本)

產生的圖像文件會註冊為 PDF 文件的子項,並具有不同的大小變形。檔案構成如下:

my-file.pdf (您的 PDF)

- my-file-pdf.jpg (您的 PDF 的生成封面圖像)

- my-file-pdf-1024x768.jpg(大尺寸)
- my-file-pdf-300x225.jpg(中等大小)
- my-file-pdf-150x150.jpg(縮略圖大小)
- …(以及您的自定義文件大小)

選擇媒體上傳工具中的 PDF 文件,並將其插入到編輯器中。輸出的 HTML 會自動重寫如下。

<a class="link-to-pdf" title="dummy-pdf" href="http://example.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://example.com/wp-content/uploads/2015/01/dummy-pdf-pdf-227x320.jpg" alt="thumbnail-of-dummy-pdf" /></a>

如果正在使用文檔查看器外掛(如 GDE)並希望插入由其生成的 HTML,請選擇“媒體”的“默認(標題)”。

生成的圖像 ID 存儲在 PDF 附件文章的 [ '_thumbnail_id' ] 自定義字段中。在模板文件中,可以使用 get_post_thumbnail_id($your_pdf_id) 或 get_post_meta($your_pdf_id, '_thumbnail_id', true) 函数導出圖像 ID。

或者更簡單地,您可以通過 wp_get_attachment_image($your_pdf_id, $size) 和 wp_get_attachment_link($your_pdf_id, $size) 函数調用圖像並鏈接到 PDF 文件。(v1.2 或更高版本)*如果該外掛已停用,則此功能將返回空值。

外掛標籤

開發者團隊

⬇ 下載最新版 (v1.5.6) 或搜尋安裝

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

原文外掛簡介

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.
thumbnail-of-dummy-pdf

*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 ''.wp_get_attachment_image ( $thumbnail_id, 'medium' ).'';
}

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.
ID );
if( $pdfs ): foreach( $pdfs as $pdf ):
$thumbnail_id = get_post_thumbnail_id( $pdf->ID );
if( $thumbnail_id ):
echo '

  • ';
    echo '';
    echo wp_get_attachment_image ( $thumbnail_id, 'medium' );
    echo '
    ';
    echo '
  • ';
    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 ''.get_the_post_thumbnail().''."\n";
    }

    Display attachment data with in caption
    The plugin allows you to insert 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
    '

    ' .
    do_shortcode( $content ). '

    ' .$attr['caption']. '

    ' .
    '

    ';
    }
    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 = '' .$attach_output. '';
    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 = ''.get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true ).'';
    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' );

    延伸相關外掛

    文章
    Filter
    Apply Filters
    Mastodon