From ce86afd0760d1ccbf3ecc91fa04bf7fbfc12125a Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers <359867+desrosj@users.noreply.github.com> Date: Wed, 4 Nov 2026 16:45:37 -0500 Subject: [PATCH 1/3] Media: Check the result of writing an image to a stream in `WP_Image_Editor::make_image()`. When saving an image to a stream, `make_image()` ignored the return values of `fwrite()` and `fclose()`, so a save that failed to write the image still reported success. This is used by `WP_Image_Editor_GD` for stream destinations. Some stream wrappers, such as those for remote storage, only send the data when the stream is flushed or closed. As of PHP 8.6, `fclose()` returns `false` when flushing or closing a stream fails, which is the only way such a failure can be detected. `make_image()` now returns `false` when the image is not fully written or the stream cannot be closed. This matches `WP_Image_Editor_Imagick::write_image()`, which already checks the return value of `file_put_contents()`. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011Y86gwJZGdkh78pggUy4zs --- src/wp-includes/class-wp-image-editor.php | 15 +++- tests/phpunit/tests/image/editorGd.php | 83 +++++++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-image-editor.php b/src/wp-includes/class-wp-image-editor.php index 90a9f12cf16b3..7667ad40fc2ee 100644 --- a/src/wp-includes/class-wp-image-editor.php +++ b/src/wp-includes/class-wp-image-editor.php @@ -559,6 +559,7 @@ public function maybe_exif_rotate() { * Either calls editor's save function or handles file as a stream. * * @since 3.5.0 + * @since 7.2.0 Returns false when the image cannot be fully written to a stream. * * @param string $filename * @param callable $callback @@ -586,8 +587,18 @@ protected function make_image( $filename, $callback, $arguments ) { return false; } - fwrite( $fp, $contents ); - fclose( $fp ); + $bytes_written = fwrite( $fp, $contents ); + + /* + * Some stream wrappers only send the data when the stream is flushed or closed (for example, when + * uploading to remote storage), so a failure to close the stream means the image was not saved. + * As of PHP 8.6, fclose() returns false when flushing or closing a stream fails. + */ + $closed = fclose( $fp ); + + if ( strlen( $contents ) !== $bytes_written || ! $closed ) { + $result = false; + } } if ( $stream ) { diff --git a/tests/phpunit/tests/image/editorGd.php b/tests/phpunit/tests/image/editorGd.php index e1763a7d5dd3e..ca8d5502defa4 100644 --- a/tests/phpunit/tests/image/editorGd.php +++ b/tests/phpunit/tests/image/editorGd.php @@ -8,6 +8,7 @@ * @group wp-image-editor-gd */ require_once __DIR__ . '/base.php'; +require_once DIR_TESTROOT . '/includes/class-wp-test-stream.php'; class Tests_Image_Editor_GD extends WP_Image_UnitTestCase { @@ -674,4 +675,86 @@ public function test_image_non_existent_extension() { $this->assertTrue( $loaded ); } + + /** + * Tests that images can be saved to a stream. + * + * @covers WP_Image_Editor::make_image + */ + public function test_save_to_stream() { + $saved = $this->save_to_stream( 'wptestgd', 'WP_Test_Stream' ); + + $this->assertNotWPError( $saved ); + $this->assertSame( 'wptestgd://Tests_Image_Editor_GD/write.jpg', $saved['path'] ); + $this->assertNotEmpty( WP_Test_Stream::$data['Tests_Image_Editor_GD']['/write.jpg'], 'The image should have been written to the stream.' ); + } + + /** + * Tests that saving fails when the image cannot be fully written to the stream. + * + * @covers WP_Image_Editor::make_image + */ + public function test_save_to_stream_fails_when_write_fails() { + $saved = $this->save_to_stream( 'wptestgdwritefail', 'WP_Test_Stream_GD_Write_Failure' ); + + $this->assertWPError( $saved ); + $this->assertSame( 'image_save_error', $saved->get_error_code() ); + } + + /** + * Tests that saving fails when the stream cannot be flushed. + * + * PHP 8.6 is the first version where fclose() reports flush and close failures. + * + * @requires PHP >= 8.6 + * + * @covers WP_Image_Editor::make_image + */ + public function test_save_to_stream_fails_when_flush_fails() { + $saved = $this->save_to_stream( 'wptestgdflushfail', 'WP_Test_Stream_GD_Flush_Failure' ); + + $this->assertWPError( $saved ); + $this->assertSame( 'image_save_error', $saved->get_error_code() ); + } + + /** + * Loads a JPEG image with the GD editor and saves it to a stream. + * + * @param string $protocol The stream protocol to register. + * @param string $wrapper_class The stream wrapper class to register for the protocol. + * @return array|WP_Error The result of WP_Image_Editor_GD::save(). + */ + private function save_to_stream( $protocol, $wrapper_class ) { + stream_wrapper_register( $protocol, $wrapper_class ); + WP_Test_Stream::$data = array(); + + $gd_image_editor = new WP_Image_Editor_GD( DIR_TESTDATA . '/images/waffles.jpg' ); + $loaded = $gd_image_editor->load(); + + $saved = $gd_image_editor->save( $protocol . '://Tests_Image_Editor_GD/write.jpg', 'image/jpeg' ); + + stream_wrapper_unregister( $protocol ); + + $this->assertTrue( $loaded ); + + return $saved; + } +} + +/** + * A stream wrapper that fails to write any data. + */ +class WP_Test_Stream_GD_Write_Failure extends WP_Test_Stream { + public function stream_write( $data ) { + return 0; + } +} + +/** + * A stream wrapper that fails to flush, like a remote storage wrapper whose upload fails. + */ +class WP_Test_Stream_GD_Flush_Failure extends WP_Test_Stream { + public function stream_flush() { + return false; + } } From 55cbbe37ea067ec9e19428c23d4a60cd25c95989 Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers <359867+desrosj@users.noreply.github.com> Date: Thu, 5 Nov 2026 13:30:15 -0500 Subject: [PATCH 2/3] Media: Use `file_put_contents()` when writing an image to a stream. Replaces the `fopen()`, `fwrite()`, and `fclose()` calls in `WP_Image_Editor::make_image()` with a single `file_put_contents()` call, matching `WP_Image_Editor_Imagick::write_image()`. `file_put_contents()` returns `false` when the stream cannot be opened or the image cannot be fully written, and as of PHP 8.6, when flushing or closing the stream fails. That covers the same failures as the separate byte count and `fclose()` checks, using the length check PHP itself maintains. An incomplete write now also triggers the PHP warning that `file_put_contents()` raises, as it already does for Imagick. `test_save_to_stream_fails_when_write_fails()` captures and asserts that warning. Co-Authored-By: Claude Opus 5.5 --- src/wp-includes/class-wp-image-editor.php | 20 +++++--------------- tests/phpunit/tests/image/editorGd.php | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/wp-includes/class-wp-image-editor.php b/src/wp-includes/class-wp-image-editor.php index 7667ad40fc2ee..213bcdde67c54 100644 --- a/src/wp-includes/class-wp-image-editor.php +++ b/src/wp-includes/class-wp-image-editor.php @@ -580,23 +580,13 @@ protected function make_image( $filename, $callback, $arguments ) { if ( $result && $stream ) { $contents = ob_get_contents(); - $fp = fopen( $filename, 'w' ); - - if ( ! $fp ) { - ob_end_clean(); - return false; - } - - $bytes_written = fwrite( $fp, $contents ); - /* - * Some stream wrappers only send the data when the stream is flushed or closed (for example, when - * uploading to remote storage), so a failure to close the stream means the image was not saved. - * As of PHP 8.6, fclose() returns false when flushing or closing a stream fails. + * file_put_contents() returns false when the stream cannot be opened or the image cannot be fully written. + * As of PHP 8.6, it also returns false when flushing or closing the stream fails. Some stream wrappers only + * send the data at that point (for example, when uploading to remote storage), so the image was not saved. + * This matches how WP_Image_Editor_Imagick::write_image() writes to streams. */ - $closed = fclose( $fp ); - - if ( strlen( $contents ) !== $bytes_written || ! $closed ) { + if ( false === file_put_contents( $filename, $contents ) ) { $result = false; } } diff --git a/tests/phpunit/tests/image/editorGd.php b/tests/phpunit/tests/image/editorGd.php index ca8d5502defa4..1df7f5a570be0 100644 --- a/tests/phpunit/tests/image/editorGd.php +++ b/tests/phpunit/tests/image/editorGd.php @@ -695,8 +695,24 @@ public function test_save_to_stream() { * @covers WP_Image_Editor::make_image */ public function test_save_to_stream_fails_when_write_fails() { - $saved = $this->save_to_stream( 'wptestgdwritefail', 'WP_Test_Stream_GD_Write_Failure' ); + // file_put_contents() warns about the incomplete write. $this->expectWarning() is deprecated in PHPUnit 10. + $warnings = array(); + set_error_handler( + static function ( int $errno, string $errstr ) use ( &$warnings ) { + $warnings[] = $errstr; + return true; + }, + E_WARNING + ); + + try { + $saved = $this->save_to_stream( 'wptestgdwritefail', 'WP_Test_Stream_GD_Write_Failure' ); + } finally { + restore_error_handler(); + } + $this->assertCount( 1, $warnings, 'Expected one warning about the incomplete write.' ); + $this->assertStringContainsString( 'bytes written', $warnings[0] ); $this->assertWPError( $saved ); $this->assertSame( 'image_save_error', $saved->get_error_code() ); } From a491ac8c0acb5c16425bdf3c982539644c29066c Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers <359867+desrosj@users.noreply.github.com> Date: Thu, 5 Nov 2026 20:37:16 -0500 Subject: [PATCH 3/3] Improve the precision of the stream write inline comment. Co-Authored-By: Claude Opus 5.5 --- src/wp-includes/class-wp-image-editor.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-image-editor.php b/src/wp-includes/class-wp-image-editor.php index 213bcdde67c54..df07c4229b623 100644 --- a/src/wp-includes/class-wp-image-editor.php +++ b/src/wp-includes/class-wp-image-editor.php @@ -581,10 +581,12 @@ protected function make_image( $filename, $callback, $arguments ) { $contents = ob_get_contents(); /* - * file_put_contents() returns false when the stream cannot be opened or the image cannot be fully written. - * As of PHP 8.6, it also returns false when flushing or closing the stream fails. Some stream wrappers only - * send the data at that point (for example, when uploading to remote storage), so the image was not saved. - * This matches how WP_Image_Editor_Imagick::write_image() writes to streams. + * file_put_contents() returns false if the stream cannot be opened, or if fewer + * bytes are written than the image contains. + * + * As of PHP 8.6, false is also returned when flushing or closing the stream fails. + * For stream wrappers that only send data on flush or close, such a failure means + * the image was never saved. */ if ( false === file_put_contents( $filename, $contents ) ) { $result = false;