Bagaimana cara mendapatkan url gambar placeholder Magento2?


Jawaban:


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 );

Di Magento 2.2:

  • Dapatkan Pembantu di Blokir (phtml)
    $imageHelper = $this->helper(\Magento\Catalog\Helper\Image::class);

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

  • Dapatkan Url Gambar Placeholder. Pengguna sebagai parameter: 'gambar', 'smal_image', 'swatch_image' atau 'thumbnail'.
    $placeholderImageUrl = $imageHelper->getDefaultPlaceholderUrl('image');


Hai saurabh ada / hilang setelah "placeholder" saat mendapatkan sumber gambar. $ MediaUrl juga harus di atas tag img, mohon koreksi.
— aton1004

ya terima kasih aton, saya tahu tetapi ada beberapa masalah pembentukan yang mengapa saya melakukan itu.
— Saurabh Taletiya

Saya ingin melewati jalur gambar placeholder di API saya, tetapi ketika saya menggunakan getDefaultPlaceholderUrl () itu menghasilkan path gambar seperti: example.com/static/version1551260928/webapi_rest/_view/en_US/…
— Gaurav Agrawal

6

Jika Anda memeriksa pengaturan toko Anda, Anda akan menemukan opsi pemegang tempat Gambar Produk

Stores > Configuration > Catalog > Catalog > Product Image Placeholders

Anda dapat memanggil get the value dalam blok dan memanggil dalam file template Anda.

<?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
}

Dalam Panggilan File Template Anda

$block->getPlaceholderImage();

3

Untuk mendapatkan url gambar placeholder pada templat halaman daftar produk, lakukan ini:

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

3

Kita harus melihat kelas Helper Magento \Magento\Catalog\Helper\Image

Sebagai contoh :

<?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

Dalam blokir, gunakan metode berikut:

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

0

Jika Anda mencoba mendapatkannya di file template apa pun. Anda akan membutuhkan penolong gambar Magento.\Magento\Catalog\Helper\Image::class

Anda bisa mendapatkan contoh seperti ini di bagian atas file .phtml apa pun

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

dan dapatkan path ke url gambar placeholder seperti ini

$imageHelper->getDefaultPlaceholderUrl('small_image')
$imageHelper->getDefaultPlaceholderUrl('image')
Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.