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
This commit is contained in:
antpb 2023-07-19 22:35:27 +00:00
parent c0f28e113c
commit cc3652efe9
2 changed files with 23 additions and 1 deletions

View File

@ -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.

View File

@ -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.