qa
Signed-off-by: Jeff <jeff@gotenzing.com>
Showing
3 changed files
with
704 additions
and
0 deletions
| 1 | #, fuzzy | ||
| 2 | msgid "" | ||
| 3 | msgstr "" | ||
| 4 | "Project-Id-Version: PDF Thumbnail Generator\n" | ||
| 5 | "Report-Msgid-Bugs-To: \n" | ||
| 6 | "POT-Creation-Date: 2022-07-07 21:13+0000\n" | ||
| 7 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | ||
| 8 | "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | ||
| 9 | "Language-Team: \n" | ||
| 10 | "Language: \n" | ||
| 11 | "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" | ||
| 12 | "MIME-Version: 1.0\n" | ||
| 13 | "Content-Type: text/plain; charset=UTF-8\n" | ||
| 14 | "Content-Transfer-Encoding: 8bit\n" | ||
| 15 | "X-Generator: Loco https://localise.biz/\n" | ||
| 16 | "X-Loco-Version: 2.6.2; wp-6.0\n" | ||
| 17 | "X-Domain: pdf-thumbnail-generator" | ||
| 18 | |||
| 19 | #: pdf-thumbnail-generator.php:113 | ||
| 20 | msgid "Back to the settings" | ||
| 21 | msgstr "" | ||
| 22 | |||
| 23 | #: pdf-thumbnail-generator.php:185 | ||
| 24 | msgid "Generate missing PDF thumbnails" | ||
| 25 | msgstr "" | ||
| 26 | |||
| 27 | #: pdf-thumbnail-generator.php:180 | ||
| 28 | msgid "Generate thumbnails for already uploaded PDFs" | ||
| 29 | msgstr "" | ||
| 30 | |||
| 31 | #: pdf-thumbnail-generator.php:111 | ||
| 32 | #, php-format | ||
| 33 | msgid "Generated thumbnails: %d" | ||
| 34 | msgstr "" | ||
| 35 | |||
| 36 | #. Description of the plugin | ||
| 37 | msgid "Generates thumbnail for PDF files" | ||
| 38 | msgstr "" | ||
| 39 | |||
| 40 | #: pdf-thumbnail-generator.php:88 | ||
| 41 | msgid "Generating PDF thumbnails..." | ||
| 42 | msgstr "" | ||
| 43 | |||
| 44 | #: pdf-thumbnail-generator.php:182 | ||
| 45 | msgid "If you changed some settings, please save them firstly." | ||
| 46 | msgstr "" | ||
| 47 | |||
| 48 | #: pdf-thumbnail-generator.php:46 | ||
| 49 | msgid "" | ||
| 50 | "Imagick is missing on your server. PDF Thumbnail Generator can not work " | ||
| 51 | "without it." | ||
| 52 | msgstr "" | ||
| 53 | |||
| 54 | #: pdf-thumbnail-generator.php:148 | ||
| 55 | msgid "Max height" | ||
| 56 | msgstr "" | ||
| 57 | |||
| 58 | #: pdf-thumbnail-generator.php:140 | ||
| 59 | msgid "Max width" | ||
| 60 | msgstr "" | ||
| 61 | |||
| 62 | #: pdf-thumbnail-generator.php:101 | ||
| 63 | #, php-format | ||
| 64 | msgid "New thumbnail was generated for %d" | ||
| 65 | msgstr "" | ||
| 66 | |||
| 67 | #. Name of the plugin | ||
| 68 | msgid "PDF Thumbnail Generator" | ||
| 69 | msgstr "" | ||
| 70 | |||
| 71 | #: pdf-thumbnail-generator.php:58 pdf-thumbnail-generator.php:59 | ||
| 72 | #: pdf-thumbnail-generator.php:132 | ||
| 73 | msgid "PDF Thumbnails" | ||
| 74 | msgstr "" | ||
| 75 | |||
| 76 | #: pdf-thumbnail-generator.php:156 | ||
| 77 | msgid "Quality" | ||
| 78 | msgstr "" | ||
| 79 | |||
| 80 | #: pdf-thumbnail-generator.php:189 | ||
| 81 | msgid "Regenerate all PDF thumbnails" | ||
| 82 | msgstr "" | ||
| 83 | |||
| 84 | #: pdf-thumbnail-generator.php:177 | ||
| 85 | msgid "Save" | ||
| 86 | msgstr "" | ||
| 87 | |||
| 88 | #: pdf-thumbnail-generator.php:68 | ||
| 89 | msgid "Settings" | ||
| 90 | msgstr "" | ||
| 91 | |||
| 92 | #: pdf-thumbnail-generator.php:133 | ||
| 93 | msgid "Settings saved." | ||
| 94 | msgstr "" | ||
| 95 | |||
| 96 | #: pdf-thumbnail-generator.php:103 pdf-thumbnail-generator.php:106 | ||
| 97 | #, php-format | ||
| 98 | msgid "Thumbnail already exists for %d" | ||
| 99 | msgstr "" | ||
| 100 | |||
| 101 | #: pdf-thumbnail-generator.php:164 | ||
| 102 | msgid "Type" | ||
| 103 | msgstr "" |
| 1 | <?php | ||
| 2 | /* | ||
| 3 | Plugin Name: PDF Thumbnail Generator | ||
| 4 | Plugin URI: https://wp-speedup.eu | ||
| 5 | Description: Generates thumbnail for PDF files | ||
| 6 | Version: 1.1 | ||
| 7 | Author: KubiQ | ||
| 8 | Author URI: https://kubiq.sk | ||
| 9 | Text Domain: pdf-thumbnail-generator | ||
| 10 | Domain Path: /languages | ||
| 11 | */ | ||
| 12 | |||
| 13 | defined('ABSPATH') || exit; | ||
| 14 | |||
| 15 | if( ! class_exists('pdf_thumbnail_generator') ){ | ||
| 16 | class pdf_thumbnail_generator{ | ||
| 17 | var $settings; | ||
| 18 | |||
| 19 | function __construct(){ | ||
| 20 | add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) ); | ||
| 21 | add_action( 'admin_menu', array( $this, 'plugin_menu_link' ) ); | ||
| 22 | add_action( 'init', array( $this, 'plugin_init' ) ); | ||
| 23 | add_action( 'add_attachment', array( $this, 'generate_thumbnail' ), 11, 1 ); | ||
| 24 | add_action( 'delete_attachment', array( $this, 'delete' ) ); | ||
| 25 | add_filter( 'wp_mime_type_icon', array( $this, 'wp_mime_type_icon' ), 10, 3 ); | ||
| 26 | |||
| 27 | add_shortcode( 'pdf_thumbnail', function( $atts ){ | ||
| 28 | if( is_admin() ) return true; | ||
| 29 | |||
| 30 | if( ! isset( $atts['id'] ) || ! intval( $atts['id'] ) ) return false; | ||
| 31 | |||
| 32 | return get_pdf_thumbnail_image( $atts['id'] ); | ||
| 33 | }); | ||
| 34 | |||
| 35 | add_shortcode( 'pdf_thumbnail_url', function( $atts ){ | ||
| 36 | if( is_admin() ) return true; | ||
| 37 | |||
| 38 | if( ! isset( $atts['id'] ) || ! intval( $atts['id'] ) ) return false; | ||
| 39 | |||
| 40 | return $this->get_url( $atts['id'] ); | ||
| 41 | }); | ||
| 42 | } | ||
| 43 | |||
| 44 | function activate(){ | ||
| 45 | if( ! extension_loaded('imagick') ){ | ||
| 46 | esc_html_e( 'Imagick is missing on your server. PDF Thumbnail Generator can not work without it.', 'pdf-thumbnail-generator' ); | ||
| 47 | exit; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | function plugins_loaded(){ | ||
| 52 | load_plugin_textdomain( 'pdf-thumbnail-generator', FALSE, basename( __DIR__ ) . '/languages/' ); | ||
| 53 | } | ||
| 54 | |||
| 55 | function plugin_menu_link(){ | ||
| 56 | add_submenu_page( | ||
| 57 | 'options-general.php', | ||
| 58 | __( 'PDF Thumbnails', 'pdf-thumbnail-generator' ), | ||
| 59 | __( 'PDF Thumbnails', 'pdf-thumbnail-generator' ), | ||
| 60 | 'manage_options', | ||
| 61 | basename( __FILE__ ), | ||
| 62 | array( $this, 'admin_options_page' ) | ||
| 63 | ); | ||
| 64 | add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'filter_plugin_actions' ), 10, 2 ); | ||
| 65 | } | ||
| 66 | |||
| 67 | function filter_plugin_actions( $links, $file ){ | ||
| 68 | array_unshift( $links, '<a href="options-general.php?page=' . basename( __FILE__ ) . '">' . __( 'Settings', 'pdf-thumbnail-generator' ) . '</a>' ); | ||
| 69 | return $links; | ||
| 70 | } | ||
| 71 | |||
| 72 | function plugin_init(){ | ||
| 73 | $this->settings = array_merge( | ||
| 74 | array( | ||
| 75 | 'max_width' => 1024, | ||
| 76 | 'max_height' => 1024, | ||
| 77 | 'quality' => 80, | ||
| 78 | 'type' => 'png', | ||
| 79 | ), | ||
| 80 | get_option( 'pdf_thumbnail_generator_settings', array() ) | ||
| 81 | ); | ||
| 82 | } | ||
| 83 | |||
| 84 | function admin_options_page(){ | ||
| 85 | global $wpdb; | ||
| 86 | if( isset( $_GET['generate'] ) ){ ?> | ||
| 87 | <div class="wrap"> | ||
| 88 | <h2><?php _e( 'Generating PDF thumbnails...', 'pdf-thumbnail-generator' ) ?></h2> | ||
| 89 | <div id="pdf-list"><?php | ||
| 90 | $generated_thumbs = 0; | ||
| 91 | $pdfs = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_type = 'attachment' AND {$wpdb->posts}.post_mime_type = 'application/pdf'"); | ||
| 92 | if( $pdfs ){ | ||
| 93 | foreach( $pdfs as $pdf ){ | ||
| 94 | $regenerate = true; | ||
| 95 | $thumbnail; | ||
| 96 | if( ! $thumbnail || $regenerate ){ | ||
| 97 | $generated = $this->generate_thumbnail( $pdf, $regenerate ); | ||
| 98 | $thumbnail = get_post_meta( $pdf, '_pdf_thumbnail', true ); | ||
| 99 | if( $thumbnail && $generated ){ | ||
| 100 | $generated_thumbs++; | ||
| 101 | echo '<div>' . sprintf( esc_html__( 'New thumbnail was generated for %d', 'pdf-thumbnail-generator' ), $pdf ) . '</div>'; | ||
| 102 | }else{ | ||
| 103 | echo '<div>' . sprintf( esc_html__( 'Thumbnail already exists for %d', 'pdf-thumbnail-generator' ), $pdf ) . '</div>'; | ||
| 104 | } | ||
| 105 | }else{ | ||
| 106 | echo '<div>' . sprintf( esc_html__( 'Thumbnail already exists for %d', 'pdf-thumbnail-generator' ), $pdf ) . '</div>'; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | echo '<div>' . sprintf( esc_html__( 'Generated thumbnails: %d', 'pdf-thumbnail-generator' ), $generated_thumbs ) . '</div>'; | ||
| 112 | |||
| 113 | echo '<br><a href="' . remove_query_arg('generate') . '" class="button button-primary">' . __( 'Back to the settings', 'pdf-thumbnail-generator' ) . '</a>'; ?> | ||
| 114 | </div> | ||
| 115 | </div><?php | ||
| 116 | }else{ | ||
| 117 | $show_update_notice = false; | ||
| 118 | if( isset( $_POST['plugin_sent'] ) ){ | ||
| 119 | if( check_admin_referer( 'save_these_settings', 'settings_nonce' ) ){ | ||
| 120 | |||
| 121 | $this->settings = array(); | ||
| 122 | $this->settings['max_width'] = intval( $_POST['max_width'] ); | ||
| 123 | $this->settings['max_height'] = intval( $_POST['max_height'] ); | ||
| 124 | $this->settings['quality'] = intval( $_POST['quality'] ); | ||
| 125 | $this->settings['type'] = $_POST['type'] == 'jpg' ? 'jpg' : 'png'; | ||
| 126 | |||
| 127 | update_option( 'pdf_thumbnail_generator_settings', $this->settings ); | ||
| 128 | $show_update_notice = true; | ||
| 129 | } | ||
| 130 | } ?> | ||
| 131 | <div class="wrap"> | ||
| 132 | <h2><?php _e( 'PDF Thumbnails', 'pdf-thumbnail-generator' ) ?></h2> | ||
| 133 | <?php if( $show_update_notice ) echo '<div class="below-h2 updated"><p>' . __( 'Settings saved.', 'pdf-thumbnail-generator' ) . '</p></div>'; ?> | ||
| 134 | <form method="post" action="<?php echo admin_url( 'options-general.php?page=' . basename( __FILE__ ) ) ?>"> | ||
| 135 | <input type="hidden" name="plugin_sent" value="1"> | ||
| 136 | <?php wp_nonce_field( 'save_these_settings', 'settings_nonce' ) ?> | ||
| 137 | <table class="form-table"> | ||
| 138 | <tr> | ||
| 139 | <th> | ||
| 140 | <label for="q_field_1"><?php _e( 'Max width', 'pdf-thumbnail-generator' ) ?></label> | ||
| 141 | </th> | ||
| 142 | <td> | ||
| 143 | <input type="number" name="max_width" value="<?php echo intval( $this->settings['max_width'] ) ?>" id="q_field_1"> px | ||
| 144 | </td> | ||
| 145 | </tr> | ||
| 146 | <tr> | ||
| 147 | <th> | ||
| 148 | <label for="q_field_2"><?php _e( 'Max height', 'pdf-thumbnail-generator' ) ?></label> | ||
| 149 | </th> | ||
| 150 | <td> | ||
| 151 | <input type="number" name="max_height" value="<?php echo intval( $this->settings['max_height'] ) ?>" id="q_field_2"> px | ||
| 152 | </td> | ||
| 153 | </tr> | ||
| 154 | <tr> | ||
| 155 | <th> | ||
| 156 | <label for="q_field_3"><?php _e( 'Quality', 'pdf-thumbnail-generator' ) ?></label> | ||
| 157 | </th> | ||
| 158 | <td> | ||
| 159 | <input type="number" min="1" max="100" name="quality" value="<?php echo intval( $this->settings['quality'] ) ?>" id="q_field_3"> % <small>(1-100)</small> | ||
| 160 | </td> | ||
| 161 | </tr> | ||
| 162 | <tr> | ||
| 163 | <th> | ||
| 164 | <label><?php _e( 'Type', 'pdf-thumbnail-generator' ) ?></label> | ||
| 165 | </th> | ||
| 166 | <td> | ||
| 167 | <label> | ||
| 168 | <input type="radio" name="type" value="png" <?php echo $this->settings['type'] == 'png' ? 'checked' : '' ?>> png | ||
| 169 | </label> | ||
| 170 |   | ||
| 171 | <label> | ||
| 172 | <input type="radio" name="type" value="jpg" <?php echo $this->settings['type'] == 'jpg' ? 'checked' : '' ?>> jpg | ||
| 173 | </label> | ||
| 174 | </td> | ||
| 175 | </tr> | ||
| 176 | </table> | ||
| 177 | <p class="submit"><input type="submit" class="button button-primary button-large" value="<?php _e( 'Save', 'pdf-thumbnail-generator' ) ?>"></p> | ||
| 178 | </form> | ||
| 179 | |||
| 180 | <h3><?php esc_html_e( 'Generate thumbnails for already uploaded PDFs', 'pdf-thumbnail-generator' ) ?></h3> | ||
| 181 | |||
| 182 | <p><?php esc_html_e( 'If you changed some settings, please save them firstly.', 'pdf-thumbnail-generator' ) ?></p> | ||
| 183 | |||
| 184 | <a href="<?php echo add_query_arg( 'generate', 'missing' ) ?>" class="button button-primary"> | ||
| 185 | <?php esc_html_e( 'Generate missing PDF thumbnails', 'pdf-thumbnail-generator' ) ?> | ||
| 186 | </a> | ||
| 187 |   | ||
| 188 | <a href="<?php echo add_query_arg( 'generate', 'all' ) ?>" class="button button-primary"> | ||
| 189 | <?php esc_html_e( 'Regenerate all PDF thumbnails', 'pdf-thumbnail-generator' ) ?> | ||
| 190 | </a> | ||
| 191 | </div><?php | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | function generate_thumbnail( $pdf_id, $regenerate = false ){ | ||
| 196 | if( get_post_mime_type( $pdf_id ) === 'application/pdf' ){ | ||
| 197 | $regenerate = true; | ||
| 198 | $thumbnail = get_post_meta( $pdf_id, '_pdf_thumbnail', true ); | ||
| 199 | if( $thumbnail ){ | ||
| 200 | if( $regenerate ){ | ||
| 201 | $this->delete( $pdf_id ); | ||
| 202 | }else{ | ||
| 203 | return false; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | set_time_limit( 0 ); | ||
| 208 | |||
| 209 | $max_width = apply_filters( 'pdf_thumbnail_max_width', $this->settings['max_width'], $pdf_id ); | ||
| 210 | $max_width = intval( $max_width ); | ||
| 211 | $max_height = apply_filters( 'pdf_thumbnail_max_height', $this->settings['max_height'], $pdf_id ); | ||
| 212 | $max_height = intval( $max_height ); | ||
| 213 | $quality = apply_filters( 'pdf_thumbnail_quality', $this->settings['quality'], $pdf_id ); | ||
| 214 | $quality = intval( $quality ); | ||
| 215 | $type = apply_filters( 'pdf_thumbnail_type', $this->settings['type'], $pdf_id ); | ||
| 216 | $type = $type == 'jpg' ? 'jpg' : 'png'; | ||
| 217 | |||
| 218 | $page_number = apply_filters( 'pdf_thumbnail_page_number', 0, $pdf_id ); | ||
| 219 | $page_number = intval( $page_number ); | ||
| 220 | |||
| 221 | $resolution = ceil( max( $max_height, $max_width ) * 0.16 ); | ||
| 222 | |||
| 223 | $bgcolor = apply_filters( 'pdf_thumbnail_bgcolor', 'white', $pdf_id ); | ||
| 224 | |||
| 225 | $filepath = get_attached_file( $pdf_id ); | ||
| 226 | |||
| 227 | $new_filename = sanitize_file_name( basename( $filepath ) . '.' . $type ); | ||
| 228 | $new_filename = wp_unique_filename( dirname( $filepath ), $new_filename ); | ||
| 229 | $new_filename = apply_filters( 'pdf_thumbnail_filename', $new_filename, $pdf_id ); | ||
| 230 | |||
| 231 | $new_filepath = str_replace( basename( $filepath ), $new_filename, $filepath ); | ||
| 232 | |||
| 233 | try{ | ||
| 234 | $imagick = new Imagick(); | ||
| 235 | $imagick->setResolution( $resolution, $resolution ); | ||
| 236 | $imagick->readimage( $filepath . '[' . $page_number . ']' ); | ||
| 237 | $imagick->setCompressionQuality( $quality ); | ||
| 238 | $imagick->scaleImage( $max_width, $max_height, true ); | ||
| 239 | $imagick->setImageFormat( $type ); | ||
| 240 | $imagick->setImageBackgroundColor( $bgcolor ); | ||
| 241 | if( method_exists( 'Imagick', 'setImageAlphaChannel' ) ){ | ||
| 242 | if( defined('Imagick::ALPHACHANNEL_REMOVE') ){ | ||
| 243 | $imagick->setImageAlphaChannel( Imagick::ALPHACHANNEL_REMOVE ); | ||
| 244 | }else{ | ||
| 245 | $imagick->setImageAlphaChannel( 11 ); | ||
| 246 | } | ||
| 247 | } | ||
| 248 | if( method_exists( 'Imagick','mergeImageLayers' ) ){ | ||
| 249 | $imagick->mergeImageLayers( Imagick::LAYERMETHOD_FLATTEN ); | ||
| 250 | }else{ | ||
| 251 | $imagick = $imagick->flattenImages(); | ||
| 252 | } | ||
| 253 | $imagick = apply_filters( 'pdf_thumbnail_imagick', $imagick, $pdf_id ); | ||
| 254 | $imagick->stripImage(); | ||
| 255 | $imagick->writeImage( $new_filepath ); | ||
| 256 | $imagick->clear(); | ||
| 257 | update_post_meta( $pdf_id, '_pdf_thumbnail', $new_filename ); | ||
| 258 | do_action( 'pdf_thumbnail_generated', $new_filepath, $pdf_id ); | ||
| 259 | }catch( ImagickException $err ){ | ||
| 260 | error_log( $err ); | ||
| 261 | }catch( Exception $err ){ | ||
| 262 | error_log( $err ); | ||
| 263 | } | ||
| 264 | |||
| 265 | return true; | ||
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | function wp_mime_type_icon( $icon, $mime, $pdf_id ){ | ||
| 270 | if( $mime === 'application/pdf' && strpos( $_SERVER['REQUEST_URI'], '/wp-admin/upload.php' ) === false ){ | ||
| 271 | $thumbnail_url = $this->get_url( $pdf_id ); | ||
| 272 | if( $thumbnail_url ){ | ||
| 273 | return $thumbnail_url; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | return $icon; | ||
| 277 | } | ||
| 278 | |||
| 279 | function delete( $pdf_id ){ | ||
| 280 | if( get_post_mime_type( $pdf_id ) === 'application/pdf' ){ | ||
| 281 | $thumbnail_filepath = $this->get_path( $pdf_id ); | ||
| 282 | if( $thumbnail_filepath ){ | ||
| 283 | unlink( $thumbnail_filepath ); | ||
| 284 | } | ||
| 285 | } | ||
| 286 | } | ||
| 287 | |||
| 288 | function get_path( $pdf_id ){ | ||
| 289 | if( get_post_mime_type( $pdf_id ) === 'application/pdf' ){ | ||
| 290 | $thumbnail = get_post_meta( $pdf_id, '_pdf_thumbnail', true ); | ||
| 291 | if( $thumbnail ){ | ||
| 292 | $filepath = get_attached_file( $pdf_id ); | ||
| 293 | $thumbnail_filepath = str_replace( basename( $filepath ), $thumbnail, $filepath ); | ||
| 294 | if( file_exists( $thumbnail_filepath ) ){ | ||
| 295 | return $thumbnail_filepath; | ||
| 296 | } | ||
| 297 | } | ||
| 298 | } | ||
| 299 | return false; | ||
| 300 | } | ||
| 301 | |||
| 302 | function get_url( $pdf_id ){ | ||
| 303 | if( get_post_mime_type( $pdf_id ) === 'application/pdf' ){ | ||
| 304 | $thumbnail = get_post_meta( $pdf_id, '_pdf_thumbnail', true ); | ||
| 305 | if( $thumbnail ){ | ||
| 306 | $filepath = get_attached_file( $pdf_id ); | ||
| 307 | $thumbnail_filepath = str_replace( basename( $filepath ), $thumbnail, $filepath ); | ||
| 308 | if( file_exists( $thumbnail_filepath ) ){ | ||
| 309 | $thumbnail_url = wp_get_attachment_url( $pdf_id ); | ||
| 310 | return str_replace( basename( $filepath ), $thumbnail, $thumbnail_url ); | ||
| 311 | } | ||
| 312 | } | ||
| 313 | } | ||
| 314 | return false; | ||
| 315 | } | ||
| 316 | |||
| 317 | } | ||
| 318 | |||
| 319 | $pdf_thumbnail = new pdf_thumbnail_generator(); | ||
| 320 | register_activation_hook( __FILE__, array( $pdf_thumbnail, 'activate' ) ); | ||
| 321 | |||
| 322 | if( ! function_exists('get_pdf_thumbnail_url') ){ | ||
| 323 | function get_pdf_thumbnail_url( $pdf_id ){ | ||
| 324 | global $pdf_thumbnail; | ||
| 325 | return $pdf_thumbnail->get_url( $pdf_id ); | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | if( ! function_exists('get_pdf_thumbnail_path') ){ | ||
| 330 | function get_pdf_thumbnail_path( $pdf_id ){ | ||
| 331 | global $pdf_thumbnail; | ||
| 332 | return $pdf_thumbnail->get_path( $pdf_id ); | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | if( ! function_exists('get_pdf_thumbnail_image_src') ){ | ||
| 337 | function get_pdf_thumbnail_image_src( $pdf_id ){ | ||
| 338 | global $pdf_thumbnail; | ||
| 339 | $data = false; | ||
| 340 | $thumbnail_url = $pdf_thumbnail->get_url( $pdf_id ); | ||
| 341 | if( $thumbnail_url ){ | ||
| 342 | $thumbnail_path = $pdf_thumbnail->get_path( $pdf_id ); | ||
| 343 | $info = getimagesize( $thumbnail_path ); | ||
| 344 | $data = array( $thumbnail_url, $info[0], $info[1] ); | ||
| 345 | } | ||
| 346 | return $data; | ||
| 347 | } | ||
| 348 | } | ||
| 349 | |||
| 350 | if( ! function_exists('get_pdf_thumbnail_image') ){ | ||
| 351 | function get_pdf_thumbnail_image( $pdf_id ){ | ||
| 352 | global $pdf_thumbnail; | ||
| 353 | $html = ''; | ||
| 354 | $thumbnail_url = $pdf_thumbnail->get_url( $pdf_id ); | ||
| 355 | if( $thumbnail_url ){ | ||
| 356 | $thumbnail_path = $pdf_thumbnail->get_path( $pdf_id ); | ||
| 357 | $info = getimagesize( $thumbnail_path ); | ||
| 358 | $default_attr = array( | ||
| 359 | 'src' => $thumbnail_url, | ||
| 360 | 'class' => 'pdf-thumbnail', | ||
| 361 | 'alt' => esc_attr( trim( get_the_title( $pdf_id ) ) ), | ||
| 362 | 'width' => $info[0], | ||
| 363 | 'height' => $info[1], | ||
| 364 | ); | ||
| 365 | |||
| 366 | if( wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' ) ){ | ||
| 367 | $default_attr['loading'] = wp_get_loading_attr_default('wp_get_attachment_image'); | ||
| 368 | } | ||
| 369 | |||
| 370 | if( array_key_exists( 'loading', $default_attr ) && ! $default_attr['loading'] ){ | ||
| 371 | unset( $default_attr['loading'] ); | ||
| 372 | } | ||
| 373 | |||
| 374 | $attr = apply_filters( 'get_pdf_thumbnail_image_attributes', $default_attr, $pdf_id ); | ||
| 375 | |||
| 376 | $attr = array_map( 'esc_attr', $attr ); | ||
| 377 | $html = '<img'; | ||
| 378 | |||
| 379 | foreach( $attr as $name => $value ){ | ||
| 380 | $html .= ' ' . $name . '="' . $value . '"'; | ||
| 381 | } | ||
| 382 | |||
| 383 | $html .= '>'; | ||
| 384 | } | ||
| 385 | return $html; | ||
| 386 | } | ||
| 387 | } | ||
| 388 | } | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 1 | === PDF Thumbnail Generator === | ||
| 2 | Contributors: kubiq | ||
| 3 | Donate link: https://www.paypal.me/jakubnovaksl | ||
| 4 | Tags: pdf, image, thumbnail, generator, creator | ||
| 5 | Requires at least: 3.0.1 | ||
| 6 | Requires PHP: 5.6 | ||
| 7 | Tested up to: 6.4 | ||
| 8 | Stable tag: 1.1 | ||
| 9 | License: GPLv2 or later | ||
| 10 | License URI: http://www.gnu.org/licenses/gpl-2.0.html | ||
| 11 | |||
| 12 | Generates thumbnail for PDF files | ||
| 13 | |||
| 14 | |||
| 15 | == Description == | ||
| 16 | |||
| 17 | Generates thumbnail for PDF file automatically after file is uploaded to the Media library. | ||
| 18 | |||
| 19 | 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. | ||
| 20 | |||
| 21 | <strong>Imagick library must be installed on your server, otherwise this plugin will not work</strong> | ||
| 22 | |||
| 23 | <ul> | ||
| 24 | <li>automated test after plugin activation to make sure it will work on your server</li> | ||
| 25 | <li>works with all types of WordPress installations: domain, subdomain, subdirectory, multisite/network</li> | ||
| 26 | <li>works on Apache and NGiNX</li> | ||
| 27 | <li>automatically generate thumbnail for new uploaded PDFs</li> | ||
| 28 | <li>(re)generate thumbnails for existing PDFs in Media library</li> | ||
| 29 | <li>set maximum width of PDF thumbnail</li> | ||
| 30 | <li>set maximum height of PDF thumbnail</li> | ||
| 31 | <li>set image quality of PDF thumbnail</li> | ||
| 32 | <li>set image file type of PDF thumbnail</li> | ||
| 33 | </ul> | ||
| 34 | |||
| 35 | ## Shortcodes | ||
| 36 | |||
| 37 | ### pdf_thumbnail | ||
| 38 | Maybe you want to display PDF thumbnail by using a shortcode | ||
| 39 | |||
| 40 | `[pdf_thumbnail id="123"]` | ||
| 41 | |||
| 42 | ### pdf_thumbnail_url | ||
| 43 | Maybe you want to display PDF thumbnail url by using a shortcode | ||
| 44 | |||
| 45 | `[pdf_thumbnail_url id="123"]` | ||
| 46 | |||
| 47 | ## Functions | ||
| 48 | |||
| 49 | ### get_pdf_thumbnail_url | ||
| 50 | If you want to return PDF thumbnail URL you can use | ||
| 51 | |||
| 52 | `get_pdf_thumbnail_url( $pdf_id )` | ||
| 53 | |||
| 54 | it works similar to `wp_get_attachment_url` and it will return something like | ||
| 55 | |||
| 56 | `https://site.com/wp-content/uploads/2022/01/example.pdf.png` | ||
| 57 | |||
| 58 | ### get_pdf_thumbnail_path | ||
| 59 | If you want to return PDF thumbnail URL you can use | ||
| 60 | |||
| 61 | `get_pdf_thumbnail_path( $pdf_id )` | ||
| 62 | |||
| 63 | it works similar to `get_attached_file` and it will return something like | ||
| 64 | |||
| 65 | `/www/site.com/wp-content/uploads/2022/01/example.pdf.png` | ||
| 66 | |||
| 67 | ### get_pdf_thumbnail_image_src | ||
| 68 | If you want to return PDF thumbnail url, width and height you can use | ||
| 69 | |||
| 70 | `get_pdf_thumbnail_image_src( $pdf_id )` | ||
| 71 | |||
| 72 | it works similar to `wp_get_attachment_image_src` and it will return something like | ||
| 73 | |||
| 74 | `[ | ||
| 75 | 0 => 'https://site.com/wp-content/uploads/2022/01/example.pdf.png', | ||
| 76 | 1 => 600, | ||
| 77 | 2 => 800 | ||
| 78 | ]` | ||
| 79 | |||
| 80 | ### get_pdf_thumbnail_image | ||
| 81 | If you want to return PDF thumbnail image tag you can use | ||
| 82 | |||
| 83 | `get_pdf_thumbnail_image( $pdf_id )` | ||
| 84 | |||
| 85 | it works similar to `wp_get_attachment_image` and it will return something like | ||
| 86 | |||
| 87 | `<img src="https://site.com/wp-content/uploads/2022/01/example.pdf.png" width="600" height="800" alt="example" loading="lazy">` | ||
| 88 | |||
| 89 | ## Hooks | ||
| 90 | |||
| 91 | ### pdf_thumbnail_max_width | ||
| 92 | Maybe you want to change global PDF thumbnail max_width for a specific PDF file | ||
| 93 | |||
| 94 | `add_filter( 'pdf_thumbnail_max_width', function( $max_width, $pdf_id ){ | ||
| 95 | if( $pdf_id == 123 ){ | ||
| 96 | return 1024; | ||
| 97 | } | ||
| 98 | return $max_width; | ||
| 99 | }, 10, 2 );` | ||
| 100 | |||
| 101 | ### pdf_thumbnail_max_height | ||
| 102 | Maybe you want to change global PDF thumbnail max_width for a specific PDF file | ||
| 103 | |||
| 104 | `add_filter( 'pdf_thumbnail_max_height', function( $max_height, $pdf_id ){ | ||
| 105 | if( $pdf_id == 123 ){ | ||
| 106 | return 768; | ||
| 107 | } | ||
| 108 | return $max_height; | ||
| 109 | }, 10, 2 );` | ||
| 110 | |||
| 111 | ### pdf_thumbnail_quality | ||
| 112 | Maybe you want to change global PDF thumbnail quality for a specific PDF file | ||
| 113 | |||
| 114 | `add_filter( 'pdf_thumbnail_quality', function( $quality, $pdf_id ){ | ||
| 115 | if( $pdf_id == 123 ){ | ||
| 116 | return 100; | ||
| 117 | } | ||
| 118 | return $quality; | ||
| 119 | }, 10, 2 );` | ||
| 120 | |||
| 121 | ### pdf_thumbnail_type | ||
| 122 | Maybe you want to change global PDF thumbnail file type for a specific PDF file | ||
| 123 | |||
| 124 | `add_filter( 'pdf_thumbnail_type', function( $type, $pdf_id ){ | ||
| 125 | if( $pdf_id == 123 ){ | ||
| 126 | return 'png'; // or 'jpg' | ||
| 127 | } | ||
| 128 | return $type; | ||
| 129 | }, 10, 2 );` | ||
| 130 | |||
| 131 | ### pdf_thumbnail_bgcolor | ||
| 132 | Maybe you want to change default PDF thumbnail background for a specific PDF file | ||
| 133 | |||
| 134 | `add_filter( 'pdf_thumbnail_bgcolor', function( $bgcolor, $pdf_id ){ | ||
| 135 | if( $pdf_id == 123 ){ | ||
| 136 | return 'black'; // default is 'white' | ||
| 137 | } | ||
| 138 | return $bgcolor; | ||
| 139 | }, 10, 2 );` | ||
| 140 | |||
| 141 | ### pdf_thumbnail_page_number | ||
| 142 | Maybe you want to PDF thumbnail page number for a specific PDF file | ||
| 143 | |||
| 144 | `add_filter( 'pdf_thumbnail_page_number', function( $page, $pdf_id ){ | ||
| 145 | if( $pdf_id == 123 ){ | ||
| 146 | return 1; // default is 0 | ||
| 147 | } | ||
| 148 | return $page; | ||
| 149 | }, 10, 2 );` | ||
| 150 | |||
| 151 | ### pdf_thumbnail_filename | ||
| 152 | Maybe you want to PDF thumbnail filename for a specific PDF file | ||
| 153 | |||
| 154 | `add_filter( 'pdf_thumbnail_filename', function( $filename, $pdf_id ){ | ||
| 155 | if( $pdf_id == 123 ){ | ||
| 156 | return str_replace( '.pdf.png', '.png', $filename ); | ||
| 157 | } | ||
| 158 | return $filename; | ||
| 159 | }, 10, 2 );` | ||
| 160 | |||
| 161 | ### pdf_thumbnail_imagick | ||
| 162 | Maybe you want to add watermark to PDF thumbnail for a specific PDF file | ||
| 163 | |||
| 164 | `add_filter( 'pdf_thumbnail_imagick', function( $imagick, $pdf_id ){ | ||
| 165 | if( $pdf_id == 123 ){ | ||
| 166 | // add your watermark here | ||
| 167 | } | ||
| 168 | return $imagick; | ||
| 169 | }, 10, 2 );` | ||
| 170 | |||
| 171 | ### get_pdf_thumbnail_image_attributes | ||
| 172 | Maybe you want to change attributes for image tag from `get_pdf_thumbnail_image` function | ||
| 173 | |||
| 174 | `add_filter( 'get_pdf_thumbnail_image_attributes', function( $attr, $pdf_id ){ | ||
| 175 | $attr['loading'] = 'eager'; | ||
| 176 | return $attr; | ||
| 177 | }, 10, 2 );` | ||
| 178 | |||
| 179 | ### pdf_thumbnail_generated | ||
| 180 | Maybe you want to do something after the thumbnail is generated | ||
| 181 | |||
| 182 | `add_action( 'pdf_thumbnail_generated', function( $thumbnail_path, $pdf_id ){ | ||
| 183 | // do somthing with the local file $thumbnail_path | ||
| 184 | }, 10, 2 );` | ||
| 185 | |||
| 186 | |||
| 187 | == Installation == | ||
| 188 | |||
| 189 | 1. Upload `pdf-thumbnail-generator` directory to the `/wp-content/plugins/` directory | ||
| 190 | 2. Activate the plugin through the 'Plugins' menu in WordPress | ||
| 191 | |||
| 192 | |||
| 193 | == Frequently Asked Questions == | ||
| 194 | |||
| 195 | = Plugin requirements = | ||
| 196 | |||
| 197 | PHP 5.6 or higher | ||
| 198 | Imagick extension | ||
| 199 | |||
| 200 | = PDF thumbnails stored location = | ||
| 201 | |||
| 202 | PDF thumbnails are generated in the same directory as original PDF file. Example: | ||
| 203 | pdf file: `/wp-content/uploads/2022/01/example.pdf` | ||
| 204 | thumbnail: `/wp-content/uploads/2022/01/example.pdf.png` | ||
| 205 | |||
| 206 | |||
| 207 | == Changelog == | ||
| 208 | |||
| 209 | = 1.1 = | ||
| 210 | * tested on WP 6.4 | ||
| 211 | |||
| 212 | = 1.0 = | ||
| 213 | * First version | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or sign in to post a comment