From a8d8f22f4e4064f1396c50772e9b72e677cab145 Mon Sep 17 00:00:00 2001 From: Mike Schroder Date: Tue, 20 Oct 2020 14:37:05 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@49230 git-svn-id: http://core.svn.wordpress.org/trunk@48992 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-image-editor-imagick.php | 51 +++++++++++++++++-- wp-includes/class-wp-image-editor.php | 10 ++-- wp-includes/version.php | 2 +- 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/wp-includes/class-wp-image-editor-imagick.php b/wp-includes/class-wp-image-editor-imagick.php index 0d2aa05ac7..5a2620330a 100644 --- a/wp-includes/class-wp-image-editor-imagick.php +++ b/wp-includes/class-wp-image-editor-imagick.php @@ -71,6 +71,7 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { 'flipimage', 'flopimage', 'readimage', + 'readimageblob', ); // Now, test for deep requirements within Imagick. @@ -127,7 +128,7 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { return true; } - if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) { + if ( ! is_file( $this->file ) && ! wp_is_stream( $this->file ) ) { return new WP_Error( 'error_loading_image', __( 'File doesn’t exist?' ), $this->file ); } @@ -148,7 +149,12 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { return $pdf_loaded; } } else { - $this->image->readImage( $this->file ); + if ( wp_is_stream( $this->file ) ) { + // Due to reports of issues with streams with `Imagick::readImageFile()`, uses `Imagick::readImageBlob()` instead. + $this->image->readImageBlob( file_get_contents( $this->file ), $this->file ); + } else { + $this->image->readImage( $this->file ); + } } if ( ! $this->image->valid() ) { @@ -682,8 +688,16 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { $orig_format = $this->image->getImageFormat(); $this->image->setImageFormat( strtoupper( $this->get_extension( $mime_type ) ) ); - $this->make_image( $filename, array( $image, 'writeImage' ), array( $filename ) ); + } catch ( Exception $e ) { + return new WP_Error( 'image_save_error', $e->getMessage(), $filename ); + } + $write_image_result = $this->write_image( $this->image, $filename ); + if ( is_wp_error( $write_image_result ) ) { + return $write_image_result; + } + + try { // Reset original format. $this->image->setImageFormat( $orig_format ); } catch ( Exception $e ) { @@ -705,6 +719,37 @@ class WP_Image_Editor_Imagick extends WP_Image_Editor { ); } + /** + * Writes an image to a file or stream. + * + * @since 5.6 + * + * @param Imagick $image + * @param string $filename The destination filename or stream URL. + * + * @return true|WP_Error + */ + private function write_image( $image, $filename ) { + if ( wp_is_stream( $filename ) ) { + /* + * Due to reports of issues with streams with `Imagick::writeImageFile()` and `Imagick::writeImage()`, copies the blob instead. + * Checks for exact type due to: https://www.php.net/manual/en/function.file-put-contents.php + */ + if ( file_put_contents( $filename, $image->getImageBlob() ) === false ) { + /* translators: %s: PHP function name. */ + return new WP_Error( 'image_save_error', sprintf( __( '%s failed while writing image to stream.' ), 'file_put_contents()' ), $filename ); + } else { + return true; + } + } else { + try { + return $image->writeImage( $filename ); + } catch ( Exception $e ) { + return new WP_Error( 'image_save_error', $e->getMessage(), $filename ); + } + } + } + /** * Streams current image to browser. * diff --git a/wp-includes/class-wp-image-editor.php b/wp-includes/class-wp-image-editor.php index 26a5d0a813..033601ab2a 100644 --- a/wp-includes/class-wp-image-editor.php +++ b/wp-includes/class-wp-image-editor.php @@ -365,9 +365,13 @@ abstract class WP_Image_Editor { $new_ext = strtolower( $extension ? $extension : $ext ); if ( ! is_null( $dest_path ) ) { - $_dest_path = realpath( $dest_path ); - if ( $_dest_path ) { - $dir = $_dest_path; + if ( ! wp_is_stream( $dest_path ) ) { + $_dest_path = realpath( $dest_path ); + if ( $_dest_path ) { + $dir = $_dest_path; + } + } else { + $dir = $dest_path; } } diff --git a/wp-includes/version.php b/wp-includes/version.php index 067b45b0d2..b47db5c179 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.6-alpha-49229'; +$wp_version = '5.6-alpha-49230'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.