Make WordPress Core

Ticket #39216: 39216.patch

File 39216.patch, 3.7 KB (added by gitlost, 9 years ago)

Patch to remove alpha channel. Unittest requires blank test pdf.

  • src/wp-includes/class-wp-image-editor-imagick.php

     
    146146
    147147                try {
    148148                        $this->image = new Imagick();
    149                         $file_parts = pathinfo( $this->file );
     149                        $is_pdf = 'pdf' === strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) );
    150150                        $filename = $this->file;
    151151
    152                         if ( 'pdf' == strtolower( $file_parts['extension'] ) ) {
     152                        if ( $is_pdf ) {
    153153                                $filename = $this->pdf_setup();
    154154                        }
    155155
     
    164164                        if ( is_callable( array( $this->image, 'setIteratorIndex' ) ) )
    165165                                $this->image->setIteratorIndex(0);
    166166
     167                        if ( $is_pdf ) {
     168                                $this->pdf_process();
     169                        }
     170
    167171                        $this->mime_type = $this->get_mime_type( $this->image->getImageFormat() );
    168172                }
    169173                catch ( Exception $e ) {
     
    761765                }
    762766        }
    763767
     768        /**
     769         * Process PDF after it's been read.
     770         * Remove alpha channel to avoid black backgrounds.
     771         *
     772         * @since 4.x
     773         * @access protected
     774         *
     775         * @return void
     776         */
     777        protected function pdf_process() {
     778                // Remove alpha channel if possible to avoid black backgrounds. RemoveAlphaChannel added in ImageMagick 6.7.5.
     779                $version = Imagick::getVersion();
     780                if ( $version['versionNumber'] >= 0x675 ) {
     781                        try {
     782                                // Imagick::ALPHACHANNEL_REMOVE mapped to RemoveAlphaChannel in PHP imagick 3.2.0b2.
     783                                // Value seems to have remained stable at 12 for ImageMagick 6.7.5 to 7.0 (although other AlphaChannelType enum values have changed).
     784                                $this->image->setImageAlphaChannel( defined( 'Imagick::ALPHACHANNEL_REMOVE' ) ? Imagick::ALPHACHANNEL_REMOVE : 12 );
     785                        }
     786                        catch ( Exception $e ) {
     787                                // Ignore.
     788                        }
     789                }
     790        }
    764791}
  • tests/phpunit/tests/image/editor_imagick.php

     
    531531                $this->assertImageAlphaAtPointImagick( $save_to_file, array( 0, 0 ), $pre_rotate_alpha );
    532532                unlink( $save_to_file );
    533533        }
     534
     535        /**
     536         * @ticket 39216
     537         */
     538        public function test_remove_alpha_pdf_preview() {
     539                if ( ! wp_image_editor_supports( array( 'mime_type' => 'application/pdf' ) ) ) {
     540                        $this->markTestSkipped( 'Rendering PDFs is not supported on this system.' );
     541                }
     542
     543                $test_file = 'test_alpha.pdf';
     544                $test_path = DIR_TESTDATA . '/images/' . $test_file;
     545                $attachment_id = $this->factory->attachment->create_upload_object( $test_path );
     546                $this->assertNotEmpty( $attachment_id );
     547
     548                $attached_file = get_attached_file( $attachment_id );
     549                $this->assertNotEmpty( $attached_file );
     550
     551                $check = image_get_intermediate_size( $attachment_id, 'thumbnail' );
     552                $this->assertNotEmpty( $check['file'] );
     553                $check_file = str_replace( $test_file, $check['file'], $attached_file );
     554
     555                $imagick = new Imagick( $check_file );
     556                $expected = array( 'r' => 1, 'g' => 1, 'b' => 1, 'a' => 1 ); // White.
     557                $output = array_map( 'ceil', $imagick->getImagePixelColor( 1, 1 )->getColor( true /*normalized*/ ) );
     558                $this->assertEquals( $expected, $output ); // Allow for floating point equivalence.
     559                $imagick->destroy();
     560
     561                $check = image_get_intermediate_size( $attachment_id, 'full' );
     562                $this->assertNotEmpty( $check['file'] );
     563                $check_file = str_replace( $test_file, $check['file'], $attached_file );
     564
     565                $imagick = new Imagick( $check_file );
     566                $expected = array( 'r' => 1, 'g' => 1, 'b' => 1, 'a' => 1 ); // White.
     567                $output = array_map( 'ceil', $imagick->getImagePixelColor( 100, 100 )->getColor( true /*normalized*/ ) );
     568                $this->assertEquals( $expected, $output ); // Allow for floating point equivalence.
     569                $imagick->destroy();
     570        }
    534571}