WordPress at a glance

wp_get_attachment_image_src() WP 1.0

Retrieve an image to represent an attachment.

Возвращает

Array/false. Array of image data, or boolean false if no image is available.

  • 0(string)
    Image source URL.

  • 1(int)
    Image width in pixels.

  • 2(int)
    Image height in pixels.

  • 3(true/false)
    Whether the image is a resized image.
Works based on: image_downsize()
1 time = 0.003252s = very slow | 50000 times = 12.29s = slow | PHP 7.1.2, WP 4.7.5
Hooks from the function

Usage

wp_get_attachment_image_src( $attachment_id, $size, $icon );
$attachment_id(int) (required)
Image attachment ID.
$size(string/int[])
Image size. Accepts any valid image size name, or an array of width and height values in pixels (in that order).
Default: 'thumbnail'
$icon(true/false)
Whether the image should fall back to a mime type icon.
Default: false

Список изменений

С версии 2.5.0 Введена.

Code of wp_get_attachment_image_src() WP 5.5.1

wp-includes/media.php
<?php
function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) {
	// Get a thumbnail or intermediate image if there is one.
	$image = image_downsize( $attachment_id, $size );
	if ( ! $image ) {
		$src = false;

		if ( $icon ) {
			$src = wp_mime_type_icon( $attachment_id );

			if ( $src ) {
				/** This filter is documented in wp-includes/post.php */
				$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );

				$src_file               = $icon_dir . '/' . wp_basename( $src );
				list( $width, $height ) = @getimagesize( $src_file );
			}
		}

		if ( $src && $width && $height ) {
			$image = array( $src, $width, $height, false );
		}
	}
	/**
	 * Filters the attachment image source result.
	 *
	 * @since 4.3.0
	 *
	 * @param array|false  $image         {
	 *     Array of image data, or boolean false if no image is available.
	 *
	 *     @type string $0 Image source URL.
	 *     @type int    $1 Image width in pixels.
	 *     @type int    $2 Image height in pixels.
	 *     @type bool   $3 Whether the image is a resized image.
	 * }
	 * @param int          $attachment_id Image attachment ID.
	 * @param string|int[] $size          Requested size of image. Image size name, or array of width
	 *                                    and height values (in that order).
	 * @param bool         $icon          Whether the image should be treated as an icon.
	 */
	return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );
}

Related Functions

From category: Images

More from Template Tags: Post Thumbnails

No comments
    Log In . Register