Automattic\WooCommerce\Blocks\Utils

ProductGalleryUtils::get_image_data │ private static │ WC 1.0

Get source data for one image.

Method of the class: ProductGalleryUtils{}

No Hooks.

Returns

array. Image source data.

Usage

$result = ProductGalleryUtils::get_image_data( $image_id, $size, $product_title, $index );
$image_id(int|string) (required)
The image ID.
$size(string) (required)
The size of the image to retrieve.
$product_title(string) (required)
The title of the product used for alt fallback.
$index(int) (required)
The image index.

ProductGalleryUtils::get_image_data() code WC 11.1.2

private static function get_image_data( $image_id, $size, $product_title, $index ) {
	$image_id = (int) $image_id;

	if ( 0 === $image_id ) {
		return array(
			'id'         => 0,
			'media_type' => 'image',
			'src'        => wc_placeholder_img_src(),
			'srcset'     => '',
			'sizes'      => '',
			'alt'        => '',
		);
	}

	$full_src = wp_get_attachment_image_src( $image_id, $size );
	$srcset   = wp_get_attachment_image_srcset( $image_id, $size );
	$sizes    = wp_get_attachment_image_sizes( $image_id, $size );
	$alt      = get_post_meta( $image_id, '_wp_attachment_image_alt', true );

	return array(
		'id'         => $image_id,
		'media_type' => 'image',
		'src'        => $full_src ? $full_src[0] : '',
		'srcset'     => $srcset ? $srcset : '',
		'sizes'      => $sizes ? $sizes : '',
		'alt'        => $alt ? $alt : sprintf(
			/* translators: 1: Product title 2: Image number */
			__( '%1$s - Image %2$d', 'woocommerce' ),
			$product_title,
			$index + 1
		),
	);
}