在Magento 2中获取图像数据

时间:2016-04-18 13:44:10

标签: magento2 magento-2.0 magento2.0.2

以下代码为我提供了图像周围的HTML。

<?php

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

echo $productImage->toHtml();

?>

我想知道如何在Magento 2中收集图像的图像数据,以便我可以根据需要添加HTML。

以下代码为我提供了原始图片的网址。但我想要小尺寸的图像。

$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 . '">';

}

?>

1 个答案:

答案 0 :(得分:0)

我最终通过在此处保存文件来覆盖图像PHTML文件:

[your_magento_installation] /应用/设计/前端/ [your_vendor] / [your_theme] /Magento_Catalog/templates/product/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); ?>"/>