Ticket #48853: 48853.2.diff
File 48853.2.diff, 3.1 KB (added by , 3 years ago) |
---|
-
src/wp-includes/class-wp-image-editor-imagick.php
140 140 try { 141 141 $this->image = new Imagick(); 142 142 $file_extension = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) ); 143 $filename = $this->file;144 143 145 144 if ( 'pdf' === $file_extension ) { 146 $filename = $this->pdf_setup(); 145 $pdf_loaded = $this->pdf_load_source(); 146 147 if ( is_wp_error( $pdf_loaded ) ) { 148 return $pdf_loaded; 149 } 150 } else { 151 $this->image->readImage( $this->file ); 147 152 } 148 153 149 // Reading image after Imagick instantiation because `setResolution`150 // only applies correctly before the image is read.151 $this->image->readImage( $filename );152 153 154 if ( ! $this->image->valid() ) { 154 155 return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file ); 155 156 } … … 165 166 } 166 167 167 168 $updated_size = $this->update_size(); 169 168 170 if ( is_wp_error( $updated_size ) ) { 169 171 return $updated_size; 170 172 } … … 785 787 * Increases rendering DPI and only loads first page. 786 788 * 787 789 * @since 4.7.0 790 * @deprecated 5.5.0 use WP_Image_Editor_Imagick::pdf_load_source() that also loads the source. 788 791 * 789 792 * @return string|WP_Error File to load or WP_Error on failure. 790 793 */ 791 794 protected function pdf_setup() { 795 _deprecated_function( __FUNCTION__, '5.5.0', 'WP_Image_Editor_Imagick::pdf_load_source()' ); 796 792 797 try { 793 798 // By default, PDFs are rendered in a very low resolution. 794 799 // We want the thumbnail to be readable, so increase the rendering DPI. … … 805 810 } 806 811 } 807 812 813 /** 814 * Sets up Imagick for PDF processing and load the image outputted by `gs`. 815 * Increases rendering DPI and only loads first page. 816 * 817 * @since 5.5.0 818 * 819 * @return true|WP_Error True on success, WP_Error on failure. 820 */ 821 protected function pdf_load_source() { 822 // By default, PDFs are rendered in a very low resolution. 823 // We want the thumbnail to be readable, so increase the rendering DPI. 824 // (`setResolution` has to be done before the image is loaded.) 825 $this->image->setResolution( 128, 128 ); 826 827 try { 828 // When generating thumbnails from cropped PDF pages, Imagemagick uses the uncropped 829 // area (resulting in unnecessary whitespace) unless the following option is set. 830 $this->image->setOption( 'pdf:use-cropbox', true ); 831 832 // Only load the first page. 833 $this->image->readImage( $this->file . '[0]' ); 834 } catch ( Exception $e ) { 835 try { 836 // Attempt to run `gs` without the `use-cropbox` option. 837 // This is a workaround for a bug in GNU Ghostscript 8.7 (and possibly other old versions) 838 // that may prevent processing of some PDF files when `use-cropbox` is set. See #48853. 839 $this->image->setOption( 'pdf:use-cropbox', false ); 840 $this->image->readImage( $this->file . '[0]' ); 841 } catch ( Exception $ex ) { 842 return new WP_Error( 'pdf_setup_failed', $ex->getMessage(), $this->file ); 843 } 844 } 845 846 return true; 847 } 848 808 849 }