Commit 9cdd649768 for wordpress.org

commit 9cdd64976822de65e0b059915cb338f4dc352df1
Author: Mike Schroder <mschrode@umich.edu>
Date:   Fri Oct 16 10:48:07 2020 +0000

    Media: Work around `use-cropbox` bug in Ghostscript 8.70

    Wraps `Imagick::readImage()` for PDFs with exception handling, trying again without `use-cropbox` if this fails.

    Introduces `WP_Image_Editor_Imagick::pdf_load_source()`.

    Works around a known issue in Ghostscript 8.70 (fixed in 8.71) that results in a stack underflow.
    While it only affects this version, it remains a common version found on hosts, and prevented some PDF thumbnails from being generated.

    See this Ghostscript bug for more details: https://bugs.ghostscript.com/show_bug.cgi?id=690676

    Props azaozz, joseaneto, cranewest, dantahoua, n5hzr, mikeschroder.
    Fixes #48853.
    Built from https://develop.svn.wordpress.org/trunk@49174


    git-svn-id: http://core.svn.wordpress.org/trunk@48936 1a063a9b-81f0-0310-95a4-ce76da25c4cd

diff --git a/wp-includes/class-wp-image-editor-imagick.php b/wp-includes/class-wp-image-editor-imagick.php
index dbdf1607cd..0d2aa05ac7 100644
--- a/wp-includes/class-wp-image-editor-imagick.php
+++ b/wp-includes/class-wp-image-editor-imagick.php
@@ -140,15 +140,16 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
 		try {
 			$this->image    = new Imagick();
 			$file_extension = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) );
-			$filename       = $this->file;

 			if ( 'pdf' === $file_extension ) {
-				$filename = $this->pdf_setup();
-			}
+				$pdf_loaded = $this->pdf_load_source();

-			// Reading image after Imagick instantiation because `setResolution`
-			// only applies correctly before the image is read.
-			$this->image->readImage( $filename );
+				if ( is_wp_error( $pdf_loaded ) ) {
+					return $pdf_loaded;
+				}
+			} else {
+				$this->image->readImage( $this->file );
+			}

 			if ( ! $this->image->valid() ) {
 				return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file );
@@ -165,6 +166,7 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
 		}

 		$updated_size = $this->update_size();
+
 		if ( is_wp_error( $updated_size ) ) {
 			return $updated_size;
 		}
@@ -795,15 +797,46 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
 			// 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 );
+		}
+	}
+
+	/**
+	 * Load the image produced by Ghostscript.
+	 *
+	 * Includes a workaround for a bug in Ghostscript 8.70 that prevents processing of some PDF files
+	 * when `use-cropbox` is set.
+	 *
+	 * @since 5.6
+	 *
+	 * @return true|WP_error
+	 */
+	protected function pdf_load_source() {
+		$filename = $this->pdf_setup();
+
+		if ( is_wp_error( $filename ) ) {
+			return $filename;
+		}
+
+		try {
 			// When generating thumbnails from cropped PDF pages, Imagemagick uses the uncropped
 			// area (resulting in unnecessary whitespace) unless the following option is set.
 			$this->image->setOption( 'pdf:use-cropbox', true );

-			// Only load the first page.
-			return $this->file . '[0]';
+			// Reading image after Imagick instantiation because `setResolution`
+			// only applies correctly before the image is read.
+			$this->image->readImage( $filename );
 		} catch ( Exception $e ) {
-			return new WP_Error( 'pdf_setup_failed', $e->getMessage(), $this->file );
+			// Attempt to run `gs` without the `use-cropbox` option. See #48853.
+			$this->image->setOption( 'pdf:use-cropbox', false );
+
+			$this->image->readImage( $filename );
 		}
+
+		return true;
 	}

 }
diff --git a/wp-includes/version.php b/wp-includes/version.php
index f4fcdf97ad..a2991f4004 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -13,7 +13,7 @@
  *
  * @global string $wp_version
  */
-$wp_version = '5.6-alpha-49173';
+$wp_version = '5.6-alpha-49174';

 /**
  * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.