product image url code example

Example 1: get product image woocommerce

// Use $post->ID to get from post object
$id = $loop->post->ID
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), 'single-post-thumbnail' );

<img src="<?php echo $image[0]; ?>" data-id="<?php echo $id; ?>" />

Example 2: magento 2 get product image

//todo get product object $product 

$objectManager =\Magento\Framework\App\ObjectManager::getInstance();
$helperImport = $objectManager->get('\Magento\Catalog\Helper\Image');

$imageUrl = $helperImport->init($product, 'product_page_image_small')
                ->setImageFile($product->getSmallImage()) // image,small_image,thumbnail
                ->resize(380)
                ->getUrl();
echo $imageUrl;

Tags:

Php Example