2016-04-18 30 views
1

下面的代碼爲我提供了一個HTML完整的圖像周圍的跨度。在Magento 2中獲取圖像數據

<?php 

$productImage = $block->getImage($_product, $image); 

echo $productImage->toHtml(); 

?> 

我想知道如何收集圖像的圖像數據在Magento 2,這樣我就可以把togheter的HTML我所想要的。

以下代碼給出了原始圖像的URL。但我想要小尺寸的圖像。

$block->getUrl().'pub/media/catalog/product' . $_product->getImage() 

這就是我想要的:

/Magento_Catalog/templates/product/list.php

<?php 

$_productCollection = $block->getLoadedProductCollection(); 

foreach ($_productCollection as $_product){ 

// Get front image in small size for category view 

$get_main_image_url = ''; 
$get_main_image_width = ''; 
$get_main_image_height = ''; 
$get_main_image_alt = ''; 

echo '<img src="' . $get_main_image_url . '" width="' . $get_main_image_width . '" height="' . $get_main_image_height . '"' . ' alt="' . $get_main_image_alt . '">'; 

// Get back image in small size for category view 

$get_back_image_url = ''; 
$get_back_image_width = ''; 
$get_back_image_height = ''; 
$get_back_image_alt = ''; 

echo '<img src="' . $get_back_image_url . '" width="' . $get_back_image_width . '" height="' . $get_back_image_height . '"' . ' alt="' . $get_back_image_alt . '">'; 

} 

?> 
+0

您面臨的問題是什麼? – urfusion

回答

0

我結束了由這裏保存文件覆蓋圖像PHTML文件:

[your_magento_installation]/app/design/frontend/[your_vendor]/[your_theme]/Magento_Catalog/templates/produc t/image_with_borders.phtml

您可能想要複製image.phtml或image_with_borders.phtml的內容。這是我的文件的樣子:

<?php /** @var $block \Magento\Catalog\Block\Product\Image */ ?> 

<img 
    <?php /* @escapeNotVerified */ echo $block->getCustomAttributes(); ?> 
    src="<?php /* @escapeNotVerified */ echo $block->getImageUrl(); ?>" 
    width="<?php /* @escapeNotVerified */ echo $block->getResizedImageWidth(); ?>" 
    height="<?php /* @escapeNotVerified */ echo $block->getResizedImageHeight(); ?>" 
    alt="<?php /* @escapeNotVerified */ echo $block->stripTags($block->getLabel(), null, true); ?>"/>