From 0ab17d2158845681f58b5fc35beeeecf64a5cc85 Mon Sep 17 00:00:00 2001 From: Mike Schroder Date: Fri, 18 Nov 2016 22:21:19 +0000 Subject: [PATCH] Media: Allow override of PDF setup for multiple pages or DPI. After [39187], WordPress started loading only the first page of a PDF. This is appropriate for performance, but made it impossible to write plugins that read other pages without overriding `load()`. Introduces `WP_Image_Editor_Imagick->pdf_setup()`, to allow an override to change WordPress' rendering DPI defaults or which pages are loaded. Fixes #38832. See #38522, #31050. Props markoheijnen, joemcgill, mikeschroder. git-svn-id: https://develop.svn.wordpress.org/trunk@39303 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-image-editor-imagick.php | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-image-editor-imagick.php b/src/wp-includes/class-wp-image-editor-imagick.php index c89706cd58..fc6fc933ae 100644 --- a/src/wp-includes/class-wp-image-editor-imagick.php +++ b/src/wp-includes/class-wp-image-editor-imagick.php @@ -149,13 +149,8 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { $file_parts = pathinfo( $this->file ); $filename = $this->file; - // By default, PDFs are rendered in a very low resolution. - // We want the thumbnail to be readable, so increase the rendering dpi. if ( 'pdf' == strtolower( $file_parts['extension'] ) ) { - $this->image->setResolution( 128, 128 ); - - // Only load the first page. - $filename .= '[0]'; + $filename = $this->pdf_setup(); } // Reading image after Imagick instantiation because `setResolution` @@ -743,4 +738,27 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { return true; } + /** + * Sets up Imagick for PDF processing. + * Increases rendering DPI and only loads first page. + * + * @since 4.7.0 + * @access protected + * + * @return string|WP_Error File to load or WP_Error on failure. + */ + protected function pdf_setup() { + try { + // By default, PDFs are rendered in a very low resolution. + // We want the thumbnail to be readable, so increase the rendering DPI. + $this->image->setResolution( 128, 128 ); + + // Only load the first page. + return $this->file . '[0]'; + } + catch ( Exception $e ) { + return new WP_Error( 'pdf_setup_failed', $e->getMessage(), $this->file ); + } + } + }