Make WordPress Core


Ignore:
Timestamp:
10/20/2020 02:35:10 PM (4 years ago)
Author:
mikeschroder
Message:

Media: Support Stream Wrappers In WP_Image_Editor_Imagick

Since WP_Image_Editor's introduction, stream wrappers have functioned in WP_Image_Editor_GD, but haven't been properly supported in WP_Image_Editor_Imagick.

  • Detects stream wrappers and uses file_put_contents() along with Imagick::read/getImageBlob() for handling when necessary.
  • Introduces private method, WP_Image_Editor_Imagick::write_image to handle detection and proper saving.
  • Introduces WP_Test_Stream class for testing stream wrappers, along with new tests for Imagick's stream handling and a stream filename test.

Adds requirement for Imagick::readImageBlob(), available in Imagick >= 2.0.0, which aligns with the current requirement of Imagick >= 2.2.0.

Props p00ya, calin, joemcgill, pputzer, jimyaghi, mikeschroder.
Fixes #42663.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/image/editorImagick.php

    r49108 r49230  
    1717        require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
    1818        require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php';
     19        require_once DIR_TESTROOT . '/includes/class-wp-test-stream.php';
    1920
    2021        parent::setUp();
     
    575576    }
    576577
     578    /**
     579     * Test that images can be loaded and written over streams
     580     */
     581    public function test_streams() {
     582        stream_wrapper_register( 'wptest', 'WP_Test_Stream' );
     583        WP_Test_Stream::$data = array(
     584            'Tests_Image_Editor_Imagick' => array(
     585                '/read.jpg' => file_get_contents( DIR_TESTDATA . '/images/waffles.jpg' ),
     586            ),
     587        );
     588
     589        $file                 = 'wptest://Tests_Image_Editor_Imagick/read.jpg';
     590        $imagick_image_editor = new WP_Image_Editor_Imagick( $file );
     591
     592        $ret = $imagick_image_editor->load();
     593        $this->assertNotWPError( $ret );
     594
     595        $temp_file = 'wptest://Tests_Image_Editor_Imagick/write.jpg';
     596
     597        $ret = $imagick_image_editor->save( $temp_file );
     598        $this->assertNotWPError( $ret );
     599
     600        $this->assertSame( $temp_file, $ret['path'] );
     601
     602        if ( $temp_file !== $ret['path'] ) {
     603            unlink( $ret['path'] );
     604        }
     605        unlink( $temp_file );
     606    }
    577607}
Note: See TracChangeset for help on using the changeset viewer.