From cc3652efe9a78c732d4c61d5d85f04ab64e10343 Mon Sep 17 00:00:00 2001 From: antpb Date: Wed, 19 Jul 2023 22:35:27 +0000 Subject: [PATCH] Media: Adjust PDF upload handling to remove non-opaque alpha channels from previews. Previously, Imagick uploads of PDF files with non-opaque alpha channels would result in a black background replacing alpha in the generated thumbnail. This patch adds a `remove_pdf_alpha_channel()` function in the Imagick classes to use a white background instead. Props gitlost, joemcgill, joedolson, launchinteractive, emirpprime, mwtsn, ceer, maysi, madejackson, 6adminit, costdev, oglekler. Fixes #39216. Built from https://develop.svn.wordpress.org/trunk@56271 git-svn-id: http://core.svn.wordpress.org/trunk@55783 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-image-editor-imagick.php | 22 +++++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/wp-includes/class-wp-image-editor-imagick.php b/wp-includes/class-wp-image-editor-imagick.php index 756fcce894..f29373f253 100644 --- a/wp-includes/class-wp-image-editor-imagick.php +++ b/wp-includes/class-wp-image-editor-imagick.php @@ -168,6 +168,10 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { $this->image->setIteratorIndex( 0 ); } + if ( 'pdf' === $file_extension ) { + $this->remove_pdf_alpha_channel(); + } + $this->mime_type = $this->get_mime_type( $this->image->getImageFormat() ); } catch ( Exception $e ) { return new WP_Error( 'invalid_image', $e->getMessage(), $this->file ); @@ -751,6 +755,24 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { return $saved; } + /** + * Removes PDF alpha after it's been read. + * + * @since 6.4.0 + */ + protected function remove_pdf_alpha_channel() { + $version = Imagick::getVersion(); + // Remove alpha channel if possible to avoid black backgrounds for Ghostscript >= 9.14. RemoveAlphaChannel added in ImageMagick 6.7.5. + if ( $version['versionNumber'] >= 0x675 ) { + try { + // Imagick::ALPHACHANNEL_REMOVE mapped to RemoveAlphaChannel in PHP imagick 3.2.0b2. + $this->image->setImageAlphaChannel( defined( 'Imagick::ALPHACHANNEL_REMOVE' ) ? Imagick::ALPHACHANNEL_REMOVE : 12 ); + } catch ( Exception $e ) { + return new WP_Error( 'pdf_alpha_process_failed', $e->getMessage() ); + } + } + } + /** * @since 3.5.0 * @since 6.0.0 The `$filesize` value was added to the returned array. diff --git a/wp-includes/version.php b/wp-includes/version.php index 89c095943c..8ae67197d0 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.4-alpha-56270'; +$wp_version = '6.4-alpha-56271'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.