如何在没有HTML标记的情况下获得Magento 2中的图像数据


How to get image data in Magento 2 without HTML markup?

下面的代码为我提供了一个充满图像跨度的HTML。

<?php
$productImage = $block->getImage($_product, $image);
echo $productImage->toHtml();
?>

我想知道如何在Magento 2中收集图像的图像数据,这样我就可以随心所欲地将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 . '">';
}
?>

我在这里保存了一个文件,从而覆盖了图像PHTML文件:

[your_magento_installation]/app/design/frontend/[your_vendor]/[your_theme]/magento_Catalog/templates/product/image_with_borders.phtml

您可能想要复制image.phtml或image_with_borders.html的内容

<?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); ?>"/>