2

Can any body tell me how can we get The Category thumbnail image url and Icon image url to display image on frontend in magento 2.3.2

2
  • On where you want to fetch this images? Oct 14, 2020 at 13:43
  • I want to get this into my phtml file. Want to display on some custom page
    – Nafsss
    Oct 15, 2020 at 10:06

2 Answers 2

1

To get Category Image url:

<?php echo $category->getImageUrl(); ?>

And use it in <img> tag like

<img src="<?php  echo $category->getImageUrl(); ?>" />
1
0

[Magento 2.4.x]

You can use the below function to get the category media image URL.

/**
 * @category Msquare
 * @package Msquare_CategoryImageSlider
 * @author Mahesh Makwana <[email protected]>
 */

 ...............................................................
 ...............................................................
 ...............................................................

/**
 * @var \Magento\Catalog\Model\Category\Image
 */
protected $categoryImage;

/**
 * @var \Magento\Catalog\Model\Category\FileInfo
 */
protected $fileInfo;

/**
 * @var \Magento\Catalog\Helper\ImageFactory
 */
protected $imageHelperFactory;

/**
 * @var \Magento\Framework\View\Asset\Repository
 */
protected $assetRepository;

/**
 * @var \Magento\Catalog\Model\CategoryRepository
 */
protected $categoryRepository;

public function __construct(
    ...............................................................
    \Magento\Catalog\Model\CategoryRepository $categoryRepository,
    \Magento\Catalog\Model\Category\Image $categoryImage,
    \Magento\Catalog\Model\Category\FileInfo $fileInfo,
    \Magento\Catalog\Helper\ImageFactory $imageHelperFactory,
    \Magento\Framework\View\Asset\Repository $assetRepository,
    ...............................................................
) {
    ...............................................................
    $this->categoryRepository = $categoryRepository;
    $this->categoryImage = $categoryImage;
    $this->fileInfo = $fileInfo;
    $this->imageHelperFactory = $imageHelperFactory;
    $this->assetRepository = $assetRepository;
    ...............................................................
}

public function getCategoryImage($categoryData, $attributeCode)
{
    $imageLink = null;
    $fileName = $categoryData->getData($attributeCode);
    if (($fileName) && ($this->fileInfo->isExist($fileName))) {
        $imageLink = $this->categoryImage->getUrl($categoryData, $attributeCode); //get category image
    } else {
        $imageHelper = $this->imageHelperFactory->create();
        $imageLink = $this->assetRepository->getUrl($imageHelper->getPlaceholder('image')); // get default placeholder image
    }

    return $imageLink;
}

public function getCategoryData($categoryId)
{
  return $this->categoryRepository->get($categoryId);
}

After that you need to call the above function into your related phtml file like below:

$categoryData = $block->getCategoryData(4);
$categoryImage = $block->getCategoryImage($categoryData, 'image');

Here you need to add your custom category image attribute code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.