Make WordPress Core

Ticket #39216: 39216-5.patch

File 39216-5.patch, 3.4 KB (added by antpb, 3 years ago)

Updating coding standards and conflicts

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

     
    166166                                $this->image->setIteratorIndex( 0 );
    167167                        }
    168168
     169                        if ( 'pdf' === $file_extension ) {
     170                                $this->pdf_process();
     171                        }
     172
    169173                        $this->mime_type = $this->get_mime_type( $this->image->getImageFormat() );
    170174                } catch ( Exception $e ) {
    171175                        return new WP_Error( 'invalid_image', $e->getMessage(), $this->file );
     
    746750        }
    747751
    748752        /**
     753         * Process PDF after it's been read.
     754         *
     755         * @since  6.3.0
     756         * @access protected
     757         *
     758         * @return void
     759         */
     760        protected function pdf_process() {
     761                $version = Imagick::getVersion();
     762                // Remove alpha channel if possible to avoid black backgrounds for Ghostscript >= 9.14. RemoveAlphaChannel added in ImageMagick 6.7.5.
     763                if ( $version['versionNumber'] >= 0x675 ) {
     764                        try {
     765                                // Imagick::ALPHACHANNEL_REMOVE mapped to RemoveAlphaChannel in PHP imagick 3.2.0b2.
     766                                $this->image->setImageAlphaChannel( defined( 'Imagick::ALPHACHANNEL_REMOVE' ) ? Imagick::ALPHACHANNEL_REMOVE : 12 );
     767                        }
     768                        catch ( Exception $e ) {
     769                                return new WP_Error( 'pdf_alpha_process_failed', $e->getMessage() );
     770                        }
     771                }
     772        }
     773
     774        /**
    749775         * @since 3.5.0
    750776         * @since 6.0.0 The `$filesize` value was added to the returned array.
    751777         *
  • tests/phpunit/tests/image/editorImagick.php

    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: tests/phpunit/data/images/test-alpha.pdf
    ___________________________________________________________________
    Added: svn:mime-type
    ## -0,0 +1 ##
    +application/octet-stream
    \ No newline at end of property
     
    641641
    642642                $this->assertNotWPError( $saved );
    643643        }
     644
     645        /**
     646         * @ticket 39216
     647         * Only affects systems with Ghostscript version >= 9.14.
     648         */
     649        public function test_remove_alpha_pdf_preview() {
     650                if ( ! wp_image_editor_supports( array( 'mime_type' => 'application/pdf' ) ) ) {
     651                        $this->markTestSkipped( 'Rendering PDFs is not supported on this system.' );
     652                }
     653
     654                $test_file = DIR_TESTDATA . '/images/test-alpha.pdf';
     655                $attachment_id = $this->factory->attachment->create_upload_object( $test_file );
     656                $this->assertNotEmpty( $attachment_id );
     657
     658                $attached_file = get_attached_file( $attachment_id );
     659                $this->assertNotEmpty( $attached_file );
     660
     661                $rgb = array( 'r' => true, 'g' => true, 'b' => true ); // Used for intersecting - not interested in alpha channel.
     662                $expected = array( 'r' => 1, 'g' => 1, 'b' => 1 ); // White.
     663
     664                        $check = image_get_intermediate_size( $attachment_id, 'full' );
     665                $this->assertNotEmpty( $check['file'] );
     666                $check_file = path_join( dirname( $attached_file ), $check['file'] );
     667
     668                $imagick = new Imagick( $check_file );
     669                $output = array_map( 'round', array_intersect_key( $imagick->getImagePixelColor( 100, 100 )->getColor( true /*normalized*/ ), $rgb ) );
     670                $imagick->destroy();
     671                $this->assertEquals( $expected, $output ); // Allow for floating point equivalence.
     672        }
    644673}