95429ce8 by Jeff Balicki

pdf thumb

Signed-off-by: Jeff <jeff@gotenzing.com>
1 parent da35d68c
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PDF Thumbnail Generator\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2022-07-07 21:13+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: \n"
"Language: \n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Loco https://localise.biz/\n"
"X-Loco-Version: 2.6.2; wp-6.0\n"
"X-Domain: pdf-thumbnail-generator"
#: pdf-thumbnail-generator.php:113
msgid "Back to the settings"
msgstr ""
#: pdf-thumbnail-generator.php:185
msgid "Generate missing PDF thumbnails"
msgstr ""
#: pdf-thumbnail-generator.php:180
msgid "Generate thumbnails for already uploaded PDFs"
msgstr ""
#: pdf-thumbnail-generator.php:111
#, php-format
msgid "Generated thumbnails: %d"
msgstr ""
#. Description of the plugin
msgid "Generates thumbnail for PDF files"
msgstr ""
#: pdf-thumbnail-generator.php:88
msgid "Generating PDF thumbnails..."
msgstr ""
#: pdf-thumbnail-generator.php:182
msgid "If you changed some settings, please save them firstly."
msgstr ""
#: pdf-thumbnail-generator.php:46
msgid ""
"Imagick is missing on your server. PDF Thumbnail Generator can not work "
"without it."
msgstr ""
#: pdf-thumbnail-generator.php:148
msgid "Max height"
msgstr ""
#: pdf-thumbnail-generator.php:140
msgid "Max width"
msgstr ""
#: pdf-thumbnail-generator.php:101
#, php-format
msgid "New thumbnail was generated for %d"
msgstr ""
#. Name of the plugin
msgid "PDF Thumbnail Generator"
msgstr ""
#: pdf-thumbnail-generator.php:58 pdf-thumbnail-generator.php:59
#: pdf-thumbnail-generator.php:132
msgid "PDF Thumbnails"
msgstr ""
#: pdf-thumbnail-generator.php:156
msgid "Quality"
msgstr ""
#: pdf-thumbnail-generator.php:189
msgid "Regenerate all PDF thumbnails"
msgstr ""
#: pdf-thumbnail-generator.php:177
msgid "Save"
msgstr ""
#: pdf-thumbnail-generator.php:68
msgid "Settings"
msgstr ""
#: pdf-thumbnail-generator.php:133
msgid "Settings saved."
msgstr ""
#: pdf-thumbnail-generator.php:103 pdf-thumbnail-generator.php:106
#, php-format
msgid "Thumbnail already exists for %d"
msgstr ""
#: pdf-thumbnail-generator.php:164
msgid "Type"
msgstr ""
<?php
/*
Plugin Name: PDF Thumbnail Generator
Plugin URI: https://wp-speedup.eu
Description: Generates thumbnail for PDF files
Version: 1.1
Author: KubiQ
Author URI: https://kubiq.sk
Text Domain: pdf-thumbnail-generator
Domain Path: /languages
*/
defined('ABSPATH') || exit;
if( ! class_exists('pdf_thumbnail_generator') ){
class pdf_thumbnail_generator{
var $settings;
function __construct(){
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
add_action( 'admin_menu', array( $this, 'plugin_menu_link' ) );
add_action( 'init', array( $this, 'plugin_init' ) );
add_action( 'add_attachment', array( $this, 'generate_thumbnail' ), 11, 1 );
add_action( 'delete_attachment', array( $this, 'delete' ) );
add_filter( 'wp_mime_type_icon', array( $this, 'wp_mime_type_icon' ), 10, 3 );
add_shortcode( 'pdf_thumbnail', function( $atts ){
if( is_admin() ) return true;
if( ! isset( $atts['id'] ) || ! intval( $atts['id'] ) ) return false;
return get_pdf_thumbnail_image( $atts['id'] );
});
add_shortcode( 'pdf_thumbnail_url', function( $atts ){
if( is_admin() ) return true;
if( ! isset( $atts['id'] ) || ! intval( $atts['id'] ) ) return false;
return $this->get_url( $atts['id'] );
});
}
function activate(){
if( ! extension_loaded('imagick') ){
esc_html_e( 'Imagick is missing on your server. PDF Thumbnail Generator can not work without it.', 'pdf-thumbnail-generator' );
exit;
}
}
function plugins_loaded(){
load_plugin_textdomain( 'pdf-thumbnail-generator', FALSE, basename( __DIR__ ) . '/languages/' );
}
function plugin_menu_link(){
add_submenu_page(
'options-general.php',
__( 'PDF Thumbnails', 'pdf-thumbnail-generator' ),
__( 'PDF Thumbnails', 'pdf-thumbnail-generator' ),
'manage_options',
basename( __FILE__ ),
array( $this, 'admin_options_page' )
);
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'filter_plugin_actions' ), 10, 2 );
}
function filter_plugin_actions( $links, $file ){
array_unshift( $links, '<a href="options-general.php?page=' . basename( __FILE__ ) . '">' . __( 'Settings', 'pdf-thumbnail-generator' ) . '</a>' );
return $links;
}
function plugin_init(){
$this->settings = array_merge(
array(
'max_width' => 1024,
'max_height' => 1024,
'quality' => 80,
'type' => 'png',
),
get_option( 'pdf_thumbnail_generator_settings', array() )
);
}
function admin_options_page(){
global $wpdb;
if( isset( $_GET['generate'] ) ){ ?>
<div class="wrap">
<h2><?php _e( 'Generating PDF thumbnails...', 'pdf-thumbnail-generator' ) ?></h2>
<div id="pdf-list"><?php
$generated_thumbs = 0;
$pdfs = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_type = 'attachment' AND {$wpdb->posts}.post_mime_type = 'application/pdf'");
if( $pdfs ){
foreach( $pdfs as $pdf ){
$regenerate = $_GET['generate'] == 'all' ? true : false;
$thumbnail = get_post_meta( $pdf, '_pdf_thumbnail', true );
if( ! $thumbnail || $regenerate ){
$generated = $this->generate_thumbnail( $pdf, $regenerate );
$thumbnail = get_post_meta( $pdf, '_pdf_thumbnail', true );
if( $thumbnail && $generated ){
$generated_thumbs++;
echo '<div>' . sprintf( esc_html__( 'New thumbnail was generated for %d', 'pdf-thumbnail-generator' ), $pdf ) . '</div>';
}else{
echo '<div>' . sprintf( esc_html__( 'Thumbnail already exists for %d', 'pdf-thumbnail-generator' ), $pdf ) . '</div>';
}
}else{
echo '<div>' . sprintf( esc_html__( 'Thumbnail already exists for %d', 'pdf-thumbnail-generator' ), $pdf ) . '</div>';
}
}
}
echo '<div>' . sprintf( esc_html__( 'Generated thumbnails: %d', 'pdf-thumbnail-generator' ), $generated_thumbs ) . '</div>';
echo '<br><a href="' . remove_query_arg('generate') . '" class="button button-primary">' . __( 'Back to the settings', 'pdf-thumbnail-generator' ) . '</a>'; ?>
</div>
</div><?php
}else{
$show_update_notice = false;
if( isset( $_POST['plugin_sent'] ) ){
if( check_admin_referer( 'save_these_settings', 'settings_nonce' ) ){
$this->settings = array();
$this->settings['max_width'] = intval( $_POST['max_width'] );
$this->settings['max_height'] = intval( $_POST['max_height'] );
$this->settings['quality'] = intval( $_POST['quality'] );
$this->settings['type'] = $_POST['type'] == 'jpg' ? 'jpg' : 'png';
update_option( 'pdf_thumbnail_generator_settings', $this->settings );
$show_update_notice = true;
}
} ?>
<div class="wrap">
<h2><?php _e( 'PDF Thumbnails', 'pdf-thumbnail-generator' ) ?></h2>
<?php if( $show_update_notice ) echo '<div class="below-h2 updated"><p>' . __( 'Settings saved.', 'pdf-thumbnail-generator' ) . '</p></div>'; ?>
<form method="post" action="<?php echo admin_url( 'options-general.php?page=' . basename( __FILE__ ) ) ?>">
<input type="hidden" name="plugin_sent" value="1">
<?php wp_nonce_field( 'save_these_settings', 'settings_nonce' ) ?>
<table class="form-table">
<tr>
<th>
<label for="q_field_1"><?php _e( 'Max width', 'pdf-thumbnail-generator' ) ?></label>
</th>
<td>
<input type="number" name="max_width" value="<?php echo intval( $this->settings['max_width'] ) ?>" id="q_field_1"> px
</td>
</tr>
<tr>
<th>
<label for="q_field_2"><?php _e( 'Max height', 'pdf-thumbnail-generator' ) ?></label>
</th>
<td>
<input type="number" name="max_height" value="<?php echo intval( $this->settings['max_height'] ) ?>" id="q_field_2"> px
</td>
</tr>
<tr>
<th>
<label for="q_field_3"><?php _e( 'Quality', 'pdf-thumbnail-generator' ) ?></label>
</th>
<td>
<input type="number" min="1" max="100" name="quality" value="<?php echo intval( $this->settings['quality'] ) ?>" id="q_field_3"> %&emsp;<small>(1-100)</small>
</td>
</tr>
<tr>
<th>
<label><?php _e( 'Type', 'pdf-thumbnail-generator' ) ?></label>
</th>
<td>
<label>
<input type="radio" name="type" value="png" <?php echo $this->settings['type'] == 'png' ? 'checked' : '' ?>> png
</label>
&emsp;
<label>
<input type="radio" name="type" value="jpg" <?php echo $this->settings['type'] == 'jpg' ? 'checked' : '' ?>> jpg
</label>
</td>
</tr>
</table>
<p class="submit"><input type="submit" class="button button-primary button-large" value="<?php _e( 'Save', 'pdf-thumbnail-generator' ) ?>"></p>
</form>
<h3><?php esc_html_e( 'Generate thumbnails for already uploaded PDFs', 'pdf-thumbnail-generator' ) ?></h3>
<p><?php esc_html_e( 'If you changed some settings, please save them firstly.', 'pdf-thumbnail-generator' ) ?></p>
<a href="<?php echo add_query_arg( 'generate', 'missing' ) ?>" class="button button-primary">
<?php esc_html_e( 'Generate missing PDF thumbnails', 'pdf-thumbnail-generator' ) ?>
</a>
&emsp;
<a href="<?php echo add_query_arg( 'generate', 'all' ) ?>" class="button button-primary">
<?php esc_html_e( 'Regenerate all PDF thumbnails', 'pdf-thumbnail-generator' ) ?>
</a>
</div><?php
}
}
function generate_thumbnail( $pdf_id, $regenerate = false ){
if( get_post_mime_type( $pdf_id ) === 'application/pdf' ){
$thumbnail = get_post_meta( $pdf_id, '_pdf_thumbnail', true );
if( $thumbnail ){
if( $regenerate ){
$this->delete( $pdf_id );
}else{
return false;
}
}
set_time_limit( 0 );
$max_width = apply_filters( 'pdf_thumbnail_max_width', $this->settings['max_width'], $pdf_id );
$max_width = intval( $max_width );
$max_height = apply_filters( 'pdf_thumbnail_max_height', $this->settings['max_height'], $pdf_id );
$max_height = intval( $max_height );
$quality = apply_filters( 'pdf_thumbnail_quality', $this->settings['quality'], $pdf_id );
$quality = intval( $quality );
$type = apply_filters( 'pdf_thumbnail_type', $this->settings['type'], $pdf_id );
$type = $type == 'jpg' ? 'jpg' : 'png';
$page_number = apply_filters( 'pdf_thumbnail_page_number', 0, $pdf_id );
$page_number = intval( $page_number );
$resolution = ceil( max( $max_height, $max_width ) * 0.16 );
$bgcolor = apply_filters( 'pdf_thumbnail_bgcolor', 'white', $pdf_id );
$filepath = get_attached_file( $pdf_id );
$new_filename = sanitize_file_name( basename( $filepath ) . '.' . $type );
$new_filename = wp_unique_filename( dirname( $filepath ), $new_filename );
$new_filename = apply_filters( 'pdf_thumbnail_filename', $new_filename, $pdf_id );
$new_filepath = str_replace( basename( $filepath ), $new_filename, $filepath );
try{
$imagick = new Imagick();
$imagick->setResolution( $resolution, $resolution );
$imagick->readimage( $filepath . '[' . $page_number . ']' );
$imagick->setCompressionQuality( $quality );
$imagick->scaleImage( $max_width, $max_height, true );
$imagick->setImageFormat( $type );
$imagick->setImageBackgroundColor( $bgcolor );
if( method_exists( 'Imagick', 'setImageAlphaChannel' ) ){
if( defined('Imagick::ALPHACHANNEL_REMOVE') ){
$imagick->setImageAlphaChannel( Imagick::ALPHACHANNEL_REMOVE );
}else{
$imagick->setImageAlphaChannel( 11 );
}
}
if( method_exists( 'Imagick','mergeImageLayers' ) ){
$imagick->mergeImageLayers( Imagick::LAYERMETHOD_FLATTEN );
}else{
$imagick = $imagick->flattenImages();
}
$imagick = apply_filters( 'pdf_thumbnail_imagick', $imagick, $pdf_id );
$imagick->stripImage();
$imagick->writeImage( $new_filepath );
$imagick->clear();
update_post_meta( $pdf_id, '_pdf_thumbnail', $new_filename );
do_action( 'pdf_thumbnail_generated', $new_filepath, $pdf_id );
}catch( ImagickException $err ){
error_log( $err );
}catch( Exception $err ){
error_log( $err );
}
return true;
}
}
function wp_mime_type_icon( $icon, $mime, $pdf_id ){
if( $mime === 'application/pdf' && strpos( $_SERVER['REQUEST_URI'], '/wp-admin/upload.php' ) === false ){
$thumbnail_url = $this->get_url( $pdf_id );
if( $thumbnail_url ){
return $thumbnail_url;
}
}
return $icon;
}
function delete( $pdf_id ){
if( get_post_mime_type( $pdf_id ) === 'application/pdf' ){
$thumbnail_filepath = $this->get_path( $pdf_id );
if( $thumbnail_filepath ){
unlink( $thumbnail_filepath );
}
}
}
function get_path( $pdf_id ){
if( get_post_mime_type( $pdf_id ) === 'application/pdf' ){
$thumbnail = get_post_meta( $pdf_id, '_pdf_thumbnail', true );
if( $thumbnail ){
$filepath = get_attached_file( $pdf_id );
$thumbnail_filepath = str_replace( basename( $filepath ), $thumbnail, $filepath );
if( file_exists( $thumbnail_filepath ) ){
return $thumbnail_filepath;
}
}
}
return false;
}
function get_url( $pdf_id ){
if( get_post_mime_type( $pdf_id ) === 'application/pdf' ){
$thumbnail = get_post_meta( $pdf_id, '_pdf_thumbnail', true );
if( $thumbnail ){
$filepath = get_attached_file( $pdf_id );
$thumbnail_filepath = str_replace( basename( $filepath ), $thumbnail, $filepath );
if( file_exists( $thumbnail_filepath ) ){
$thumbnail_url = wp_get_attachment_url( $pdf_id );
return str_replace( basename( $filepath ), $thumbnail, $thumbnail_url );
}
}
}
return false;
}
}
$pdf_thumbnail = new pdf_thumbnail_generator();
register_activation_hook( __FILE__, array( $pdf_thumbnail, 'activate' ) );
if( ! function_exists('get_pdf_thumbnail_url') ){
function get_pdf_thumbnail_url( $pdf_id ){
global $pdf_thumbnail;
return $pdf_thumbnail->get_url( $pdf_id );
}
}
if( ! function_exists('get_pdf_thumbnail_path') ){
function get_pdf_thumbnail_path( $pdf_id ){
global $pdf_thumbnail;
return $pdf_thumbnail->get_path( $pdf_id );
}
}
if( ! function_exists('get_pdf_thumbnail_image_src') ){
function get_pdf_thumbnail_image_src( $pdf_id ){
global $pdf_thumbnail;
$data = false;
$thumbnail_url = $pdf_thumbnail->get_url( $pdf_id );
if( $thumbnail_url ){
$thumbnail_path = $pdf_thumbnail->get_path( $pdf_id );
$info = getimagesize( $thumbnail_path );
$data = array( $thumbnail_url, $info[0], $info[1] );
}
return $data;
}
}
if( ! function_exists('get_pdf_thumbnail_image') ){
function get_pdf_thumbnail_image( $pdf_id ){
global $pdf_thumbnail;
$html = '';
$thumbnail_url = $pdf_thumbnail->get_url( $pdf_id );
if( $thumbnail_url ){
$thumbnail_path = $pdf_thumbnail->get_path( $pdf_id );
$info = getimagesize( $thumbnail_path );
$default_attr = array(
'src' => $thumbnail_url,
'class' => 'pdf-thumbnail',
'alt' => esc_attr( trim( get_the_title( $pdf_id ) ) ),
'width' => $info[0],
'height' => $info[1],
);
if( wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' ) ){
$default_attr['loading'] = wp_get_loading_attr_default('wp_get_attachment_image');
}
if( array_key_exists( 'loading', $default_attr ) && ! $default_attr['loading'] ){
unset( $default_attr['loading'] );
}
$attr = apply_filters( 'get_pdf_thumbnail_image_attributes', $default_attr, $pdf_id );
$attr = array_map( 'esc_attr', $attr );
$html = '<img';
foreach( $attr as $name => $value ){
$html .= ' ' . $name . '="' . $value . '"';
}
$html .= '>';
}
return $html;
}
}
}
\ No newline at end of file
=== PDF Thumbnail Generator ===
Contributors: kubiq
Donate link: https://www.paypal.me/jakubnovaksl
Tags: pdf, image, thumbnail, generator, creator
Requires at least: 3.0.1
Requires PHP: 5.6
Tested up to: 6.4
Stable tag: 1.1
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Generates thumbnail for PDF files
== Description ==
Generates thumbnail for PDF file automatically after file is uploaded to the Media library.
You can also generate thumbnails for old PDF files that are already in the Media library - you can generate missing thumbnails or regenerate all thumbnails.
<strong>Imagick library must be installed on your server, otherwise this plugin will not work</strong>
<ul>
<li>automated test after plugin activation to make sure it will work on your server</li>
<li>works with all types of WordPress installations: domain, subdomain, subdirectory, multisite/network</li>
<li>works on Apache and NGiNX</li>
<li>automatically generate thumbnail for new uploaded PDFs</li>
<li>(re)generate thumbnails for existing PDFs in Media library</li>
<li>set maximum width of PDF thumbnail</li>
<li>set maximum height of PDF thumbnail</li>
<li>set image quality of PDF thumbnail</li>
<li>set image file type of PDF thumbnail</li>
</ul>
## Shortcodes
### pdf_thumbnail
Maybe you want to display PDF thumbnail by using a shortcode
`[pdf_thumbnail id="123"]`
### pdf_thumbnail_url
Maybe you want to display PDF thumbnail url by using a shortcode
`[pdf_thumbnail_url id="123"]`
 
## Functions
### get_pdf_thumbnail_url
If you want to return PDF thumbnail URL you can use
`get_pdf_thumbnail_url( $pdf_id )`
it works similar to `wp_get_attachment_url` and it will return something like
`https://site.com/wp-content/uploads/2022/01/example.pdf.png`
### get_pdf_thumbnail_path
If you want to return PDF thumbnail URL you can use
`get_pdf_thumbnail_path( $pdf_id )`
it works similar to `get_attached_file` and it will return something like
`/www/site.com/wp-content/uploads/2022/01/example.pdf.png`
### get_pdf_thumbnail_image_src
If you want to return PDF thumbnail url, width and height you can use
`get_pdf_thumbnail_image_src( $pdf_id )`
it works similar to `wp_get_attachment_image_src` and it will return something like
`[
0 => 'https://site.com/wp-content/uploads/2022/01/example.pdf.png',
1 => 600,
2 => 800
]`
### get_pdf_thumbnail_image
If you want to return PDF thumbnail image tag you can use
`get_pdf_thumbnail_image( $pdf_id )`
it works similar to `wp_get_attachment_image` and it will return something like
`<img src="https://site.com/wp-content/uploads/2022/01/example.pdf.png" width="600" height="800" alt="example" loading="lazy">`
 
## Hooks
### pdf_thumbnail_max_width
Maybe you want to change global PDF thumbnail max_width for a specific PDF file
`add_filter( 'pdf_thumbnail_max_width', function( $max_width, $pdf_id ){
if( $pdf_id == 123 ){
return 1024;
}
return $max_width;
}, 10, 2 );`
### pdf_thumbnail_max_height
Maybe you want to change global PDF thumbnail max_width for a specific PDF file
`add_filter( 'pdf_thumbnail_max_height', function( $max_height, $pdf_id ){
if( $pdf_id == 123 ){
return 768;
}
return $max_height;
}, 10, 2 );`
### pdf_thumbnail_quality
Maybe you want to change global PDF thumbnail quality for a specific PDF file
`add_filter( 'pdf_thumbnail_quality', function( $quality, $pdf_id ){
if( $pdf_id == 123 ){
return 100;
}
return $quality;
}, 10, 2 );`
### pdf_thumbnail_type
Maybe you want to change global PDF thumbnail file type for a specific PDF file
`add_filter( 'pdf_thumbnail_type', function( $type, $pdf_id ){
if( $pdf_id == 123 ){
return 'png'; // or 'jpg'
}
return $type;
}, 10, 2 );`
### pdf_thumbnail_bgcolor
Maybe you want to change default PDF thumbnail background for a specific PDF file
`add_filter( 'pdf_thumbnail_bgcolor', function( $bgcolor, $pdf_id ){
if( $pdf_id == 123 ){
return 'black'; // default is 'white'
}
return $bgcolor;
}, 10, 2 );`
### pdf_thumbnail_page_number
Maybe you want to PDF thumbnail page number for a specific PDF file
`add_filter( 'pdf_thumbnail_page_number', function( $page, $pdf_id ){
if( $pdf_id == 123 ){
return 1; // default is 0
}
return $page;
}, 10, 2 );`
### pdf_thumbnail_filename
Maybe you want to PDF thumbnail filename for a specific PDF file
`add_filter( 'pdf_thumbnail_filename', function( $filename, $pdf_id ){
if( $pdf_id == 123 ){
return str_replace( '.pdf.png', '.png', $filename );
}
return $filename;
}, 10, 2 );`
### pdf_thumbnail_imagick
Maybe you want to add watermark to PDF thumbnail for a specific PDF file
`add_filter( 'pdf_thumbnail_imagick', function( $imagick, $pdf_id ){
if( $pdf_id == 123 ){
// add your watermark here
}
return $imagick;
}, 10, 2 );`
### get_pdf_thumbnail_image_attributes
Maybe you want to change attributes for image tag from `get_pdf_thumbnail_image` function
`add_filter( 'get_pdf_thumbnail_image_attributes', function( $attr, $pdf_id ){
$attr['loading'] = 'eager';
return $attr;
}, 10, 2 );`
### pdf_thumbnail_generated
Maybe you want to do something after the thumbnail is generated
`add_action( 'pdf_thumbnail_generated', function( $thumbnail_path, $pdf_id ){
// do somthing with the local file $thumbnail_path
}, 10, 2 );`
== Installation ==
1. Upload `pdf-thumbnail-generator` directory to the `/wp-content/plugins/` directory
2. Activate the plugin through the 'Plugins' menu in WordPress
== Frequently Asked Questions ==
= Plugin requirements =
PHP 5.6 or higher
Imagick extension
= PDF thumbnails stored location =
PDF thumbnails are generated in the same directory as original PDF file. Example:
pdf file: `/wp-content/uploads/2022/01/example.pdf`
thumbnail: `/wp-content/uploads/2022/01/example.pdf.png`
== Changelog ==
= 1.1 =
* tested on WP 6.4
= 1.0 =
* First version
\ No newline at end of file