wp_get_attachment_image_src

Retrieve an image to represent an attachment.

Description

(false|array) wp_get_attachment_image_src( (int) $attachment_id, (string) $size = 'thumbnail', (bool) $icon = false ); 

A mime icon for files, thumbnail or intermediate size for images.

The returned array contains four values: the URL of the attachment image src, the width of the image file, the height of the image file, and a boolean representing whether the returned array describes an intermediate (generated) image size or the original, full-sized upload.

Returns (false|array)

Returns an array (url, width, height, is_intermediate), or false, if no image is available.

Parameters (3)

0. $attachment_id (int)
Image attachment ID.
1. $size — Optional. (string) => 'thumbnail'
Image size. Accepts any valid image size, or an array of width and height values in pixels (in that order). Default thumbnail..
2. $icon — Optional. (bool) => false
Whether the image should be treated as an icon. Default false.

Usage

  1. if ( !function_exists( 'wp_get_attachment_image_src' ) ) { 
  2. require_once ABSPATH . WPINC . '/media.php'; 
  3.  
  4. // Image attachment ID. 
  5. $attachment_id = -1; 
  6.  
  7. // Optional. Image size. Accepts any valid image size, or an array of width 
  8. // and height values in pixels (in that order). Default 'thumbnail'. 
  9. $size = 'thumbnail'; 
  10.  
  11. // Optional. Whether the image should be treated as an icon. Default false. 
  12. $icon = false; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = wp_get_attachment_image_src($attachment_id, $size, $icon); 
  16.  

Defined (1)

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

/wp-includes/media.php  
  1. function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) { 
  2. // get a thumbnail or intermediate image if there is one 
  3. $image = image_downsize( $attachment_id, $size ); 
  4. if ( ! $image ) { 
  5. $src = false; 
  6.  
  7. if ( $icon && $src = wp_mime_type_icon( $attachment_id ) ) { 
  8. /** This filter is documented in wp-includes/post.php */ 
  9. $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); 
  10.  
  11. $src_file = $icon_dir . '/' . wp_basename( $src ); 
  12. @list( $width, $height ) = getimagesize( $src_file ); 
  13.  
  14. if ( $src && $width && $height ) { 
  15. $image = array( $src, $width, $height ); 
  16. /** 
  17. * Filters the image src result. 
  18. * @since 4.3.0 
  19. * @param array|false $image Either array with src, width & height, icon src, or false. 
  20. * @param int $attachment_id Image attachment ID. 
  21. * @param string|array $size Size of image. Image size or array of width and height values 
  22. * (in that order). Default 'thumbnail'. 
  23. * @param bool $icon Whether the image should be treated as an icon. Default false. 
  24. */ 
  25. return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );