[wp-trac] [WordPress Trac] #45982: PDF thumbnails have a default black background

WordPress Trac noreply at wordpress.org
Mon Dec 21 06:10:27 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 thatstevensguy):

 If you want to just dump @jlambe's great solution at the end of your
 themes functions.php, you will get errors about the namespace not being at
 the start of the file, and correct me if I am wrong, but its also not good
 practice to have more than one namespace per file.

 In this case, the below less elegant version is working for me.

 {{{#!php
 <?php

 /**
  * Patch for black backgrounds on PDF's.
  *
  * https://core.trac.wordpress.org/ticket/45982
  */
 require_once __DIR__ . '/../../../wp-includes/class-wp-image-editor.php';
 require_once __DIR__ . '/../../../wp-includes/class-wp-image-editor-
 imagick.php';

 // phpcs:ignore PSR1.Classes.ClassDeclaration.MissingNamespace
 final class ExtendedWpImageEditorImagick extends WP_Image_Editor_Imagick
 {
     /**
      * Add properties to the image produced by Ghostscript to prevent
 black PDF backgrounds.
      *
      * @return true|WP_error
      */
     // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
     protected function pdf_load_source()
     {
         $loaded = parent::pdf_load_source();

         try {
 $this->image->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
             $this->image->setBackgroundColor('#ffffff');
         } catch (Exception $exception) {
             error_log($exception->getMessage());
         }

         return $loaded;
     }
 }

 /**
  * Filters the list of image editing library classes to prevent black PDF
 backgrounds.
  *
  * @param array $editors
  * @return array
  */
 add_filter('wp_image_editors', function (array $editors): array {
     array_unshift($editors, ExtendedWpImageEditorImagick::class);

     return $editors;
 });

 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/45982#comment:17>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list