pdf-thumbnail-generator.php
13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?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 = true;
$thumbnail;
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"> % <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>
 
<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>
 
<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' ){
$regenerate = true;
$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;
}
}
}