image_constrain_size_for_editor

Scale down the default size of an image.

Description

(array) image_constrain_size_for_editor( (int) $width, (int) $height, (string) $size = 'medium', (null) $context = null ); 

This is so that the image is a better fit for the editor and theme.

The $size parameter accepts either an array or a string. The supported string values are thumb. or thumbnail for the given thumbnail size or defaults at 128 width and 96 height in pixels. Also supported for the string value is 'medium', medium_large and full. The full isn't actually supported, but any value other than the supported will result in the content_width size or 500 if that is not set.

Finally, there is a filter named , that will be called on the calculated array for width and height, respectively. The second parameter will be the value that was in the $size parameter. The returned type for the hook is an array with the width as the first element and the height as the second element.

Returns (array)

Width and height of what the result image should resize to.

Parameters (4)

0. $width (int)
Width of the image in pixels.
1. $height (int)
Height of the image in pixels.
2. $size — Optional. (string) => 'medium'
Image size. Accepts any valid image size, or an array of width and height values in pixels (in that order). Default medium..
3. $context — Optional. (null) => null
Could be display (like in a theme) or edit (like inserting into an editor). Default null.

Usage

  1. if ( !function_exists( 'image_constrain_size_for_editor' ) ) { 
  2. require_once ABSPATH . WPINC . '/media.php'; 
  3.  
  4. // Width of the image in pixels. 
  5. $width = -1; 
  6.  
  7. // Height of the image in pixels. 
  8. $height = -1; 
  9. $size = 'medium'; 
  10.  
  11. // Optional. Could be 'display' (like in a theme) or 'edit' 
  12. // (like inserting into an editor). Default null. 
  13. $context = null; 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = image_constrain_size_for_editor($width, $height, $size, $context); 
  17.  

Defined (1)

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

/wp-includes/media.php  
  1. function image_constrain_size_for_editor( $width, $height, $size = 'medium', $context = null ) { 
  2. global $content_width; 
  3.  
  4. $_wp_additional_image_sizes = wp_get_additional_image_sizes(); 
  5.  
  6. if ( ! $context ) 
  7. $context = is_admin() ? 'edit' : 'display'; 
  8.  
  9. if ( is_array($size) ) { 
  10. $max_width = $size[0]; 
  11. $max_height = $size[1]; 
  12. elseif ( $size == 'thumb' || $size == 'thumbnail' ) { 
  13. $max_width = intval(get_option('thumbnail_size_w')); 
  14. $max_height = intval(get_option('thumbnail_size_h')); 
  15. // last chance thumbnail size defaults 
  16. if ( !$max_width && !$max_height ) { 
  17. $max_width = 128; 
  18. $max_height = 96; 
  19. elseif ( $size == 'medium' ) { 
  20. $max_width = intval(get_option('medium_size_w')); 
  21. $max_height = intval(get_option('medium_size_h')); 
  22.  
  23. elseif ( $size == 'medium_large' ) { 
  24. $max_width = intval( get_option( 'medium_large_size_w' ) ); 
  25. $max_height = intval( get_option( 'medium_large_size_h' ) ); 
  26.  
  27. if ( intval( $content_width ) > 0 ) { 
  28. $max_width = min( intval( $content_width ), $max_width ); 
  29. elseif ( $size == 'large' ) { 
  30. /** 
  31. * We're inserting a large size image into the editor. If it's a really 
  32. * big image we'll scale it down to fit reasonably within the editor 
  33. * itself, and within the theme's content width if it's known. The user 
  34. * can resize it in the editor if they wish. 
  35. */ 
  36. $max_width = intval(get_option('large_size_w')); 
  37. $max_height = intval(get_option('large_size_h')); 
  38. if ( intval($content_width) > 0 ) { 
  39. $max_width = min( intval($content_width), $max_width ); 
  40. } elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) { 
  41. $max_width = intval( $_wp_additional_image_sizes[$size]['width'] ); 
  42. $max_height = intval( $_wp_additional_image_sizes[$size]['height'] ); 
  43. // Only in admin. Assume that theme authors know what they're doing. 
  44. if ( intval( $content_width ) > 0 && 'edit' === $context ) { 
  45. $max_width = min( intval( $content_width ), $max_width ); 
  46. // $size == 'full' has no constraint 
  47. else { 
  48. $max_width = $width; 
  49. $max_height = $height; 
  50.  
  51. /** 
  52. * Filters the maximum image size dimensions for the editor. 
  53. * @since 2.5.0 
  54. * @param array $max_image_size An array with the width as the first element,  
  55. * and the height as the second element. 
  56. * @param string|array $size Size of what the result image should be. 
  57. * @param string $context The context the image is being resized for. 
  58. * Possible values are 'display' (like in a theme) 
  59. * or 'edit' (like inserting into an editor). 
  60. */ 
  61. list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size, $context ); 
  62.  
  63. return wp_constrain_dimensions( $width, $height, $max_width, $max_height );