3

我是 Magento 2.0 的新手,我一直在努力寻找解决方案。我的第一个问题,我还没有弄清楚,如何达到特定的功能?因为我注意到很多人使用 Magento 1.+ 中的那些:

Mage::helper('cms/page')->

或者

Mage::getModel('catalog/product_media_config')  

(例如获取原始图片 url Magento (1.6.1.0)

但我不能在 Magento 2.0 中使用它们。如果这种使用在上一个版本中不再可用,我可以用它作为实现功能的替代方法吗?

至于另一个问题,我无法在 grid.phtml (目录列表)中获得原始大小的图像。以下是获取图像的方法:

<?php 
echo $block->getImage($_item, $image)->getImageUrl(); 
echo $block->getImage($_item, $image)->getWidth(); 
echo $block->getImage($_item, $image)->getHeight();
?>

结果是这样的:

http://192.168.1.4/magento/pub/media/catalog/product/cache/1/small_image/240x300/beff4985b56e3afdbeabfc89641a4582/t/h/thumbnail_1.jpg240300

正如我上面提到的,我想获得原始大小的图像 url 而不是 small_image。我希望我能解释我的问题。如果有人有任何想法,请告诉我。谢谢!

4

2 回答 2

6

使用以下函数获取特定商店的媒体基本 url:

function getMediaBaseUrl() {

$om = \Magento\Framework\App\ObjectManager::getInstance();

$storeManager = $om->get('Magento\Store\Model\StoreManagerInterface');

$currentStore = $storeManager->getStore();
return $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
}

使用 getMediaBaseUrl() 函数我们可以在 Magento2 中找到图像 url:

echo $block->getMediaBaseUrl() .'catalog/product'. $_product->getImage();
于 2016-02-18T06:47:37.660 回答
3

1)直接写媒体目录路径的快速解决方案

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

2) 使用 StoreManager 类对象获取媒体目录路径

namespace YourNamespace\YourModule\Block;
class YourModule extends \Magento\Framework\View\Element\Template
{
    protected $_storeManager;

    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        ...        
    )
    {
        $this->_storeManager = $storeManager;
        ...
    }

    public function getMediaBaseUrl()
    {
        return $this->_storeManager
                    ->getStore()
                    ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
    }
}

然后在你的模块的模板(.phtml)文件中,你可以写成:

echo $block->getMediaBaseUrl() . $_product->getImage();
于 2016-02-13T14:39:16.530 回答