]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/class-wp-image-editor-imagick.php
WordPress 4.7
[autoinstalls/wordpress.git] / wp-includes / class-wp-image-editor-imagick.php
index 74ba793be7a66f88cb2d78dca7bf8854521407fb..fc6fc933ae383bcd6e10c1b498a003dd0255f4bd 100644 (file)
@@ -73,6 +73,7 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
                        'rotateimage',
                        'flipimage',
                        'flopimage',
                        'rotateimage',
                        'flipimage',
                        'flopimage',
+                       'readimage',
                );
 
                // Now, test for deep requirements within Imagick.
                );
 
                // Now, test for deep requirements within Imagick.
@@ -137,12 +138,24 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
                if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) )
                        return new WP_Error( 'error_loading_image', __('File doesn’t exist?'), $this->file );
 
                if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) )
                        return new WP_Error( 'error_loading_image', __('File doesn’t exist?'), $this->file );
 
-               /** This filter is documented in wp-includes/class-wp-image-editor-imagick.php */
-               // Even though Imagick uses less PHP memory than GD, set higher limit for users that have low PHP.ini limits
-               @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );
+               /*
+                * Even though Imagick uses less PHP memory than GD, set higher limit
+                * for users that have low PHP.ini limits.
+                */
+               wp_raise_memory_limit( 'image' );
 
                try {
 
                try {
-                       $this->image = new Imagick( $this->file );
+                       $this->image = new Imagick();
+                       $file_parts = pathinfo( $this->file );
+                       $filename = $this->file;
+
+                       if ( 'pdf' == strtolower( $file_parts['extension'] ) ) {
+                               $filename = $this->pdf_setup();
+                       }
+
+                       // Reading image after Imagick instantiation because `setResolution`
+                       // only applies correctly before the image is read.
+                       $this->image->readImage( $filename );
 
                        if ( ! $this->image->valid() )
                                return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file);
 
                        if ( ! $this->image->valid() )
                                return new WP_Error( 'invalid_image', __('File is not an image.'), $this->file);
@@ -311,7 +324,7 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
                }
 
                /**
                }
 
                /**
-                * Filter whether to strip metadata from images when they're resized.
+                * Filters whether to strip metadata from images when they're resized.
                 *
                 * This filter only applies when resizing using the Imagick editor since GD
                 * always strips profiles by default.
                 *
                 * This filter only applies when resizing using the Imagick editor since GD
                 * always strips profiles by default.
@@ -343,7 +356,7 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
                         * Use resizeImage() when it's available and a valid filter value is set.
                         * Otherwise, fall back to the scaleImage() method for resizing, which
                         * results in better image quality over resizeImage() with default filter
                         * Use resizeImage() when it's available and a valid filter value is set.
                         * Otherwise, fall back to the scaleImage() method for resizing, which
                         * results in better image quality over resizeImage() with default filter
-                        * settings and retains backwards compatibility with pre 4.5 functionality.
+                        * settings and retains backward compatibility with pre 4.5 functionality.
                         */
                        if ( is_callable( array( $this->image, 'resizeImage' ) ) && $filter ) {
                                $this->image->setOption( 'filter:support', '2.0' );
                         */
                        if ( is_callable( array( $this->image, 'resizeImage' ) ) && $filter ) {
                                $this->image->setOption( 'filter:support', '2.0' );
@@ -725,4 +738,27 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
                return true;
        }
 
                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 );
+               }
+       }
+
 }
 }