wp_ajax_crop_image

Ajax handler for cropping an image.

Description

wp_ajax_crop_image(); 

Usage

  1. if ( !function_exists( 'wp_ajax_crop_image' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/ajax-actions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_ajax_crop_image(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_crop_image() { 
  2. $attachment_id = absint( $_POST['id'] ); 
  3.  
  4. check_ajax_referer( 'image_editor-' . $attachment_id, 'nonce' ); 
  5. if ( ! current_user_can( 'customize' ) ) { 
  6.  
  7. $context = str_replace( '_', '-', $_POST['context'] ); 
  8. $data = array_map( 'absint', $_POST['cropDetails'] ); 
  9. $cropped = wp_crop_image( $attachment_id, $data['x1'], $data['y1'], $data['width'], $data['height'], $data['dst_width'], $data['dst_height'] ); 
  10.  
  11. if ( ! $cropped || is_wp_error( $cropped ) ) { 
  12. wp_send_json_error( array( 'message' => __( 'Image could not be processed.' ) ) ); 
  13.  
  14. switch ( $context ) { 
  15. case 'site-icon': 
  16. require_once ABSPATH . '/wp-admin/includes/class-wp-site-icon.php'; 
  17. $wp_site_icon = new WP_Site_Icon(); 
  18.  
  19. // Skip creating a new attachment if the attachment is a Site Icon. 
  20. if ( get_post_meta( $attachment_id, '_wp_attachment_context', true ) == $context ) { 
  21.  
  22. // Delete the temporary cropped file, we don't need it. 
  23. wp_delete_file( $cropped ); 
  24.  
  25. // Additional sizes in wp_prepare_attachment_for_js(). 
  26. add_filter( 'image_size_names_choose', array( $wp_site_icon, 'additional_sizes' ) ); 
  27. break; 
  28.  
  29. /** This filter is documented in wp-admin/custom-header.php */ 
  30. $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication. 
  31. $object = $wp_site_icon->create_attachment_object( $cropped, $attachment_id ); 
  32. unset( $object['ID'] ); 
  33.  
  34. // Update the attachment. 
  35. add_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) ); 
  36. $attachment_id = $wp_site_icon->insert_attachment( $object, $cropped ); 
  37. remove_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) ); 
  38.  
  39. // Additional sizes in wp_prepare_attachment_for_js(). 
  40. add_filter( 'image_size_names_choose', array( $wp_site_icon, 'additional_sizes' ) ); 
  41. break; 
  42.  
  43. default: 
  44.  
  45. /** 
  46. * Fires before a cropped image is saved. 
  47. * Allows to add filters to modify the way a cropped image is saved. 
  48. * @since 4.3.0 
  49. * @param string $context The Customizer control requesting the cropped image. 
  50. * @param int $attachment_id The attachment ID of the original image. 
  51. * @param string $cropped Path to the cropped image file. 
  52. */ 
  53. do_action( 'wp_ajax_crop_image_pre_save', $context, $attachment_id, $cropped ); 
  54.  
  55. /** This filter is documented in wp-admin/custom-header.php */ 
  56. $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication. 
  57.  
  58. $parent_url = wp_get_attachment_url( $attachment_id ); 
  59. $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url ); 
  60.  
  61. $size = @getimagesize( $cropped ); 
  62. $image_type = ( $size ) ? $size['mime'] : 'image/jpeg'; 
  63.  
  64. $object = array( 
  65. 'post_title' => basename( $cropped ),  
  66. 'post_content' => $url,  
  67. 'post_mime_type' => $image_type,  
  68. 'guid' => $url,  
  69. 'context' => $context,  
  70. ); 
  71.  
  72. $attachment_id = wp_insert_attachment( $object, $cropped ); 
  73. $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped ); 
  74.  
  75. /** 
  76. * Filters the cropped image attachment metadata. 
  77. * @since 4.3.0 
  78. * @param array $metadata Attachment metadata. 
  79. */ 
  80. wp_update_attachment_metadata( $attachment_id, $metadata ); 
  81.  
  82. /** 
  83. * Filters the attachment ID for a cropped image. 
  84. * @since 4.3.0 
  85. * @param int $attachment_id The attachment ID of the cropped image. 
  86. * @param string $context The Customizer control requesting the cropped image. 
  87. */ 
  88. $attachment_id = apply_filters( 'wp_ajax_cropped_attachment_id', $attachment_id, $context ); 
  89.