image_resize_dimensions

Retrieves calculated resize dimensions for use in WP_Image_Editor.

Description

(false|array) image_resize_dimensions( (int) $orig_w, (int) $orig_h, (int) $dest_w, (int) $dest_h, (bool) $crop = false ); 

Calculates dimensions and coordinates for a resized image that fits within a specified width and height.

Cropping behavior is dependent on the value of $crop: 1. If false (default), images will not be cropped. 2. If an array in the form of array( x_crop_position, y_crop_position ): 3. If true, images will be cropped to the specified dimensions using center positions.

Returns (false|array)

False on failure. Returned array matches parameters for `imagecopyresampled()`.

Parameters (5)

0. $orig_w (int)
Original width in pixels.
1. $orig_h (int)
Original height in pixels.
2. $dest_w (int)
New width in pixels.
3. $dest_h (int)
New height in pixels.
4. $crop — Optional. (bool) => false
Whether to crop image to specified width and height or resize. An array can specify positioning of the crop area. Default false.

Usage

  1. if ( !function_exists( 'image_resize_dimensions' ) ) { 
  2. require_once ABSPATH . WPINC . '/media.php'; 
  3.  
  4. // Original width in pixels. 
  5. $orig_w = -1; 
  6.  
  7. // Original height in pixels. 
  8. $orig_h = -1; 
  9.  
  10. // New width in pixels. 
  11. $dest_w = -1; 
  12.  
  13. // New height in pixels. 
  14. $dest_h = -1; 
  15.  
  16. // Optional. Whether to crop image to specified width and height or resize. 
  17. // An array can specify positioning of the crop area. Default false. 
  18. $crop = false; 
  19.  
  20. // NOTICE! Understand what this does before running. 
  21. $result = image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop); 
  22.  

Defined (1)

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

/wp-includes/media.php  
  1. function image_resize_dimensions( $orig_w, $orig_h, $dest_w, $dest_h, $crop = false ) { 
  2.  
  3. if ($orig_w <= 0 || $orig_h <= 0) 
  4. return false; 
  5. // at least one of dest_w or dest_h must be specific 
  6. if ($dest_w <= 0 && $dest_h <= 0) 
  7. return false; 
  8.  
  9. /** 
  10. * Filters whether to preempt calculating the image resize dimensions. 
  11. * Passing a non-null value to the filter will effectively short-circuit 
  12. * image_resize_dimensions(), returning that value instead. 
  13. * @since 3.4.0 
  14. * @param null|mixed $null Whether to preempt output of the resize dimensions. 
  15. * @param int $orig_w Original width in pixels. 
  16. * @param int $orig_h Original height in pixels. 
  17. * @param int $dest_w New width in pixels. 
  18. * @param int $dest_h New height in pixels. 
  19. * @param bool|array $crop Whether to crop image to specified width and height or resize. 
  20. * An array can specify positioning of the crop area. Default false. 
  21. */ 
  22. $output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop ); 
  23. if ( null !== $output ) 
  24. return $output; 
  25.  
  26. if ( $crop ) { 
  27. // crop the largest possible portion of the original image that we can size to $dest_w x $dest_h 
  28. $aspect_ratio = $orig_w / $orig_h; 
  29. $new_w = min($dest_w, $orig_w); 
  30. $new_h = min($dest_h, $orig_h); 
  31.  
  32. if ( ! $new_w ) { 
  33. $new_w = (int) round( $new_h * $aspect_ratio ); 
  34.  
  35. if ( ! $new_h ) { 
  36. $new_h = (int) round( $new_w / $aspect_ratio ); 
  37.  
  38. $size_ratio = max($new_w / $orig_w, $new_h / $orig_h); 
  39.  
  40. $crop_w = round($new_w / $size_ratio); 
  41. $crop_h = round($new_h / $size_ratio); 
  42.  
  43. if ( ! is_array( $crop ) || count( $crop ) !== 2 ) { 
  44. $crop = array( 'center', 'center' ); 
  45.  
  46. list( $x, $y ) = $crop; 
  47.  
  48. if ( 'left' === $x ) { 
  49. $s_x = 0; 
  50. } elseif ( 'right' === $x ) { 
  51. $s_x = $orig_w - $crop_w; 
  52. } else { 
  53. $s_x = floor( ( $orig_w - $crop_w ) / 2 ); 
  54.  
  55. if ( 'top' === $y ) { 
  56. $s_y = 0; 
  57. } elseif ( 'bottom' === $y ) { 
  58. $s_y = $orig_h - $crop_h; 
  59. } else { 
  60. $s_y = floor( ( $orig_h - $crop_h ) / 2 ); 
  61. } else { 
  62. // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box 
  63. $crop_w = $orig_w; 
  64. $crop_h = $orig_h; 
  65.  
  66. $s_x = 0; 
  67. $s_y = 0; 
  68.  
  69. list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h ); 
  70.  
  71. // if the resulting image would be the same size or larger we don't want to resize it 
  72. if ( $new_w >= $orig_w && $new_h >= $orig_h && $dest_w != $orig_w && $dest_h != $orig_h ) { 
  73. return false; 
  74.  
  75. // the return array matches the parameters to imagecopyresampled() 
  76. // int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h 
  77. return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h ); 
  78.