如何获取占位符图片网址Magento2?


Answers:


9
    use  \Magento\Store\Model\StoreManager $storeManager

    $this->storeManager = $storeManager;
    $path =
    catalog/placeholder/thumbnail_placeholder OR
    catalog/placeholder/swatch_image_placeholder OR
    catalog/placeholder/small_image_placeholder OR
    catalog/placeholder/image_placeholder OR

     public function getConfig($config_path)
        {
            return $this->storeManager->getStore()->getConfig($config_path);
        }

<img src = $mediaUrl.'catalog/product/placeholder/'.$this->getConfig($path) />
    $mediaUrl = $this ->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA );

在Magento 2.2上:

  • 获取帮助块(phtml)
    $imageHelper = $this->helper(\Magento\Catalog\Helper\Image::class);

  • 获取全球帮助者
    $imageHelper = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Catalog\Helper\Image::class);

  • 获取占位符图像网址。用户作为参数:“图像”,“ smal_image”,“ swatch_image”或“缩略图”。
    $placeholderImageUrl = $imageHelper->getDefaultPlaceholderUrl('image');


嗨,saurabh在获取图像源时,在“占位符”之后有一个/缺失。$ mediaUrl也应位于img标签上方,请更正。
aton1004

是的,感谢aton,我知道但是有一些格式化问题,为什么我要这样做。
Saurabh Taletiya

我想通过我的API占位符图像路径,但是当我使用getDefaultPlaceholderUrl()它生成图像路径,如: example.com/static/version1551260928/webapi_rest/_view/en_US/...
拉夫阿格拉瓦尔

6

如果您检查商店设置,则会发现“产品图片”占位符的选项

Stores > Configuration > Catalog > Catalog > Product Image Placeholders

您可以在块中调用获取值,然后在模板文件中调用。

<?php 

protected $_scopeConfig;

public function __construct
(
    ---
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ---

){
    ---
    $this->_scopeConfig = $scopeConfig;
    ---
}

public function getPlaceholderImage(){
    return $this->_scopeConfig->getValue('catalog/placeholder/image_placeholder'); // Base Image
    $this->_scopeConfig->getValue('catalog/placeholder/small_image_placeholder'); // Small Image
    $this->_scopeConfig->getValue('catalog/placeholder/swatch_image_placeholder'); // Swatch Image
    $this->_scopeConfig->getValue('catalog/placeholder/thumbnail_placeholder'); // Thumbnail Image
}

在您的模板文件调用中

$block->getPlaceholderImage();

3

要在产品列表页面模板上获取占位符图像网址,请执行以下操作:

$imageUrl = $block->getImage($block->getProduct(), 'category_page_grid')->getImageUrl();

3

我们应该看一下Magento Helper类 \Magento\Catalog\Helper\Image

例如 :

<?php

namespace Vendor\Module\Helper;

use Magento\Catalog\Helper\ImageFactory as HelperFactory;
use Magento\Framework\View\Asset\Repository;

class Image
{
    /**
     * @var HelperFactory
     */
    protected $helperFactory;

    /**
     * @var Repository
     */
    protected $assetRepos;

    /**
     * Image constructor.
     * @param HelperFactory $helperFactory
     * @param Repository $repository
     */
    public function __construct(
        HelperFactory $helperFactory,
        Repository $repository
    ) {
        $this->assetRepos = $repository;
        $this->helperFactory = $helperFactory;
    }

    /**
     * Get image url
     *
     * @param $product
     * @param $imageId
     * @return mixed
     */
    public function getImageUrl($product, $imageId = null)
    {
        /** @var \Magento\Catalog\Helper\Image $helper */
        if ($imageId == null) {
            $imageId = 'cart_page_product_thumbnail';
        }
        $helper = $this->helperFactory->create()
            ->init($product, $imageId);
        return $helper->getUrl();
    }

    /**
     * Get small place holder image
     *
     * @return string
     */
    public function getPlaceHolderImage()
    {
        /** @var \Magento\Catalog\Helper\Image $helper */
        $helper = $this->helperFactory->create();
        return $this->assetRepos->getUrl($helper->getPlaceholder('small_image'));
    }
}

2

在块中,使用以下方法:

public function getPlaceholderImage() {
    return sprintf('<img src="%s"/>', $this->getViewFileUrl('Magento_Catalog::images/product/placeholder/thumbnail.jpg'));
}

0

如果您要在任何模板文件中获取它。您将需要Magento的图像助手。\Magento\Catalog\Helper\Image::class

您可以在任何.phtml文件的顶部获得这样的实例

$imageHelper = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Catalog\Helper\Image::class);

并像这样获取占位符图像URL的路径

$imageHelper->getDefaultPlaceholderUrl('small_image')
$imageHelper->getDefaultPlaceholderUrl('image')
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.