wp_calculate_image_sizes

Creates a 'sizes' attribute value for an image.

Description

(string|bool) wp_calculate_image_sizes( (array|string) $size, (constant) $image_src = null, (constant) $image_meta = null, (int) $attachment_id = 0 ); 

Returns (string|bool)

A valid source size value for use in a 'sizes' attribute or false.

Parameters (4)

0. $size (array|string)
Image size to retrieve. Accepts any valid image size, or an array of width and height values in pixels (in that order). Default medium..
1. $image_src — Optional. (constant) => null
The URL to the image file. Default null.
2. $image_meta — Optional. (constant) => null
The image meta data as returned by wp_get_attachment_metadata().. Default null.
3. $attachment_id — Optional. (int)
Image attachment ID. Either $image_meta or $attachment_id is needed when using the image size name as argument for $size.. Default 0.

Usage

  1. if ( !function_exists( 'wp_calculate_image_sizes' ) ) { 
  2. require_once ABSPATH . WPINC . '/media.php'; 
  3.  
  4. // Image size to retrieve. Accepts any valid image size, or an array 
  5. // of width and height values in pixels (in that order). Default 'medium'. 
  6. $size = null; 
  7.  
  8. // Optional. The URL to the image file. Default null. 
  9. $image_src = null; 
  10.  
  11. // Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. 
  12. // Default null. 
  13. $image_meta = null; 
  14.  
  15. // Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` 
  16. // is needed when using the image size name as argument for `$size`. Default 0. 
  17. $attachment_id = -1; 
  18.  
  19. // NOTICE! Understand what this does before running. 
  20. $result = wp_calculate_image_sizes($size, $image_src, $image_meta, $attachment_id); 
  21.  

Defined (1)

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

/wp-includes/media.php  
  1. function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null, $attachment_id = 0 ) { 
  2. $width = 0; 
  3.  
  4. if ( is_array( $size ) ) { 
  5. $width = absint( $size[0] ); 
  6. } elseif ( is_string( $size ) ) { 
  7. if ( ! $image_meta && $attachment_id ) { 
  8. $image_meta = wp_get_attachment_metadata( $attachment_id ); 
  9.  
  10. if ( is_array( $image_meta ) ) { 
  11. $size_array = _wp_get_image_size_from_meta( $size, $image_meta ); 
  12. if ( $size_array ) { 
  13. $width = absint( $size_array[0] ); 
  14.  
  15. if ( ! $width ) { 
  16. return false; 
  17.  
  18. // Setup the default 'sizes' attribute. 
  19. $sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $width ); 
  20.  
  21. /** 
  22. * Filters the output of 'wp_calculate_image_sizes()'. 
  23. * @since 4.4.0 
  24. * @param string $sizes A source size value for use in a 'sizes' attribute. 
  25. * @param array|string $size Requested size. Image size or array of width and height values 
  26. * in pixels (in that order). 
  27. * @param string|null $image_src The URL to the image file or null. 
  28. * @param array|null $image_meta The image meta data as returned by wp_get_attachment_metadata() or null. 
  29. * @param int $attachment_id Image attachment ID of the original image or 0. 
  30. */ 
  31. return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id );