Automattic\WooCommerce\Blocks\Utils
ProductGalleryUtils::get_image_src_data
Get the image source data.
Method of the class: ProductGalleryUtils{}
No Hooks.
Returns
Array. An array of image source data.
Usage
$result = ProductGalleryUtils::get_image_src_data( $image_ids, $size, $product_title );
- $image_ids(array) (required)
- The image IDs to retrieve the source data for.
- $size(string) (required)
- The size of the image to retrieve.
- $product_title(string)
- The title of the product used for alt fallback.
Default:''
ProductGalleryUtils::get_image_src_data() ProductGalleryUtils::get image src data code WC 10.8.1
public static function get_image_src_data( $image_ids, $size, $product_title = '' ) {
$image_src_data = array();
foreach ( $image_ids as $index => $image_id ) {
if ( 0 === $image_id ) {
// Handle placeholder image.
$image_src_data[] = array(
'id' => 0,
'src' => wc_placeholder_img_src(),
'srcset' => '',
'sizes' => '',
'alt' => '',
);
continue;
}
// Get the image source.
$full_src = wp_get_attachment_image_src( $image_id, $size );
// Get srcset and sizes.
$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 );
$image_src_data[] = array(
'id' => $image_id,
'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
),
);
}
return $image_src_data;
}