[WordPress] 外掛分享: PDF Image Generator

WordPress 外掛 PDF Image Generator 的封面圖片。

前言介紹

  • 這款 WordPress 外掛「PDF Image Generator」是 2015-01-13 上架。 目前已經下架不再更新,不建議安裝使用。
  • 目前有 10000 個安裝啟用數。
  • 上一次更新是 2017-04-25,距離現在已有 2930 天。超過一年沒更新,安裝要確認版本是否可用。以及後續維護問題!
  • 外掛最低要求 WordPress 4.0 以上版本才可以安裝。
  • 有 21 人給過評分。
  • 還沒有人在論壇上發問,可能目前使用數不多,還沒有什麼大問題。

外掛協作開發者

fishpie |

外掛標籤

jpg | pdf | png | image | thumbnail |

內容簡介

這個外掛可以透過上傳 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 會自動重寫如下。

thumbnail-of-dummy-pdf

如果正在使用文檔查看器外掛(如 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 或更高版本)*如果該外掛已停用,則此功能將返回空值。

原文外掛簡介

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' );

    各版本下載點

    • 方法一:點下方版本號的連結下載 ZIP 檔案後,登入網站後台左側選單「外掛」的「安裝外掛」,然後選擇上方的「上傳外掛」,把下載回去的 ZIP 外掛打包檔案上傳上去安裝與啟用。
    • 方法二:透過「安裝外掛」的畫面右方搜尋功能,搜尋外掛名稱「PDF Image Generator」來進行安裝。

    (建議使用方法二,確保安裝的版本符合當前運作的 WordPress 環境。


    1.1 | 1.3 | 1.2.2 | 1.3.1 | 1.3.2 | 1.3.3 | 1.3.4 | 1.3.5 | 1.3.6 | 1.3.7 | 1.3.8 | 1.3.9 | 1.4.0 | 1.4.1 | 1.4.2 | 1.4.3 | 1.4.4 | 1.4.5 | 1.4.6 | 1.4.7 | 1.4.8 | 1.4.9 | 1.5.0 | 1.5.1 | 1.5.2 | 1.5.3 | 1.5.4 | 1.5.5 | 1.5.6 | trunk |

    延伸相關外掛(你可能也想知道)

    • PNG to JPG 》將PNG圖像轉換為JPG,釋放網頁空間,並加快網頁速度, , 設置轉換後JPG的質量, 上傳時自動轉換, 僅當PNG沒有透明度時才自動轉換, 僅在JPG文件大小低於PNG文件...。
    • Easy Set Favicon 》透過簡單的連結設定您的部落格或網站的 favicon。, 功能, , 易於使用且簡單。, 您可以將任何類型的圖像設定為您 WordPress 網站的 favicon。, 您可以將來自網...。
    • Image Converter for WebP 》總結:這個 WordPress 外掛可以在網頁加載期間將圖片轉換為 WebP 格式,使網站加載速度極快,無任何中斷或停擺。, , ### 問題與答案:, , - **為什麼圖片對於...。
    • Image Converter & Optimizer 》這款外掛讓你輕鬆地將任何圖像格式轉換為另一種,包括 png、jpg、gif、webp 和 avif。只需點擊一下,你就可以將圖像轉換為這些格式之一,並且支援圖像子尺寸...。
    • Wp-UnitPNGfix 》如果瀏覽器是 IE6 或以下版本,這個外掛將包含unitpngfix.js JavaScript 檔案。簡單來說,這個檔案實現了 Unit Interactive Labs (http://labs.unitinteracti...。
    • Instant WebP Converter 》總結:Instant WebP Converter 自動將 JPEG 和 PNG 圖像轉換為 WebP 格式,以提升網站性能和速度。, , 問題與答案:, - Instant WebP Converter 可以自動將哪...。
    • Flickr Photo Post 》這個 Flickr WordPress 外掛與 jQuery 搭配運作,讓你可以輕鬆地在 WordPress 文章或頁面中加入 Flickr 圖片。有些 Flickr 用戶提供採用創意共享(CC)許可證的...。
    • Femora Compress – WEBP – PNG – JPG 》透過將 JPEG 和 PNG 圖像優化為 WebP,讓您的網站更快速。該外掛透過整合影像壓縮服務 FemoraPro FemoraPro,自動優化所有圖像。, 功能, , 輕鬆批次優化現有...。
    • WP TinyPNG 》WP TinyPNG採用TinyPNG API,在WordPress內部整潔地壓縮您的PNG圖像。。
    • Graphicsly – The ultimate graphics plugin for WordPress website builder ( Gutenberg, Elementor, Beaver Builder, WPBakery ) 》提供超過13,000個免費和專業的WordPress網站建立圖形資源,讓您的網站更上一層樓。, Graphicly 是您最喜愛的頁面建構器的全方位圖形解決方案,支援檔案(SVG...。
    • QR Code Management 》這是一個外掛,可為文章、固定頁面和自訂文章類型的公開頁面生成 QR 碼。QR 碼格式可以是 PNG、SVG、EPS、JPEG。。
    • WP pngfix 》這個外掛使用 CSS 技術來解決 png 圖片透明度的問題,再利用 jQuery 將此腳本套用到頁面中顯示的所有 png 圖片上。, 但這並不適用於定位和平鋪的 png 背景圖...。
    • ResizeFly 》這個外掛讓你只需要在需要的時候才建立圖片尺寸,而不是在上傳時就建立。, 通常,當你啟用一個新佈景主題或外掛,新增了新的圖片尺寸大小後,你必須使用像是 ...。
    • WebP Image Optimization 》總結:WebP 圖片優化通過將上傳的 JPEG 和 PNG 圖片轉換為更高效的 WebP 格式來增強您的網站性能。它還允許您調整圖像大小到指定的最大尺寸,以及調整圖像質...。
    • Png Compress 》壓縮 PNG 檔案, , 在媒體庫中添加 PNG 檔案時,可壓縮 PNG 檔案。, 可指定品質。, 可指定壓縮程度。, , 注意, 這對於使用 TinyPNG 壓縮的檔案無效。同樣地,...。

    文章
    Filter
    Apply Filters
    Mastodon