wppaste
WordPress

wp_stream_image( WP_Image_Editor $image, string $mime_type, int $attachment_id ): bool

Since
2.9.0
Source
wp-admin/includes/image-edit.php:344
Streams image in WP_Image_Editor to browser.

Compatibility

WordPress
since 2.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$imageWP_Image_Editor
The image editor instance.
$mime_typestring
The mime type of the image.
$attachment_idint
The image's attachment post ID.

Return value

bool
True on success, false on failure.

Performance profile

How much work a call to wp_stream_image() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
19–51

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 106.

Plugin surface
2 hooks

Third-party callbacks on 'image_editor_save_pre', 'image_save_pre' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 8 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_stream_image() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$image instanceof19apply_filters(), ->stream(), is_wp_error()
!($image instanceof)29–39__(), sprintf(), _deprecated_argument()
!($image instanceof) && !function_exists()33–43__(), sprintf(), _deprecated_argument(), function_exists()
!($image instanceof)35–39__(), sprintf(), _deprecated_argument(), header(), imagepng()
!($image instanceof)35–41__(), sprintf(), _deprecated_argument(), header(), imagegif()
!($image instanceof)37–39__(), sprintf(), _deprecated_argument(), header(), imagejpeg()
!($image instanceof) && function_exists()41–49__(), sprintf(), _deprecated_argument(), function_exists(), header(), imagewebp()
!($image instanceof) && function_exists()41–51__(), sprintf(), _deprecated_argument(), function_exists(), header(), imageavif()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10619–5110
8.510619–5110
8.410619–5110
8.310619–5110
8.210619–5110
8.110619–51101 more instruction than PHP 7.4
7.410519–5110

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while wp_stream_image() runs, in this order:

  1. apply_filters( image_editor_save_pre )filterline 355 (+11 into the body)

    Filters the WP_Image_Editor instance for the image to be streamed to the browser.

  2. do_action( image_save_pre )filter_deprecatedline 375 (+31 into the body)

    Filters the GD image resource to be streamed to the browser.

Uses · 5

Used by · 1

  • stream_preview_image()Streams image in post to browser, along with enqueued changes in `$_REQUEST['history']`.

Source code

function wp_stream_image( $image, $mime_type, $attachment_id ) {	if ( $image instanceof WP_Image_Editor ) { 		/**		 * Filters the WP_Image_Editor instance for the image to be streamed to the browser.		 *		 * @since 3.5.0		 *		 * @param WP_Image_Editor $image         The image editor instance.		 * @param int             $attachment_id The attachment post ID.		 */		$image = apply_filters( 'image_editor_save_pre', $image, $attachment_id ); 		if ( is_wp_error( $image->stream( $mime_type ) ) ) {			return false;		} 		return true;	} else {		/* translators: 1: $image, 2: WP_Image_Editor */		_deprecated_argument( __FUNCTION__, '3.5.0', sprintf( __( '%1$s needs to be a %2$s object.' ), '$image', 'WP_Image_Editor' ) ); 		/**		 * Filters the GD image resource to be streamed to the browser.		 *		 * @since 2.9.0		 * @deprecated 3.5.0 Use {@see 'image_editor_save_pre'} instead.		 *		 * @param resource|GdImage $image         Image resource to be streamed.		 * @param int              $attachment_id The attachment post ID.		 */		$image = apply_filters_deprecated( 'image_save_pre', array( $image, $attachment_id ), '3.5.0', 'image_editor_save_pre' ); 		switch ( $mime_type ) {			case 'image/jpeg':				header( 'Content-Type: image/jpeg' );				return imagejpeg( $image, null, 90 );			case 'image/png':				header( 'Content-Type: image/png' );				return imagepng( $image );			case 'image/gif':				header( 'Content-Type: image/gif' );				return imagegif( $image );			case 'image/webp':				if ( function_exists( 'imagewebp' ) ) {					header( 'Content-Type: image/webp' );					return imagewebp( $image, null, 90 );				}				return false;			case 'image/avif':				if ( function_exists( 'imageavif' ) ) {					header( 'Content-Type: image/avif' );					return imageavif( $image, null, 90 );				}				return false;			default:				return false;		}	}}

Changelog

Introduced in 2.9.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-admin/includes/image-edit.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.