]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/class-wp-image-editor-imagick.php
WordPress 4.7.1
[autoinstalls/wordpress.git] / wp-includes / class-wp-image-editor-imagick.php
index 82b872d7321406b1921e910aee210fac717c6d39..8f9b43cb8955d30b6d028029ab42a8e9998d3e9a 100644 (file)
@@ -73,6 +73,7 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
                        'rotateimage',
                        'flipimage',
                        'flopimage',
+                       'readimage',
                );
 
                // Now, test for deep requirements within Imagick.
@@ -144,7 +145,17 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
                wp_raise_memory_limit( 'image' );
 
                try {
-                       $this->image = new Imagick( $this->file );
+                       $this->image = new Imagick();
+                       $file_extension = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) );
+                       $filename = $this->file;
+
+                       if ( 'pdf' == $file_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);
@@ -727,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 );
+               }
+       }
+
 }