stream_preview_image( int $post_id ): bool
- Since
- 2.9.0
- Source
wp-admin/includes/image-edit.php:775
$_REQUEST['history'].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
$post_idint- Attachment post ID.
Return value
bool- True on success, false on failure.
Performance profile
How much work a call to stream_preview_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
- Heavy
- Scaling
- Constant
- Instructions
- 20–70
- Plugin surface
- None
- Called by
- 1
Reaches the database via get_post().
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 73.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - serializeserialisation
json_decode()called directly - hookthird-party callbacks
apply_filters()one call below stream_preview_image()
Further down the call graph this can also reach cache, option 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 · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost stream_preview_image() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_wp_error() | 20 | get_post(), wp_raise_memory_limit(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error() |
empty($value) | 48 | get_post(), wp_raise_memory_limit(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), ->get_size(), _image_get_preview_ratio(), ->resize(), is_wp_error() |
empty($value) | 53 | get_post(), wp_raise_memory_limit(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), image_edit_apply_changes(), ->get_size(), _image_get_preview_ratio(), ->resize(), is_wp_error() |
!is_wp_error() && empty($value) | 55 | get_post(), wp_raise_memory_limit(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), ->get_size(), _image_get_preview_ratio(), ->resize(), is_wp_error(), wp_stream_image() |
!empty($value) | 58 | get_post(), wp_raise_memory_limit(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), wp_unslash(), json_decode(), ->get_size(), _image_get_preview_ratio(), ->resize(), is_wp_error() |
!is_wp_error() && empty($value) | 60 | get_post(), wp_raise_memory_limit(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), image_edit_apply_changes(), ->get_size(), _image_get_preview_ratio(), ->resize(), is_wp_error(), wp_stream_image() |
!empty($value) | 63 | get_post(), wp_raise_memory_limit(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), wp_unslash(), json_decode(), image_edit_apply_changes(), ->get_size(), _image_get_preview_ratio(), ->resize(), is_wp_error() |
!is_wp_error() && !empty($value) | 65 | get_post(), wp_raise_memory_limit(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), wp_unslash(), json_decode(), ->get_size(), _image_get_preview_ratio(), ->resize(), is_wp_error(), wp_stream_image() |
!is_wp_error() && !empty($value) | 70 | get_post(), wp_raise_memory_limit(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), wp_unslash(), json_decode(), image_edit_apply_changes(), ->get_size(), _image_get_preview_ratio(), ->resize(), is_wp_error(), wp_stream_image() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 72 | 20–69 | 4 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 73 | 20–70 | 4 | |
| 8.4 | 73 | 20–70 | 4 | 8 fewer instructions than PHP 8.3 |
| 8.3 | 81 | 20–78 | 4 | |
| 8.2 | 81 | 20–78 | 4 | |
| 8.1 | 81 | 20–78 | 4 | |
| 7.4 | 81 | 20–78 | 4 |
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.
Uses · 9
- get_post()Retrieves post data given a post ID or post object.
- wp_raise_memory_limit()Attempts to raise the PHP memory limit for memory intensive processes.
- wp_get_image_editor()Returns a WP_Image_Editor instance and loads file into it.
- _load_image_to_edit_path()Retrieves the path or URL of an attachment's attached file.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
- image_edit_apply_changes()Performs group of changes on Editor specified.
- _image_get_preview_ratio()Image preview ratio. Internal use only.
- wp_stream_image()Streams image in WP_Image_Editor to browser.
Used by · 1
- wp_ajax_imgedit_preview()Handles image editor previews via AJAX.
Source code
function stream_preview_image( $post_id ) { $post = get_post( $post_id ); wp_raise_memory_limit( 'admin' ); $img = wp_get_image_editor( _load_image_to_edit_path( $post_id ) ); if ( is_wp_error( $img ) ) { return false; } $changes = ! empty( $_REQUEST['history'] ) ? json_decode( wp_unslash( $_REQUEST['history'] ) ) : null; if ( $changes ) { $img = image_edit_apply_changes( $img, $changes ); } // Scale the image. $size = $img->get_size(); $w = $size['width']; $h = $size['height']; $ratio = _image_get_preview_ratio( $w, $h ); $w2 = max( 1, $w * $ratio ); $h2 = max( 1, $h * $ratio ); if ( is_wp_error( $img->resize( $w2, $h2 ) ) ) { return false; } return wp_stream_image( $img, $post->post_mime_type, $post_id );}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 7.1.0 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.