我想在自定义模块中为自定义图像添加view.xml。我使用了以下方法:
- 创建etc / view.xml
<view xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/view.xsd"> <media> <images module="VendorName_ModuleName"> <image id="custom_image" type="custom_image"> <width>100</width> <height>100</height> </image> </images> </media> </view>
- 创建图像模型类以覆盖目录图像模型中的init函数
<?php
namespace VendorName\ModuleName\Model;
use Magento\Catalog\Helper\Image as CatalogImage;
class Image extends CatalogImage
{
/**
* Initialize Helper to work with Image
*
* @param \Magento\Catalog\Model\Product $product
* @param string $imageId
* @param array $attributes
* @return $this
*/
public function init($product,$imageId,$attributes = [])
{
$this->_reset();
$this->attributes = array_merge(
$this->getconfigView()->getMediaAttributes('VendorName_ModuleName',self::MEDIA_TYPE_CONFIG_NODE,$imageId),$attributes
);
$this->setProduct($product);
$this->setImageProperties();
$this->setWatermarkProperties();
return $this;
}
}
- 在di.xml中,使用我们的自定义类替换Magento \ Catalog \ Model \ View \ Asset \ Image类中的mediaConfig的参数
<type name="Magento\Catalog\Model\View\Asset\Image">
<arguments>
<argument name="mediaConfig" xsi:type="object">VendorName\ModuleName\Model\Media\Config</argument>
</arguments>
</type>
- VendorName \ ModuleName \ Model \ Media \ Config类:
<?php
namespace VendorName\ModuleName\Model\Media;
use Magento\Eav\Model\Entity\Attribute;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Catalog\Model\Product\Media\ConfigInterface;
class Config implements ConfigInterface
{
/**
* Store manager
*
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* @var Attribute
*/
private $attributeHelper;
/**
* @var string[]
*/
private $mediaAttributeCodes;
/**
* @param StoreManagerInterface $storeManager
*/
public function __construct(StoreManagerInterface $storeManager)
{
$this->storeManager = $storeManager;
}
/**
* Get filesystem directory path for product images relative to the media directory.
*
* @return string
*/
public function getBaseMediaPathAddition()
{
return 'media_folder_path';
}
/**
* Get web-based directory path for product images relative to the media directory.
*
* @return string
*/
public function getBaseMediaUrlAddition()
{
return 'media_folder_path';
}
/**
* @inheritdoc
*/
public function getBaseMediaPath()
{
return 'media_folder_path';
}
/**
* @inheritdoc
*/
public function getBaseMediaUrl()
{
return $this->storeManager->getStore()
->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'media_folder_path';
}
/**
* Filesystem directory path of temporary product images relative to the media directory.
*
* @return string
*/
public function getBaseTmpMediaPath()
{
return 'tmp/' . $this->getBaseMediaPathAddition();
}
/**
* Get temporary base media URL.
*
* @return string
*/
public function getBaseTmpMediaUrl()
{
return $this->storeManager->getStore()->getBaseUrl(
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
) . 'tmp/' . $this->getBaseMediaUrlAddition();
}
/**
* @inheritdoc
*/
public function getMediaUrl($file)
{
return $this->getBaseMediaUrl() . '/' . $this->_prepareFile($file);
}
/**
* @inheritdoc
*/
public function getMediaPath($file)
{
return $this->getBaseMediaPath() . '/' . $this->_prepareFile($file);
}
/**
* Get temporary media URL.
*
* @param string $file
* @return string
*/
public function getTmpMediaUrl($file)
{
return $this->getBaseTmpMediaUrl() . '/' . $this->_prepareFile($file);
}
/**
* Part of URL of temporary product images relative to the media directory.
*
* @param string $file
* @return string
*/
public function getTmpMediaShortUrl($file)
{
return 'tmp/' . $this->getBaseMediaUrlAddition() . '/' . $this->_prepareFile($file);
}
/**
* Part of URL of product images relatively to media folder.
*
* @param string $file
* @return string
*/
public function getMediaShortUrl($file)
{
return $this->getBaseMediaUrlAddition() . '/' . $this->_prepareFile($file);
}
/**
* Get path to the temporary media.
*
* @param string $file
* @return string
*/
public function getTmpMediaPath($file)
{
return $this->getBaseTmpMediaPath() . '/' . $this->_prepareFile($file);
}
/**
* Process file path.
*
* @param string $file
* @return string
*/
protected function _prepareFile($file)
{
return ltrim(str_replace('\\','/',$file),'/');
}
/**
* Get codes of media attribute.
*
* @return array
* @since 100.0.4
*/
public function getMediaAttributeCodes()
{
if (!isset($this->mediaAttributeCodes)) {
// the in-memory object-level caching allows to prevent unnecessary calls to the DB
$this->mediaAttributeCodes = $this->getattributeHelper()->getattributeCodesByFrontendType('media_image');
}
return $this->mediaAttributeCodes;
}
/**
* Get attribute helper.
*
* @return Attribute
*/
private function getattributeHelper()
{
if (null === $this->attributeHelper) {
$this->attributeHelper = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Eav\Model\Entity\Attribute::class);
}
return $this->attributeHelper;
}
}
- 要从您的自定义资源模型/类访问图像
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$imageHelper = $_objectManager->get('VendorName\ModuleName\Model\Image');
$image_url = $imageHelper->init($item,'custom_image')->setImageFile($image)->getUrl();
$ item将成为您的自定义资源实体
如果您有其他方法,请分享。