WP_Image_Editor_GD::load(): true|WP_Error
- Since
- 3.5.0
- Source
wp-includes/class-wp-image-editor-gd.php:90
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
- Scaling
- Constant
- Instructions
- 3–100
- Plugin surface
- None
- Called by
- 0
Reads or writes the filesystem via is_file().
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 162.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- filesystemfilesystem access
is_file()called directly - hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 3 | none |
!is_file() | 21 | is_file(), __() |
| always | 29–32 | is_file(), wp_raise_memory_limit(), file_get_contents(), __() |
!function_exists() && !is_gd_image() | 50–53 | is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), function_exists(), imagecreatefromstring(), is_gd_image(), __() |
function_exists() && !is_gd_image() | 56–59 | is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), imagecreatefromwebp(), is_gd_image(), __() |
!function_exists() && is_gd_image() | 57–60 | is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), function_exists(), imagecreatefromstring(), is_gd_image(), wp_getimagesize(), __() |
!is_gd_image() | 57–60 | is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), function_exists(), wp_get_image_mime(), imagecreatefromstring(), is_gd_image(), __() |
!is_gd_image() | 57–60 | is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), function_exists(), imagecreatefromstring(), is_gd_image(), __() |
!is_gd_image() | 60–63 | is_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–66 | is_file(), wp_raise_memory_limit(), file_get_contents(), function_exists(), wp_get_image_mime(), imagecreatefromwebp(), is_gd_image(), wp_getimagesize(), __() |
is_gd_image() | 64–67 | is_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–67 | is_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–67 | is_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–67 | is_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–70 | is_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–70 | is_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–71 | is_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–73 | is_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–74 | is_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–74 | is_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–74 | is_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–77 | is_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–77 | is_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–77 | is_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–78 | is_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–78 | is_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–81 | is_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–81 | is_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–83 | is_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–84 | is_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–85 | is_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–88 | is_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–89 | is_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–90 | is_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–90 | is_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–93 | is_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–97 | is_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–100 | is_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 162 | 3–100 | 12 | |
| 8.5 | 162 | 3–100 | 12 | |
| 8.4 | 162 | 3–100 | 12 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 165 | 3–103 | 12 | |
| 8.2 | 165 | 3–103 | 12 | |
| 8.1 | 165 | 3–103 | 12 | |
| 7.4 | 165 | 3–103 | 12 |
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
- __()Retrieves the translation of $text.
- wp_raise_memory_limit()Attempts to raise the PHP memory limit for memory intensive processes.
- wp_get_image_mime()Returns the real mime type of an image file.
- is_gd_image()Determines whether the value is an acceptable type for GD image functions.
- wp_getimagesize()Allows PHP's getimagesize() to be debuggable when necessary.
- WP_Error::__construct()Initializes the error.
- WP_Image_Editor_GD::update_size()Sets or updates current image size.
- WP_Image_Editor_GD::set_quality()Sets Image Compression quality on a 1-100% scale. Handles WebP lossless images.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.