Skip to content

Commit 8a89716

Browse files
committed
feat: Add default placeholde image
1 parent 4c1641c commit 8a89716

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

Model/Display.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ public function getImage(string $imagePath, int $width, int $height, array $para
104104
$retina = $params['retina'] ?? false;
105105
/** @var string $title */
106106
$title = $params['title'] ?? '';
107+
/** @var string $placeholderType */
108+
$placeholderType = $params['placeholder_type'] ?? 'default';
107109
/** @var string $alt */
108110
$alt = $params['alt'] ?? $title;
109111
/** @var string $class */
@@ -114,7 +116,7 @@ public function getImage(string $imagePath, int $width, int $height, array $para
114116
/** @var bool $placeholder */
115117
$placeholder = (bool)($params['placeholder'] ?? true);
116118
/** @var string $placeholderImagePath */
117-
$placeholderImagePath = $this->placeholderFactory->create(['type' => 'image'])->getPath();
119+
$placeholderImagePath = $this->placeholderFactory->create(['type' => $placeholderType])->getPath();
118120
/** @var string $placeholderImageUrl */
119121
$placeholderImageUrl = $this->resize->resizeAndGetUrl($placeholderImagePath, $width, $height, $resize);
120122

@@ -123,6 +125,7 @@ public function getImage(string $imagePath, int $width, int $height, array $para
123125
$mainImageUrl = $this->resize->resizeAndGetUrl($imagePath, $width, $height, $resize);
124126
if ($mainImageUrl === '') {
125127
$imagePath = $placeholderImagePath;
128+
$mainImageUrl = $placeholderImageUrl;
126129
}
127130

128131
if ($this->isSvgImage($imagePath)) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Web200\ImageResize\Plugin;
6+
7+
use Magento\Catalog\Model\Config\CatalogClone\Media\Image;
8+
9+
/**
10+
* Class AddDefaultPlaceholderImage
11+
*
12+
* @package Web200\ImageResize\Plugin
13+
*/
14+
class AddDefaultPlaceholderImage
15+
{
16+
/**
17+
* Add default placeholder image
18+
*
19+
* @param Image $subject
20+
* @param string[] $prefixes
21+
*
22+
* @return string[]
23+
*/
24+
public function afterGetPrefixes(Image $subject, $prefixes)
25+
{
26+
if (is_array($prefixes)) {
27+
$prefixes[] = [
28+
'field' => 'default_',
29+
'label' => 'Non Product Image placeholder',
30+
];
31+
}
32+
33+
return $prefixes;
34+
}
35+
}

etc/di.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@
3737
</type>
3838
<!-- Svg upload File Validation -->
3939
<type name="Magento\Framework\Image\Adapter\AbstractAdapter">
40-
<plugin name="validate_svg_file"
40+
<plugin name="web200_imagevalidate_svg_file"
4141
type="Web200\ImageResize\Plugin\Svg\ValidateSvgFile"/>
4242
</type>
43+
<!-- Add default placeholder image -->
44+
<type name="Magento\Catalog\Model\Config\CatalogClone\Media\Image">
45+
<plugin name="web200_imageadd_default_placeholder_image"
46+
type="Web200\ImageResize\Plugin\AddDefaultPlaceholderImage"/>
47+
</type>
4348
</config>

0 commit comments

Comments
 (0)