Make WordPress Core

Ticket #39216: 39216.4.patch

File 39216.4.patch, 2.9 KB (added by gitlost, 9 years ago)

Refresh, comment changes only.

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

     
    164164                        if ( is_callable( array( $this->image, 'setIteratorIndex' ) ) )
    165165                                $this->image->setIteratorIndex(0);
    166166
     167                        if ( 'pdf' == $file_extension ) {
     168                                $this->pdf_process();
     169                        }
     170
    167171                        $this->mime_type = $this->get_mime_type( $this->image->getImageFormat() );
    168172                }
    169173                catch ( Exception $e ) {
     
    766770                }
    767771        }
    768772
     773        /**
     774         * Process PDF after it's been read.
     775         *
     776         * @since 4.x
     777         * @access protected
     778         *
     779         * @return void
     780         */
     781        protected function pdf_process() {
     782                $version = Imagick::getVersion();
     783                // Remove alpha channel if possible to avoid black backgrounds for Ghostscript >= 9.14. RemoveAlphaChannel added in ImageMagick 6.7.5.
     784                if ( $version['versionNumber'] >= 0x675 ) {
     785                        try {
     786                                // Imagick::ALPHACHANNEL_REMOVE mapped to RemoveAlphaChannel in PHP imagick 3.2.0b2.
     787                                // Value seems to have remained stable at 12 for ImageMagick 6.7.5 to 7.0 (although other AlphaChannelType enum values have changed).
     788                                $this->image->setImageAlphaChannel( defined( 'Imagick::ALPHACHANNEL_REMOVE' ) ? Imagick::ALPHACHANNEL_REMOVE : 12 );
     789                        }
     790                        catch ( Exception $e ) {
     791                                // Ignore.
     792                        }
     793                }
     794        }
    769795}
  • tests/phpunit/tests/image/editor_imagick.php

     
    572572                unlink( $ret['path'] );
    573573        }
    574574
     575        /**
     576         * @ticket 39216
     577         * Only affects systems with Ghostscript version >= 9.14.
     578         */
     579        public function test_remove_alpha_pdf_preview() {
     580                if ( ! wp_image_editor_supports( array( 'mime_type' => 'application/pdf' ) ) ) {
     581                        $this->markTestSkipped( 'Rendering PDFs is not supported on this system.' );
     582                }
     583
     584                $test_file = DIR_TESTDATA . '/images/test_alpha.pdf';
     585                $attachment_id = $this->factory->attachment->create_upload_object( $test_file );
     586                $this->assertNotEmpty( $attachment_id );
     587
     588                $attached_file = get_attached_file( $attachment_id );
     589                $this->assertNotEmpty( $attached_file );
     590
     591                $rgb = array( 'r' => true, 'g' => true, 'b' => true ); // Used for intersecting - not interested in alpha channel.
     592                $expected = array( 'r' => 1, 'g' => 1, 'b' => 1 ); // White.
     593
     594                $check = image_get_intermediate_size( $attachment_id, 'full' );
     595                $this->assertNotEmpty( $check['file'] );
     596                $check_file = path_join( dirname( $attached_file ), $check['file'] );
     597
     598                $imagick = new Imagick( $check_file );
     599                $output = array_map( 'round', array_intersect_key( $imagick->getImagePixelColor( 100, 100 )->getColor( true /*normalized*/ ), $rgb ) );
     600                $imagick->destroy();
     601                $this->assertEquals( $expected, $output ); // Allow for floating point equivalence.
     602        }
    575603}