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/tests/phpunit/tests/image/editorImagick.php

    r54401 r56271  
    642642        $this->assertNotWPError( $saved );
    643643    }
     644
     645    /**
     646     * Tests that the alpha channel of PDFs is removed from PDF previews.
     647     *
     648     * Only affects systems with Ghostscript version >= 9.14.
     649     *
     650     * @ticket 39216
     651     *
     652     * @covers WP_Image_Editor_Imagick::remove_pdf_alpha_channel
     653     */
     654    public function test_remove_pdf_alpha_channel_should_remove_the_alpha_channel_in_preview() {
     655        if ( ! wp_image_editor_supports( array( 'mime_type' => 'application/pdf' ) ) ) {
     656            $this->markTestSkipped( 'Rendering PDFs is not supported on this system.' );
     657        }
     658
     659        $test_file     = DIR_TESTDATA . '/images/test-alpha.pdf';
     660        $attachment_id = $this->factory->attachment->create_upload_object( $test_file );
     661        $this->assertNotEmpty( $attachment_id, 'The attachment was not created before testing.' );
     662
     663        $attached_file = get_attached_file( $attachment_id );
     664        $this->assertNotEmpty( $attached_file, 'The attached file was not returned.' );
     665
     666        $rgb = array(
     667            'r' => true,
     668            'g' => true,
     669            'b' => true,
     670        );
     671
     672        // White.
     673        $expected = array(
     674            'r' => 1,
     675            'g' => 1,
     676            'b' => 1,
     677        );
     678
     679        $check = image_get_intermediate_size( $attachment_id, 'full' );
     680        $this->assertIsArray( $check, 'The intermediate size could not be retrieved.' );
     681        $this->assertArrayHasKey( 'file', $check, 'The intermediate size file was not found.' );
     682
     683        $check_file = path_join( dirname( $attached_file ), $check['file'] );
     684        $imagick    = new Imagick( $check_file );
     685        $output     = array_map(
     686            static function( $value ) {
     687                return (int) round( $value );
     688            },
     689            array_intersect_key( $imagick->getImagePixelColor( 100, 100 )->getColor( true /* normalized */ ), $rgb )
     690        );
     691        $imagick->destroy();
     692        $this->assertSame( $expected, $output, 'The image color of the generated thumb does not match expected opaque background.' ); // Allow for floating point equivalence.
     693    }
    644694}
Note: See TracChangeset for help on using the changeset viewer.