wp_crop_image

Crop an Image to a given size.

Description

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

Returns (string|WP_Error)

New filepath on success, WP_Error on failure.

Parameters (9)

0. $src (string|int)
The source file or Attachment ID.
1. $src_x (int)
The start x position to crop from.
2. $src_y (int)
The start y position to crop from.
3. $src_w (int)
The width to crop.
4. $src_h (int)
The height to crop.
5. $dst_w (int)
The destination width.
6. $dst_h (int)
The destination height.
7. $src_abs — Optional. (constant) => false
If the source crop points are absolute.
8. $dst_file — Optional. (bool) => false
The destination file to write to.

Usage

  1. if ( !function_exists( 'wp_crop_image' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/image.php'; 
  3.  
  4. // The source file or Attachment ID. 
  5. $src = null; 
  6.  
  7. // The start x position to crop from. 
  8. $src_x = -1; 
  9.  
  10. // The start y position to crop from. 
  11. $src_y = -1; 
  12.  
  13. // The width to crop. 
  14. $src_w = -1; 
  15.  
  16. // The height to crop. 
  17. $src_h = -1; 
  18.  
  19. // The destination width. 
  20. $dst_w = -1; 
  21.  
  22. // The destination height. 
  23. $dst_h = -1; 
  24.  
  25. // Optional. If the source crop points are absolute. 
  26. $src_abs = false; 
  27.  
  28. // Optional. The destination file to write to. 
  29. $dst_file = false; 
  30.  
  31. // NOTICE! Understand what this does before running. 
  32. $result = wp_crop_image($src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs, $dst_file); 
  33.  

Defined (1)

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

/wp-admin/includes/image.php  
  1. function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) { 
  2. $src_file = $src; 
  3. if ( is_numeric( $src ) ) { // Handle int as attachment ID 
  4. $src_file = get_attached_file( $src ); 
  5.  
  6. if ( ! file_exists( $src_file ) ) { 
  7. // If the file doesn't exist, attempt a URL fopen on the src link. 
  8. // This can occur with certain file replication plugins. 
  9. $src = _load_image_to_edit_path( $src, 'full' ); 
  10. } else { 
  11. $src = $src_file; 
  12.  
  13. $editor = wp_get_image_editor( $src ); 
  14. if ( is_wp_error( $editor ) ) 
  15. return $editor; 
  16.  
  17. $src = $editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs ); 
  18. if ( is_wp_error( $src ) ) 
  19. return $src; 
  20.  
  21. if ( ! $dst_file ) 
  22. $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file ); 
  23.  
  24. /** 
  25. * The directory containing the original file may no longer exist when 
  26. * using a replication plugin. 
  27. */ 
  28. wp_mkdir_p( dirname( $dst_file ) ); 
  29.  
  30. $dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) ); 
  31.  
  32. $result = $editor->save( $dst_file ); 
  33. if ( is_wp_error( $result ) ) 
  34. return $result; 
  35.  
  36. return $dst_file;