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
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
- Scaling
- Constant
- Instructions
- 21–87
- Plugin surface
- None
- Called by
- 4
Reads or writes the filesystem via file_exists().
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 92.
Nothing here hands control to plugin code.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- filesystemfilesystem access
file_exists()called directly - hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
!$src && is_wp_error() | 21 | wp_get_image_editor(), is_wp_error() |
$src && file_exists() && is_wp_error() | 30 | get_attached_file(), file_exists(), wp_get_image_editor(), is_wp_error() |
!$src | 35 | wp_get_image_editor(), is_wp_error(), ->crop(), is_wp_error() |
$src && !file_exists() && is_wp_error() | 35 | get_attached_file(), file_exists(), _load_image_to_edit_path(), wp_get_image_editor(), is_wp_error() |
$src && file_exists() | 44 | get_attached_file(), file_exists(), wp_get_image_editor(), is_wp_error(), ->crop(), is_wp_error() |
$src && !file_exists() | 49 | get_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–63 | 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() | 69–72 | get_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–73 | 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() | 74–77 | get_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–82 | get_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–87 | get_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 92 | 21–87 | 7 | |
| 8.5 | 92 | 21–87 | 7 | |
| 8.4 | 92 | 21–87 | 7 | 11 fewer instructions than PHP 8.3 |
| 8.3 | 103 | 23–98 | 7 | |
| 8.2 | 103 | 23–98 | 7 | |
| 8.1 | 103 | 23–98 | 7 | |
| 7.4 | 103 | 23–98 | 7 |
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
- get_attached_file()Retrieves attached file path based on attachment ID.
- _load_image_to_edit_path()Retrieves the path or URL of an attachment's attached file.
- wp_get_image_editor()Returns a WP_Image_Editor instance and loads file into it.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_basename()i18n-friendly version of basename().
- wp_mkdir_p()Recursive directory creation based on full path.
- wp_unique_filename()Gets a filename that is sanitized and unique for the given directory.
Used by · 4
- Custom_Image_Header::ajax_header_crop()Gets attachment uploaded by Media Manager, crops it, then saves it as a new object. Returns JSON-encoded object details.
- Custom_Image_Header::step_2()Displays second step of custom header image page.
- Custom_Image_Header::step_3()Displays third step of custom header image page.
- wp_ajax_crop_image()Handles cropping an image via AJAX.
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.
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-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.