wppaste
WordPress

WP_Image_Editor_GD::load(): true|WP_Error

Since
3.5.0
Source
wp-includes/class-wp-image-editor-gd.php:90
Loads image from $this->file into new GD Resource.

Compatibility

WordPress
since 3.5.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.

Return value

true|WP_Error
True if loaded successfully; WP_Error on failure.

Performance profile

How much work a call to WP_Image_Editor_GD::load() 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

Reads or writes the filesystem via is_file().

Scaling
Constant

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

Instructions
3–100

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

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

What it touches

  • filesystemfilesystem accessis_file()called directly
  • hookthird-party callbacksapply_filters()one call below WP_Image_Editor_GD::load()

Further down the call graph this can also reach cache, query, option, 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 · 38 distinct outcomes

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

WhenInstructionsCalls it makes
always3none
!is_file()21is_file(), __()
always29–32is_file(), wp_raise_memory_limit(), file_get_contents(), __()
!function_exists() && !is_gd_image()50–53is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), function_exists(), imagecreatefromstring(), is_gd_image(), __()
function_exists() && !is_gd_image()56–59is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), imagecreatefromwebp(), is_gd_image(), __()
!function_exists() && is_gd_image()57–60is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), function_exists(), imagecreatefromstring(), is_gd_image(), wp_getimagesize(), __()
!is_gd_image()57–60is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), function_exists(), wp_get_image_mime(), imagecreatefromstring(), is_gd_image(), __()
!is_gd_image()57–60is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), function_exists(), imagecreatefromstring(), is_gd_image(), __()
!is_gd_image()60–63is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), function_exists(), wp_get_image_mime(), imagecreatefromavif(), is_gd_image(), __()
function_exists() && is_gd_image()63–66is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), imagecreatefromwebp(), is_gd_image(), wp_getimagesize(), __()
is_gd_image()64–67is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), function_exists(), wp_get_image_mime(), imagecreatefromstring(), is_gd_image(), wp_getimagesize(), __()
is_gd_image()64–67is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), function_exists(), imagecreatefromstring(), is_gd_image(), wp_getimagesize(), __()
26 further outcomes, up to 100 instructions
function_exists() && !is_gd_image()64–67is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), function_exists(), wp_get_image_mime(), imagecreatefromstring(), is_gd_image(), __()
!function_exists() && is_gd_image()64–67is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), function_exists(), imagecreatefromstring(), is_gd_image(), wp_getimagesize(), function_exists(), ->update_size(), ->set_quality()
is_gd_image()67–70is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), function_exists(), wp_get_image_mime(), imagecreatefromavif(), is_gd_image(), wp_getimagesize(), __()
function_exists() && !is_gd_image()67–70is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), function_exists(), wp_get_image_mime(), imagecreatefromavif(), is_gd_image(), __()
is_gd_image()68–71is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), function_exists(), imagecreatefromstring(), is_gd_image(), wp_getimagesize(), function_exists(), function_exists(), ->update_size(), ->set_quality()
is_gd_image()70–73is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), imagecreatefromwebp(), is_gd_image(), wp_getimagesize(), function_exists(), ->update_size(), ->set_quality()
function_exists() && is_gd_image()71–74is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), function_exists(), wp_get_image_mime(), imagecreatefromstring(), is_gd_image(), wp_getimagesize(), __()
is_gd_image()71–74is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), function_exists(), wp_get_image_mime(), imagecreatefromstring(), is_gd_image(), wp_getimagesize(), function_exists(), ->update_size(), ->set_quality()
is_gd_image()71–74is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), function_exists(), imagecreatefromstring(), is_gd_image(), wp_getimagesize(), function_exists(), ->update_size(), ->set_quality()
function_exists() && is_gd_image()74–77is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), function_exists(), wp_get_image_mime(), imagecreatefromavif(), is_gd_image(), wp_getimagesize(), __()
is_gd_image()74–77is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), function_exists(), wp_get_image_mime(), imagecreatefromavif(), is_gd_image(), wp_getimagesize(), function_exists(), ->update_size(), ->set_quality()
is_gd_image()74–77is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), imagecreatefromwebp(), is_gd_image(), wp_getimagesize(), function_exists(), function_exists(), ->update_size(), ->set_quality()
is_gd_image()75–78is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), function_exists(), wp_get_image_mime(), imagecreatefromstring(), is_gd_image(), wp_getimagesize(), function_exists(), function_exists(), ->update_size(), ->set_quality()
is_gd_image()75–78is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), function_exists(), imagecreatefromstring(), is_gd_image(), wp_getimagesize(), function_exists(), function_exists(), ->update_size(), ->set_quality()
is_gd_image()78–81is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), function_exists(), wp_get_image_mime(), imagecreatefromavif(), is_gd_image(), wp_getimagesize(), function_exists(), function_exists(), ->update_size(), ->set_quality()
is_gd_image()78–81is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), function_exists(), wp_get_image_mime(), imagecreatefromstring(), is_gd_image(), wp_getimagesize(), function_exists(), ->update_size(), ->set_quality()
is_gd_image()80–83is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), function_exists(), imagecreatefromstring(), is_gd_image(), wp_getimagesize(), function_exists(), function_exists(), imagealphablending(), imagesavealpha(), ->update_size(), ->set_quality()
is_gd_image()81–84is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), function_exists(), wp_get_image_mime(), imagecreatefromavif(), is_gd_image(), wp_getimagesize(), function_exists(), ->update_size(), ->set_quality()
is_gd_image()82–85is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), function_exists(), wp_get_image_mime(), imagecreatefromstring(), is_gd_image(), wp_getimagesize(), function_exists(), function_exists(), ->update_size(), ->set_quality()
is_gd_image()85–88is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), function_exists(), wp_get_image_mime(), imagecreatefromavif(), is_gd_image(), wp_getimagesize(), function_exists(), function_exists(), ->update_size(), ->set_quality()
function_exists() && is_gd_image()86–89is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), imagecreatefromwebp(), is_gd_image(), wp_getimagesize(), function_exists(), function_exists(), imagealphablending(), imagesavealpha(), ->update_size(), ->set_quality()
is_gd_image()87–90is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), function_exists(), wp_get_image_mime(), imagecreatefromstring(), is_gd_image(), wp_getimagesize(), function_exists(), function_exists(), imagealphablending(), imagesavealpha(), ->update_size(), ->set_quality()
is_gd_image()87–90is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), function_exists(), imagecreatefromstring(), is_gd_image(), wp_getimagesize(), function_exists(), function_exists(), imagealphablending(), imagesavealpha(), ->update_size(), ->set_quality()
is_gd_image()90–93is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), function_exists(), wp_get_image_mime(), imagecreatefromavif(), is_gd_image(), wp_getimagesize(), function_exists(), function_exists(), imagealphablending(), imagesavealpha(), ->update_size(), ->set_quality()
function_exists() && is_gd_image()94–97is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), function_exists(), wp_get_image_mime(), imagecreatefromstring(), is_gd_image(), wp_getimagesize(), function_exists(), function_exists(), imagealphablending(), imagesavealpha(), ->update_size(), ->set_quality()
function_exists() && is_gd_image()97–100is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), function_exists(), wp_get_image_mime(), imagecreatefromavif(), is_gd_image(), wp_getimagesize(), function_exists(), function_exists(), imagealphablending(), imagesavealpha(), ->update_size(), ->set_quality()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1623–10012
8.51623–10012
8.41623–100123 fewer instructions than PHP 8.3
8.31653–10312
8.21653–10312
8.11653–10312
7.41653–10312

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

Source code

	public function load() {		if ( $this->image ) {			return true;		} 		if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) ) {			return new WP_Error( 'error_loading_image', __( 'File does not exist?' ), $this->file );		} 		// Set artificially high because GD uses uncompressed images in memory.		wp_raise_memory_limit( 'image' ); 		$file_contents = @file_get_contents( $this->file ); 		if ( ! $file_contents ) {			return new WP_Error( 'error_loading_image', __( 'File does not exist?' ), $this->file );		} 		// Handle WebP and AVIF mime types explicitly, falling back to imagecreatefromstring.		if (			function_exists( 'imagecreatefromwebp' ) && ( 'image/webp' === wp_get_image_mime( $this->file ) )		) {			$this->image = @imagecreatefromwebp( $this->file );		} elseif (			function_exists( 'imagecreatefromavif' ) && ( 'image/avif' === wp_get_image_mime( $this->file ) )		) {			$this->image = @imagecreatefromavif( $this->file );		} else {			$this->image = @imagecreatefromstring( $file_contents );		} 		if ( ! is_gd_image( $this->image ) ) {			return new WP_Error( 'invalid_image', __( 'File is not an image.' ), $this->file );		} 		$size = wp_getimagesize( $this->file ); 		if ( ! $size ) {			return new WP_Error( 'invalid_image', __( 'Could not read image size.' ), $this->file );		} 		if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {			imagealphablending( $this->image, false );			imagesavealpha( $this->image, true );		} 		$this->update_size( $size[0], $size[1] );		$this->mime_type = $size['mime']; 		return $this->set_quality();	}

Changelog

Introduced in 3.5.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 7.1.0 tag, from src/wp-includes/class-wp-image-editor-gd.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.