Make WordPress Core


Ignore:
Timestamp:
07/19/2023 10:33:47 PM (14 months ago)
Author:
antpb
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-image-editor-imagick.php

    r56250 r56271  
    169169            }
    170170
     171            if ( 'pdf' === $file_extension ) {
     172                $this->remove_pdf_alpha_channel();
     173            }
     174
    171175            $this->mime_type = $this->get_mime_type( $this->image->getImageFormat() );
    172176        } catch ( Exception $e ) {
     
    753757
    754758    /**
     759     * Removes PDF alpha after it's been read.
     760     *
     761     * @since 6.4.0
     762     */
     763    protected function remove_pdf_alpha_channel() {
     764        $version = Imagick::getVersion();
     765        // Remove alpha channel if possible to avoid black backgrounds for Ghostscript >= 9.14. RemoveAlphaChannel added in ImageMagick 6.7.5.
     766        if ( $version['versionNumber'] >= 0x675 ) {
     767            try {
     768                // Imagick::ALPHACHANNEL_REMOVE mapped to RemoveAlphaChannel in PHP imagick 3.2.0b2.
     769                $this->image->setImageAlphaChannel( defined( 'Imagick::ALPHACHANNEL_REMOVE' ) ? Imagick::ALPHACHANNEL_REMOVE : 12 );
     770            } catch ( Exception $e ) {
     771                return new WP_Error( 'pdf_alpha_process_failed', $e->getMessage() );
     772            }
     773        }
     774    }
     775
     776    /**
    755777     * @since 3.5.0
    756778     * @since 6.0.0 The `$filesize` value was added to the returned array.
Note: See TracChangeset for help on using the changeset viewer.