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
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
- Scaling
- Constant
- Instructions
- 19–51
- Plugin surface
- 2 hooks
- Called by
- 1
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 106.
Third-party callbacks on 'image_editor_save_pre', 'image_save_pre' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
$image instanceof | 19 | apply_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 106 | 19–51 | 10 | |
| 8.5 | 106 | 19–51 | 10 | |
| 8.4 | 106 | 19–51 | 10 | |
| 8.3 | 106 | 19–51 | 10 | |
| 8.2 | 106 | 19–51 | 10 | |
| 8.1 | 106 | 19–51 | 10 | 1 more instruction than PHP 7.4 |
| 7.4 | 105 | 19–51 | 10 |
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:
- 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.
- 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
- apply_filters()Calls the callback functions that have been added to a filter hook.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- _deprecated_argument()Marks a function argument as deprecated and inform when it has been used.
- __()Retrieves the translation of $text.
- apply_filters_deprecated()Fires functions attached to a deprecated filter hook.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.