Make WordPress Core

Ticket #48853: 48853.2.diff

File 48853.2.diff, 3.1 KB (added by azaozz, 3 years ago)
  • src/wp-includes/class-wp-image-editor-imagick.php

     
    140140                try {
    141141                        $this->image    = new Imagick();
    142142                        $file_extension = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) );
    143                         $filename       = $this->file;
    144143
    145144                        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 );
    147152                        }
    148153
    149                         // Reading image after Imagick instantiation because `setResolution`
    150                         // only applies correctly before the image is read.
    151                         $this->image->readImage( $filename );
    152 
    153154                        if ( ! $this->image->valid() ) {
    154155                                return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file );
    155156                        }
     
    165166                }
    166167
    167168                $updated_size = $this->update_size();
     169
    168170                if ( is_wp_error( $updated_size ) ) {
    169171                        return $updated_size;
    170172                }
     
    785787         * Increases rendering DPI and only loads first page.
    786788         *
    787789         * @since 4.7.0
     790         * @deprecated 5.5.0 use WP_Image_Editor_Imagick::pdf_load_source() that also loads the source.
    788791         *
    789792         * @return string|WP_Error File to load or WP_Error on failure.
    790793         */
    791794        protected function pdf_setup() {
     795                _deprecated_function( __FUNCTION__, '5.5.0', 'WP_Image_Editor_Imagick::pdf_load_source()' );
     796
    792797                try {
    793798                        // By default, PDFs are rendered in a very low resolution.
    794799                        // We want the thumbnail to be readable, so increase the rendering DPI.
     
    805810                }
    806811        }
    807812
     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
    808849}