wppaste
WordPress

load_image_to_edit( int $attachment_id, string $mime_type, string|int[] $size = 'full' ): resource|GdImage|false

Since
2.9.0
Source
wp-admin/includes/image.php:1133
Loads an image resource for editing.

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

$attachment_idint
Attachment ID.
$mime_typestring
Image mime type.
$sizestring|int[]optional
Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default 'full'.Default: 'full'

Return value

resource|GdImage|false
The resulting image resource or GdImage instance on success, false on failure.

Performance profile

How much work a call to load_image_to_edit() 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
11–57

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

Plugin surface
1 hook

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

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

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 · 25 distinct outcomes

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

WhenInstructionsCalls it makes
empty($filepath)11_load_image_to_edit_path()
!empty($filepath) && !is_gd_image()17–26_load_image_to_edit_path(), is_gd_image()
!empty($filepath) && !is_gd_image()21–23_load_image_to_edit_path(), imagecreatefromjpeg(), is_gd_image()
!empty($filepath) && !is_gd_image()21–25_load_image_to_edit_path(), imagecreatefrompng(), is_gd_image()
!empty($filepath) && !is_gd_image()21–27_load_image_to_edit_path(), imagecreatefromgif(), is_gd_image()
!empty($filepath) && !function_exists() && !is_gd_image()21–29_load_image_to_edit_path(), function_exists(), is_gd_image()
!empty($filepath) && function_exists() && !is_gd_image()26–34_load_image_to_edit_path(), function_exists(), imagecreatefromwebp(), is_gd_image()
!empty($filepath) && is_gd_image() && !function_exists()28–37_load_image_to_edit_path(), is_gd_image(), apply_filters(), function_exists()
!empty($filepath) && is_gd_image() && !function_exists()32–34_load_image_to_edit_path(), imagecreatefromjpeg(), is_gd_image(), apply_filters(), function_exists()
!empty($filepath) && is_gd_image() && !function_exists()32–36_load_image_to_edit_path(), imagecreatefrompng(), is_gd_image(), apply_filters(), function_exists()
!empty($filepath) && is_gd_image() && !function_exists()32–38_load_image_to_edit_path(), imagecreatefromgif(), is_gd_image(), apply_filters(), function_exists()
!empty($filepath) && !function_exists() && is_gd_image()32–40_load_image_to_edit_path(), function_exists(), is_gd_image(), apply_filters(), function_exists()
13 further outcomes, up to 57 instructions
!empty($filepath) && is_gd_image()32–41_load_image_to_edit_path(), is_gd_image(), apply_filters(), function_exists(), function_exists()
!empty($filepath) && is_gd_image()36–38_load_image_to_edit_path(), imagecreatefromjpeg(), is_gd_image(), apply_filters(), function_exists(), function_exists()
!empty($filepath) && is_gd_image()36–40_load_image_to_edit_path(), imagecreatefrompng(), is_gd_image(), apply_filters(), function_exists(), function_exists()
!empty($filepath) && is_gd_image()36–42_load_image_to_edit_path(), imagecreatefromgif(), is_gd_image(), apply_filters(), function_exists(), function_exists()
!empty($filepath) && is_gd_image()36–44_load_image_to_edit_path(), function_exists(), is_gd_image(), apply_filters(), function_exists(), function_exists()
!empty($filepath) && is_gd_image()37–45_load_image_to_edit_path(), function_exists(), imagecreatefromwebp(), is_gd_image(), apply_filters(), function_exists()
!empty($filepath) && is_gd_image() && function_exists()40–49_load_image_to_edit_path(), is_gd_image(), apply_filters(), function_exists(), function_exists(), imagealphablending(), imagesavealpha()
!empty($filepath) && is_gd_image()41–49_load_image_to_edit_path(), function_exists(), imagecreatefromwebp(), is_gd_image(), apply_filters(), function_exists(), function_exists()
!empty($filepath) && is_gd_image() && function_exists()44–46_load_image_to_edit_path(), imagecreatefromjpeg(), is_gd_image(), apply_filters(), function_exists(), function_exists(), imagealphablending(), imagesavealpha()
!empty($filepath) && is_gd_image() && function_exists()44–48_load_image_to_edit_path(), imagecreatefrompng(), is_gd_image(), apply_filters(), function_exists(), function_exists(), imagealphablending(), imagesavealpha()
!empty($filepath) && is_gd_image() && function_exists()44–50_load_image_to_edit_path(), imagecreatefromgif(), is_gd_image(), apply_filters(), function_exists(), function_exists(), imagealphablending(), imagesavealpha()
!empty($filepath) && is_gd_image()44–52_load_image_to_edit_path(), function_exists(), is_gd_image(), apply_filters(), function_exists(), function_exists(), imagealphablending(), imagesavealpha()
!empty($filepath) && function_exists() && is_gd_image()49–57_load_image_to_edit_path(), function_exists(), imagecreatefromwebp(), is_gd_image(), apply_filters(), function_exists(), function_exists(), imagealphablending(), imagesavealpha()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7511–5710
8.57511–5710
8.47511–5710
8.37511–5710
8.27511–57101 more instruction than PHP 8.1
8.17411–5710
7.47411–5710

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 · 1

One hook fires while load_image_to_edit() runs, in this order:

  1. apply_filters( load_image_to_edit )filterline 1171 (+38 into the body)

    Filters the current image being loaded for editing.

Uses · 3

Source code

function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) {	$filepath = _load_image_to_edit_path( $attachment_id, $size );	if ( empty( $filepath ) ) {		return false;	} 	switch ( $mime_type ) {		case 'image/jpeg':			$image = imagecreatefromjpeg( $filepath );			break;		case 'image/png':			$image = imagecreatefrompng( $filepath );			break;		case 'image/gif':			$image = imagecreatefromgif( $filepath );			break;		case 'image/webp':			$image = false;			if ( function_exists( 'imagecreatefromwebp' ) ) {				$image = imagecreatefromwebp( $filepath );			}			break;		default:			$image = false;			break;	} 	if ( is_gd_image( $image ) ) {		/**		 * Filters the current image being loaded for editing.		 *		 * @since 2.9.0		 *		 * @param resource|GdImage $image         Current image.		 * @param int              $attachment_id Attachment ID.		 * @param string|int[]     $size          Requested image size. Can be any registered image size name, or		 *                                        an array of width and height values in pixels (in that order).		 */		$image = apply_filters( 'load_image_to_edit', $image, $attachment_id, $size ); 		if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {			imagealphablending( $image, false );			imagesavealpha( $image, true );		}	} 	return $image;}

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.9.7 tag, from src/wp-admin/includes/image.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.