wppaste
WordPress

wp_crop_image( string|int $src, int $src_x, int $src_y, int $src_w, int $src_h, int $dst_w, int $dst_h, bool|false $src_abs = false, string|false $dst_file = false ): string|WP_Error

Since
2.1.0
Source
wp-admin/includes/image.php:25
Crops an image to a given size.

Compatibility

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

$srcstring|int
The source file or Attachment ID.
$src_xint
The start x position to crop from.
$src_yint
The start y position to crop from.
$src_wint
The width to crop.
$src_hint
The height to crop.
$dst_wint
The destination width.
$dst_hint
The destination height.
$src_absbool|falseoptional
If the source crop points are absolute.Default: false
$dst_filestring|falseoptional
The destination file to write to.Default: false

Return value

string|WP_Error
New filepath on success, WP_Error on failure.

Performance profile

How much work a call to wp_crop_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

Reads or writes the filesystem via file_exists().

Scaling
Constant

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

Instructions
21–87

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • filesystemfilesystem accessfile_exists()called directly
  • hookthird-party callbacksapply_filters()one call below wp_crop_image()

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

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

WhenInstructionsCalls it makes
!$src && is_wp_error()21wp_get_image_editor(), is_wp_error()
$src && file_exists() && is_wp_error()30get_attached_file(), file_exists(), wp_get_image_editor(), is_wp_error()
!$src35wp_get_image_editor(), is_wp_error(), ->crop(), is_wp_error()
$src && !file_exists() && is_wp_error()35get_attached_file(), file_exists(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error()
$src && file_exists()44get_attached_file(), file_exists(), wp_get_image_editor(), is_wp_error(), ->crop(), is_wp_error()
$src && !file_exists()49get_attached_file(), file_exists(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), ->crop(), is_wp_error()
!$src && !is_wp_error()60–63wp_get_image_editor(), is_wp_error(), ->crop(), is_wp_error(), wp_mkdir_p(), wp_basename(), wp_unique_filename(), ->save(), is_wp_error()
$src && file_exists() && !is_wp_error()69–72get_attached_file(), file_exists(), wp_get_image_editor(), is_wp_error(), ->crop(), is_wp_error(), wp_mkdir_p(), wp_basename(), wp_unique_filename(), ->save(), is_wp_error()
!$src && !is_wp_error()70–73wp_get_image_editor(), is_wp_error(), ->crop(), is_wp_error(), wp_basename(), wp_basename(), wp_mkdir_p(), wp_basename(), wp_unique_filename(), ->save(), is_wp_error()
$src && !file_exists() && !is_wp_error()74–77get_attached_file(), file_exists(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), ->crop(), is_wp_error(), wp_mkdir_p(), wp_basename(), wp_unique_filename(), ->save(), is_wp_error()
$src && file_exists() && !is_wp_error()79–82get_attached_file(), file_exists(), wp_get_image_editor(), is_wp_error(), ->crop(), is_wp_error(), wp_basename(), wp_basename(), wp_mkdir_p(), wp_basename(), wp_unique_filename(), ->save(), is_wp_error()
$src && !file_exists() && !is_wp_error()84–87get_attached_file(), file_exists(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error(), ->crop(), is_wp_error(), wp_basename(), wp_basename(), wp_mkdir_p(), wp_basename(), wp_unique_filename(), ->save(), is_wp_error()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9221–877
8.59221–877
8.49221–87711 fewer instructions than PHP 8.3
8.310323–987
8.210323–987
8.110323–987
7.410323–987

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

Used by · 4

Source code

function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {	$src_file = $src;	if ( is_numeric( $src ) ) { // Handle int as attachment ID.		$src_file = get_attached_file( $src ); 		if ( ! file_exists( $src_file ) ) {			/*			 * If the file doesn't exist, attempt a URL fopen on the src link.			 * This can occur with certain file replication plugins.			 */			$src = _load_image_to_edit_path( $src, 'full' );		} else {			$src = $src_file;		}	} 	$editor = wp_get_image_editor( $src );	if ( is_wp_error( $editor ) ) {		return $editor;	} 	$src = $editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs );	if ( is_wp_error( $src ) ) {		return $src;	} 	if ( ! $dst_file ) {		$dst_file = str_replace( wp_basename( $src_file ), 'cropped-' . wp_basename( $src_file ), $src_file );	} 	/*	 * The directory containing the original file may no longer exist when	 * using a replication plugin.	 */	wp_mkdir_p( dirname( $dst_file ) ); 	$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), wp_basename( $dst_file ) ); 	$result = $editor->save( $dst_file );	if ( is_wp_error( $result ) ) {		return $result;	} 	if ( ! empty( $result['path'] ) ) {		return $result['path'];	} 	return $dst_file;}

Changelog

Introduced in 2.1.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.7.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.