Native Magento 2 has four types of placeholder image called, small_image, image, thumbnail, and swatch_image.
Placeholder image URL used when you want to display default images for a site.
Magento 2 get placeholder image URL using below simple code snippet.
I hope you are aware of, How to upload Placeholder images in Magento 2?
Create Block file to programmatically get placeholder image,
<?php namespace Rbj\Training\Block; class Placeholder extends \Magento\Framework\View\Element\Template { public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\View\Asset\Repository $assetRepos, \Magento\Catalog\Helper\ImageFactory $helperImageFactory, array $data = [] ) { $this->assetRepos = $assetRepos; $this->helperImageFactory = $helperImageFactory; parent::__construct($context, $data); } /** * Get place holder image of a product for small_image * * @return string */ public function getPlaceHolderImage() { $imagePlaceholder = $this->helperImageFactory->create(); return $this->assetRepos->getUrl($imagePlaceholder->getPlaceholder('small_image')); } }
Call Placeholder function from Template file by below way,
<?php $placeholderUrl = $block->getPlaceHolderImage(); ?> <img src="<?php echo $placeholderUrl; ?>" alt="placeholder" />
Using the above way, You can get small_image Placeholder image url in Magento 2.
You can pass small_image, image, thumbnail and swatch_image for a placeholder.