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:
parent
c0f28e113c
commit
cc3652efe9
wp-includes
|
@ -168,6 +168,10 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor {
|
||||||
$this->image->setIteratorIndex( 0 );
|
$this->image->setIteratorIndex( 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( 'pdf' === $file_extension ) {
|
||||||
|
$this->remove_pdf_alpha_channel();
|
||||||
|
}
|
||||||
|
|
||||||
$this->mime_type = $this->get_mime_type( $this->image->getImageFormat() );
|
$this->mime_type = $this->get_mime_type( $this->image->getImageFormat() );
|
||||||
} catch ( Exception $e ) {
|
} catch ( Exception $e ) {
|
||||||
return new WP_Error( 'invalid_image', $e->getMessage(), $this->file );
|
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;
|
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 3.5.0
|
||||||
* @since 6.0.0 The `$filesize` value was added to the returned array.
|
* @since 6.0.0 The `$filesize` value was added to the returned array.
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @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.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
Loading…
Reference in New Issue