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

WordPress Trac noreply at wordpress.org
Mon Jan 14 13:45:47 UTC 2019


#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        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 I wouldn't necessarily call this a bug, but when a PDF is uploaded where
 the page has no solid background colour its thumbnail image background is
 rendered black. As most paper used is white ( I would hazard a guess... :)
 ), it feels like the default background colour of thumbnails should be set
 to white. I have created a fix within'' /wp-includes/class-wp-image-
 editor-imagick.php''

 {{{#!php
 <?php
 try {
                         $this->image = new Imagick();
                         $file_extension = strtolower( pathinfo(
 $this->file, PATHINFO_EXTENSION ) );
                         $filename = $this->file;

                         if ( 'pdf' == $file_extension ) {
                                 $filename = $this->pdf_setup();
                         }

                         // Reading image after Imagick instantiation
 because `setResolution`
                         // only applies correctly before the image is
 read.
                         $this->image->readImage( $filename );

                         if ( ! $this->image->valid() )
                                 return new WP_Error( 'invalid_image',
 __('File is not an image.'), $this->file);

                         // Select the first frame to handle animated
 images properly
                         if ( is_callable( array( $this->image,
 'setIteratorIndex' ) ) )
                                 $this->image->setIteratorIndex(0);

                         $this->mime_type = $this->get_mime_type(
 $this->image->getImageFormat() );

                         // !bb modification to default to white
 backgrounds

                         if ( 'pdf' == $file_extension ) {

                                 if ($this->image->getImageAlphaChannel())
 {

                                     // Remove alpha channel (use 11 if
 constant does not work... see https://stackoverflow.com/questions/10805122
 /imagemagick-setimagealphachannel-not-working-php)
 $this->image->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);

                                     // set image background color to white
 $this->image->setImageBackgroundColor('#ffffff');
                                 }
                         }// !bb end modification to default to white
 backgrounds


                 }
 }}}

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


More information about the wp-trac mailing list