[wp-trac] [WordPress Trac] #45982: PDF thumbnails have a default black background
WordPress Trac
noreply at wordpress.org
Wed Dec 16 11:04:22 UTC 2020
#45982: PDF thumbnails have a default black background
--------------------------+------------------------------
Reporter: nebrekab | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Media | Version: 5.0.3
Severity: normal | Resolution:
Keywords: | Focuses:
--------------------------+------------------------------
Comment (by jlambe):
I was facing this exact same issue recently but it seems there is an easy
fix for this by providing your own editor class that extends the core
`WP_Image_Editor_Imagick` class. You can hook into the `wp_image_editors`
filter and add your editor class as the first one from the list.
Here is an example of a working solution.
First, create a custom editor class that extends the imagick core class:
{{{#!php
<?php
namespace App\Cms\Media\Editors;
final class ExtendedWpImageEditorImagick extends \WP_Image_Editor_Imagick
{
protected function pdf_load_source()
{
$loaded = parent::pdf_load_source();
try {
$this->image->setImageAlphaChannel(\Imagick::ALPHACHANNEL_REMOVE);
$this->image->setBackgroundColor('white');
} catch (\Exception $exception) {
error_log($exception->getMessage());
}
return $loaded;
}
}
}}}
Next hook into the filter and set your editor as the first implementation
like so:
{{{#!php
<?php
add_filter('wp_image_editors', function (array $editors) {
array_unshift($editors, ExtendedWpImageEditorImagick::class);
return $editors;
});
}}}
In my example, I was able to remove the transparent alpha channel of our
PDFs to help generate a preview image with a white background. I've
overwritten the `pdf_load_source` method but you can override any other
method based on your needs.
I hope this can also help. It's not that I'm against a hook for background
color property only but perhaps a more generic hook to get the
`$this->image` instance from the imagick core editor would be useful. At
least where we're sure it is correctly loaded. In the meantime, the
solution above can help people customize the behavior without touching to
the core files for their project.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/45982#comment:16>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list