wp_ajax_crop_image()
- Since
- 4.3.0
- Source
wp-admin/includes/ajax-actions.php:4009
Compatibility
- WordPress
- since 4.3.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.
Performance profile
How much work a call to wp_ajax_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
- 86–126
- Plugin surface
- 4 hooks
- Called by
- 0
Reaches the database via get_post().
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 179.
Third-party callbacks on 'wp_create_file_in_uploads', 'wp_ajax_crop_image_pre_save', 'wp_ajax_cropped_attachment_metadata' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_post()one call below wp_ajax_crop_image()
Further down the call graph this can also reach 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 · 27 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_ajax_crop_image() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($attachment_id) && !is_wp_error() && $context === false | 86–88 | absint(), check_ajax_referer(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), wp_send_json_error(), get_post_meta(), wp_delete_file(), wp_prepare_attachment_for_js(), wp_send_json_success() |
!empty($attachment_id) && current_user_can() && !is_wp_error() && $context === false | 89–91 | absint(), check_ajax_referer(), current_user_can(), array_map(), wp_crop_image(), is_wp_error(), wp_send_json_error(), get_post_meta(), wp_delete_file(), wp_prepare_attachment_for_js(), wp_send_json_success() |
empty($attachment_id) && $context === false | 89–91 | absint(), check_ajax_referer(), wp_send_json_error(), array_map(), wp_crop_image(), __(), message(), wp_send_json_error(), get_post_meta(), wp_delete_file(), wp_prepare_attachment_for_js(), wp_send_json_success() |
!empty($attachment_id) && !current_user_can() && !is_wp_error() && $context === false | 91–93 | absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), wp_send_json_error(), get_post_meta(), wp_delete_file(), wp_prepare_attachment_for_js(), wp_send_json_success() |
!empty($attachment_id) && current_user_can() && $context === false | 92–94 | absint(), check_ajax_referer(), current_user_can(), array_map(), wp_crop_image(), __(), message(), wp_send_json_error(), get_post_meta(), wp_delete_file(), wp_prepare_attachment_for_js(), wp_send_json_success() |
empty($attachment_id) && is_wp_error() && $context === false | 93–95 | absint(), check_ajax_referer(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), __(), message(), wp_send_json_error(), get_post_meta(), wp_delete_file(), wp_prepare_attachment_for_js(), wp_send_json_success() |
!empty($attachment_id) && !current_user_can() && $context === false | 94–96 | absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), array_map(), wp_crop_image(), __(), message(), wp_send_json_error(), get_post_meta(), wp_delete_file(), wp_prepare_attachment_for_js(), wp_send_json_success() |
!empty($attachment_id) && current_user_can() && is_wp_error() && $context === false | 96–98 | absint(), check_ajax_referer(), current_user_can(), array_map(), wp_crop_image(), is_wp_error(), __(), message(), wp_send_json_error(), get_post_meta(), wp_delete_file(), wp_prepare_attachment_for_js(), wp_send_json_success() |
!empty($attachment_id) && !current_user_can() && is_wp_error() && $context === false | 98–100 | absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), __(), message(), wp_send_json_error(), get_post_meta(), wp_delete_file(), wp_prepare_attachment_for_js(), wp_send_json_success() |
empty($attachment_id) && !is_wp_error() | 106–108 | absint(), check_ajax_referer(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), do_action(), apply_filters(), wp_copy_parent_attachment_properties(), wp_insert_attachment(), wp_generate_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), apply_filters(), wp_prepare_attachment_for_js(), wp_send_json_success() |
!empty($attachment_id) && current_user_can() && !is_wp_error() | 109–111 | absint(), check_ajax_referer(), current_user_can(), array_map(), wp_crop_image(), is_wp_error(), do_action(), apply_filters(), wp_copy_parent_attachment_properties(), wp_insert_attachment(), wp_generate_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), apply_filters(), wp_prepare_attachment_for_js(), wp_send_json_success() |
empty($attachment_id) | 109–111 | absint(), check_ajax_referer(), wp_send_json_error(), array_map(), wp_crop_image(), __(), message(), do_action(), apply_filters(), wp_copy_parent_attachment_properties(), wp_insert_attachment(), wp_generate_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), apply_filters(), wp_prepare_attachment_for_js(), wp_send_json_success() |
15 further outcomes, up to 126 instructions
!empty($attachment_id) && !current_user_can() && !is_wp_error() | 111–113 | absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), do_action(), apply_filters(), wp_copy_parent_attachment_properties(), wp_insert_attachment(), wp_generate_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), apply_filters(), wp_prepare_attachment_for_js(), wp_send_json_success() |
empty($attachment_id) && !is_wp_error() && $context !== false | 112–114 | absint(), check_ajax_referer(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), wp_send_json_error(), get_post_meta(), apply_filters(), wp_copy_parent_attachment_properties(), ->insert_attachment(), wp_prepare_attachment_for_js(), wp_send_json_success() |
!empty($attachment_id) && current_user_can() | 112–114 | absint(), check_ajax_referer(), current_user_can(), array_map(), wp_crop_image(), __(), message(), do_action(), apply_filters(), wp_copy_parent_attachment_properties(), wp_insert_attachment(), wp_generate_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), apply_filters(), wp_prepare_attachment_for_js(), wp_send_json_success() |
empty($attachment_id) && is_wp_error() | 113–115 | absint(), check_ajax_referer(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), __(), message(), do_action(), apply_filters(), wp_copy_parent_attachment_properties(), wp_insert_attachment(), wp_generate_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), apply_filters(), wp_prepare_attachment_for_js(), wp_send_json_success() |
!empty($attachment_id) && !current_user_can() | 114–116 | absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), array_map(), wp_crop_image(), __(), message(), do_action(), apply_filters(), wp_copy_parent_attachment_properties(), wp_insert_attachment(), wp_generate_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), apply_filters(), wp_prepare_attachment_for_js(), wp_send_json_success() |
!empty($attachment_id) && current_user_can() && !is_wp_error() && $context !== false | 115–117 | absint(), check_ajax_referer(), current_user_can(), array_map(), wp_crop_image(), is_wp_error(), wp_send_json_error(), get_post_meta(), apply_filters(), wp_copy_parent_attachment_properties(), ->insert_attachment(), wp_prepare_attachment_for_js(), wp_send_json_success() |
empty($attachment_id) && $context !== false | 115–117 | absint(), check_ajax_referer(), wp_send_json_error(), array_map(), wp_crop_image(), __(), message(), wp_send_json_error(), get_post_meta(), apply_filters(), wp_copy_parent_attachment_properties(), ->insert_attachment(), wp_prepare_attachment_for_js(), wp_send_json_success() |
!empty($attachment_id) && current_user_can() && is_wp_error() | 116–118 | absint(), check_ajax_referer(), current_user_can(), array_map(), wp_crop_image(), is_wp_error(), __(), message(), do_action(), apply_filters(), wp_copy_parent_attachment_properties(), wp_insert_attachment(), wp_generate_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), apply_filters(), wp_prepare_attachment_for_js(), wp_send_json_success() |
!empty($attachment_id) && !current_user_can() && !is_wp_error() && $context !== false | 117–119 | absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), wp_send_json_error(), get_post_meta(), apply_filters(), wp_copy_parent_attachment_properties(), ->insert_attachment(), wp_prepare_attachment_for_js(), wp_send_json_success() |
!empty($attachment_id) && current_user_can() && $context !== false | 118–120 | absint(), check_ajax_referer(), current_user_can(), array_map(), wp_crop_image(), __(), message(), wp_send_json_error(), get_post_meta(), apply_filters(), wp_copy_parent_attachment_properties(), ->insert_attachment(), wp_prepare_attachment_for_js(), wp_send_json_success() |
!empty($attachment_id) && !current_user_can() && is_wp_error() | 118–120 | absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), __(), message(), do_action(), apply_filters(), wp_copy_parent_attachment_properties(), wp_insert_attachment(), wp_generate_attachment_metadata(), apply_filters(), wp_update_attachment_metadata(), apply_filters(), wp_prepare_attachment_for_js(), wp_send_json_success() |
empty($attachment_id) && is_wp_error() && $context !== false | 119–121 | absint(), check_ajax_referer(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), __(), message(), wp_send_json_error(), get_post_meta(), apply_filters(), wp_copy_parent_attachment_properties(), ->insert_attachment(), wp_prepare_attachment_for_js(), wp_send_json_success() |
!empty($attachment_id) && !current_user_can() && $context !== false | 120–122 | absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), array_map(), wp_crop_image(), __(), message(), wp_send_json_error(), get_post_meta(), apply_filters(), wp_copy_parent_attachment_properties(), ->insert_attachment(), wp_prepare_attachment_for_js(), wp_send_json_success() |
!empty($attachment_id) && current_user_can() && is_wp_error() && $context !== false | 122–124 | absint(), check_ajax_referer(), current_user_can(), array_map(), wp_crop_image(), is_wp_error(), __(), message(), wp_send_json_error(), get_post_meta(), apply_filters(), wp_copy_parent_attachment_properties(), ->insert_attachment(), wp_prepare_attachment_for_js(), wp_send_json_success() |
!empty($attachment_id) && !current_user_can() && is_wp_error() && $context !== false | 124–126 | absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), array_map(), wp_crop_image(), is_wp_error(), __(), message(), wp_send_json_error(), get_post_meta(), apply_filters(), wp_copy_parent_attachment_properties(), ->insert_attachment(), wp_prepare_attachment_for_js(), wp_send_json_success() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 179 | 86–126 | 7 | |
| 8.5 | 179 | 86–126 | 7 | |
| 8.4 | 179 | 86–126 | 7 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 182 | 89–129 | 7 | |
| 8.2 | 182 | 89–129 | 7 | |
| 8.1 | 182 | 89–129 | 7 | |
| 7.4 | 182 | 89–129 | 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.
Hooks and filters fired · 5
5 hooks fire while wp_ajax_crop_image() runs, in this order:
- apply_filters( wp_create_file_in_uploads )filterline 4043 (+34 into the body)
Filters the attachment file path after the custom header or background image is set.
- do_action( wp_ajax_crop_image_pre_save )actionline 4069 (+60 into the body)
Fires before a cropped image is saved.
- apply_filters( wp_create_file_in_uploads )filterline 4072 (+63 into the body)
Filters the attachment file path after the custom header or background image is set.
- apply_filters( wp_ajax_cropped_attachment_metadata )filterline 4089 (+80 into the body)
Filters the cropped image attachment metadata.
- apply_filters( wp_ajax_cropped_attachment_id )filterline 4100 (+91 into the body)
Filters the attachment ID for a cropped image.
Uses · 20
- absint()Converts a value to non-negative integer.
- check_ajax_referer()Verifies the Ajax request to prevent processing requests external of the blog.
- current_user_can()Returns whether the current user has the specified capability.
- wp_send_json_error()Sends a JSON response back to an Ajax request, indicating failure.
- wp_crop_image()Crops an image to a given size.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- __()Retrieves the translation of $text.
- get_post_meta()Retrieves a post meta field for the given post ID.
- wp_delete_file()Deletes a file.
- add_filter()Adds a callback function to a filter hook.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_copy_parent_attachment_properties()Copy parent attachment properties to newly cropped image.
Show all 20
- remove_filter()Removes a callback function from a filter hook.
- do_action()Calls the callback functions that have been added to an action hook.
- wp_insert_attachment()Inserts an attachment.
- wp_generate_attachment_metadata()Generates attachment meta data and create image sub-sizes for images.
- wp_update_attachment_metadata()Updates metadata for an attachment.
- wp_send_json_success()Sends a JSON response back to an Ajax request, indicating success.
- wp_prepare_attachment_for_js()Prepares an attachment post object for JS, where it is expected to be JSON-encoded and fit into an Attachment model.
- WP_Site_Icon::__construct()Registers actions and filters.
Source code
function wp_ajax_crop_image() { $attachment_id = absint( $_POST['id'] ); check_ajax_referer( 'image_editor-' . $attachment_id, 'nonce' ); if ( empty( $attachment_id ) || ! current_user_can( 'edit_post', $attachment_id ) ) { wp_send_json_error(); } $context = str_replace( '_', '-', $_POST['context'] ); $data = array_map( 'absint', $_POST['cropDetails'] ); $cropped = wp_crop_image( $attachment_id, $data['x1'], $data['y1'], $data['width'], $data['height'], $data['dst_width'], $data['dst_height'] ); if ( ! $cropped || is_wp_error( $cropped ) ) { wp_send_json_error( array( 'message' => __( 'Image could not be processed.' ) ) ); } switch ( $context ) { case 'site-icon': require_once ABSPATH . 'wp-admin/includes/class-wp-site-icon.php'; $wp_site_icon = new WP_Site_Icon(); // Skip creating a new attachment if the attachment is a Site Icon. if ( get_post_meta( $attachment_id, '_wp_attachment_context', true ) === $context ) { // Delete the temporary cropped file, we don't need it. wp_delete_file( $cropped ); // Additional sizes in wp_prepare_attachment_for_js(). add_filter( 'image_size_names_choose', array( $wp_site_icon, 'additional_sizes' ) ); break; } /** This filter is documented in wp-admin/includes/class-custom-image-header.php */ $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication. // Copy attachment properties. $attachment = wp_copy_parent_attachment_properties( $cropped, $attachment_id, $context ); // Update the attachment. add_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) ); $attachment_id = $wp_site_icon->insert_attachment( $attachment, $cropped ); remove_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) ); // Additional sizes in wp_prepare_attachment_for_js(). add_filter( 'image_size_names_choose', array( $wp_site_icon, 'additional_sizes' ) ); break; default: /** * Fires before a cropped image is saved. * * Allows to add filters to modify the way a cropped image is saved. * * @since 4.3.0 * * @param string $context The Customizer control requesting the cropped image. * @param int $attachment_id The attachment ID of the original image. * @param string $cropped Path to the cropped image file. */ do_action( 'wp_ajax_crop_image_pre_save', $context, $attachment_id, $cropped ); /** This filter is documented in wp-admin/includes/class-custom-image-header.php */ $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication. // Copy attachment properties. $attachment = wp_copy_parent_attachment_properties( $cropped, $attachment_id, $context ); $attachment_id = wp_insert_attachment( $attachment, $cropped ); $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped ); /** * Filters the cropped image attachment metadata. * * @since 4.3.0 * * @see wp_generate_attachment_metadata() * * @param array $metadata Attachment metadata. */Changelog
Introduced in 4.3.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 6.8.8 tag, from
src/wp-admin/includes/ajax-actions.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.