image_downsize

Scale an image to fit a particular size (such as 'thumb' or 'medium').

Description

(false|array) image_downsize( (int) $id, (string) $size = 'medium' ); 

Array with image url, width, height, and whether is intermediate size, in that order is returned on success is returned. $is_intermediate is true if $url is a resized image, false if it is the original.

The URL might be the original image, or it might be a resized version. This function won't create a new resized copy, it will just return an already resized one if it exists.

A plugin may use the filter to hook into and offer image resizing services for images. The hook must return an array with the same elements that are returned in the function. The first element being the URL to the new image that was resized.

Returns (false|array)

Array containing the image URL, width, height, and boolean for whether the image is an intermediate size. False on failure.

Parameters (2)

0. $id (int)
Attachment ID for image.
1. $size — Optional. (string) => 'medium'
Image size to scale to. Accepts any valid image size, or an array of width and height values in pixels (in that order). Default medium..

Usage

  1. if ( !function_exists( 'image_downsize' ) ) { 
  2. require_once ABSPATH . WPINC . '/media.php'; 
  3.  
  4. // Attachment ID for image. 
  5. $id = -1; 
  6. $size = 'medium'; 
  7.  
  8. // NOTICE! Understand what this does before running. 
  9. $result = image_downsize($id, $size); 
  10.  

Defined (1)

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

/wp-includes/media.php  
  1. function image_downsize( $id, $size = 'medium' ) { 
  2. $is_image = wp_attachment_is_image( $id ); 
  3.  
  4. /** 
  5. * Filters whether to preempt the output of image_downsize(). 
  6. * Passing a truthy value to the filter will effectively short-circuit 
  7. * down-sizing the image, returning that value as output instead. 
  8. * @since 2.5.0 
  9. * @param bool $downsize Whether to short-circuit the image downsize. Default false. 
  10. * @param int $id Attachment ID for image. 
  11. * @param array|string $size Size of image. Image size or array of width and height values (in that order). 
  12. * Default 'medium'. 
  13. */ 
  14. if ( $out = apply_filters( 'image_downsize', false, $id, $size ) ) { 
  15. return $out; 
  16.  
  17. $img_url = wp_get_attachment_url($id); 
  18. $width = $height = 0; 
  19. $is_intermediate = false; 
  20. $img_url_basename = wp_basename($img_url); 
  21.  
  22. // If the file isn't an image, attempt to replace its URL with a rendered image from its meta. 
  23. // Otherwise, a non-image type could be returned. 
  24. if ( ! $is_image ) { 
  25. if ( ! empty( $meta['sizes'] ) ) { 
  26. $img_url = str_replace( $img_url_basename, $meta['sizes']['full']['file'], $img_url ); 
  27. $img_url_basename = $meta['sizes']['full']['file']; 
  28. $width = $meta['sizes']['full']['width']; 
  29. $height = $meta['sizes']['full']['height']; 
  30. } else { 
  31. return false; 
  32.  
  33. // try for a new style intermediate size 
  34. if ( $intermediate = image_get_intermediate_size($id, $size) ) { 
  35. $img_url = str_replace($img_url_basename, $intermediate['file'], $img_url); 
  36. $width = $intermediate['width']; 
  37. $height = $intermediate['height']; 
  38. $is_intermediate = true; 
  39. elseif ( $size == 'thumbnail' ) { 
  40. // fall back to the old thumbnail 
  41. if ( ($thumb_file = wp_get_attachment_thumb_file($id)) && $info = getimagesize($thumb_file) ) { 
  42. $img_url = str_replace($img_url_basename, wp_basename($thumb_file), $img_url); 
  43. $width = $info[0]; 
  44. $height = $info[1]; 
  45. $is_intermediate = true; 
  46. if ( !$width && !$height && isset( $meta['width'], $meta['height'] ) ) { 
  47. // any other type: use the real image 
  48. $width = $meta['width']; 
  49. $height = $meta['height']; 
  50.  
  51. if ( $img_url) { 
  52. // we have the actual image size, but might need to further constrain it if content_width is narrower 
  53. list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size ); 
  54.  
  55. return array( $img_url, $width, $height, $is_intermediate ); 
  56. return false; 
  57.