Skip to content

Commit 0e18a3d

Browse files
Merge pull request #25 from robinallezard/feat/fallback-img
add palceholder when model doesn't have an image
2 parents 311f47e + 9e3a564 commit 0e18a3d

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

Service/ImagePluginService.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,6 @@ private function getHtmlImageRender(array $image, array $params): string
3838
$sources = $image['sources'];
3939
$data = $image['data'];
4040
$render = '';
41-
if (empty($sources) && !isset($params['placeholder'])) {
42-
return '';
43-
}
44-
45-
if (empty($sources)) {
46-
$sources[] = [
47-
'url' => $params['placeholder'],
48-
'breakpoint' => 'default'
49-
];
50-
}
5141

5242
foreach ($sources as $source) {
5343
if ($source['breakpoint'] === "default" || count($sources) <= 1) {
@@ -71,11 +61,6 @@ private function getHtmlImageRender(array $image, array $params): string
7161
return $render;
7262
}
7363

74-
75-
76-
77-
78-
7964
private function concatHtmlAttrs(?array $htmlAttrs): string
8065
{
8166
$attrs = '';

Service/ImageService.php

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function getUrlForImage(
4545
$region = 'full',
4646
$size = 'max',
4747
$rotation = 0,
48-
$quality = 'default'
48+
$quality = 'default',
4949
) {
5050
return "/image-library/$identifier/$region/$size/$rotation/$quality.$format";
5151
}
@@ -56,7 +56,7 @@ public function geFormattedImage(
5656
$size,
5757
$rotation,
5858
$quality,
59-
$format
59+
$format,
6060
) {
6161
if (!\in_array(strtolower($format), ['jpg', 'jpeg', 'png', 'gif', 'jp2', 'webp'])) {
6262
throw new HttpException(400, 'Bad format value');
@@ -65,7 +65,7 @@ public function geFormattedImage(
6565
$formattedImagePath = THELIA_WEB_DIR.'image-library'.DS.$identifier.DS.$region.DS.$size.DS.$rotation;
6666
if (!is_dir($formattedImagePath)) {
6767
if (!@mkdir($formattedImagePath, 0755, true)) {
68-
throw new \RuntimeException(sprintf('Failed to create %s file in cache directory', $formattedImagePath));
68+
throw new \RuntimeException(\sprintf('Failed to create %s file in cache directory', $formattedImagePath));
6969
}
7070
}
7171

@@ -104,7 +104,7 @@ public function applyRegion(ImageInterface $image, $region)
104104
}
105105

106106
// If region start with pct: values are percent of size
107-
$regionMode = strpos($region, 'pct:') === false ? 'value' : 'percentage';
107+
$regionMode = !str_contains($region, 'pct:') ? 'value' : 'percentage';
108108
$values = explode(',', str_replace('pct:', '', $region));
109109

110110
if (\count($values) !== 4) {
@@ -137,9 +137,9 @@ public function applyRegion(ImageInterface $image, $region)
137137

138138
public function applySize(ImageInterface $image, $size)
139139
{
140-
$upscaleMode = false !== strpos($size, '^');
141-
$keepAspectRatio = false !== strpos($size, '!');
142-
$borders = false !== strpos($size, '*');
140+
$upscaleMode = str_contains($size, '^');
141+
$keepAspectRatio = str_contains($size, '!');
142+
$borders = str_contains($size, '*');
143143
$size = str_replace('^', '', $size);
144144
$size = str_replace('!', '', $size);
145145
$size = str_replace('*', '', $size);
@@ -155,7 +155,7 @@ public function applySize(ImageInterface $image, $size)
155155
$width = $image->getSize()->getWidth();
156156
$height = $image->getSize()->getHeight();
157157

158-
if (false !== strpos($size, 'pct:')) {
158+
if (str_contains($size, 'pct:')) {
159159
$values = explode(':', $size);
160160
if (!isset($values[1])) {
161161
throw new HttpException(400, 'Bad size values');
@@ -221,7 +221,7 @@ public function applySize(ImageInterface $image, $size)
221221

222222
public function applyRotation(ImageInterface $image, $rotation, $format)
223223
{
224-
if (false !== strpos($rotation, '!')) {
224+
if (str_contains($rotation, '!')) {
225225
$image = $image->flipHorizontally();
226226
}
227227

@@ -266,7 +266,7 @@ public function applyQuality(ImageInterface $image, $quality)
266266
}
267267

268268
public function getImageFileName(
269-
LibraryImage $image = null
269+
?LibraryImage $image = null,
270270
) {
271271
if (null == $image) {
272272
return null;
@@ -288,7 +288,7 @@ public function getImageFileName(
288288
return $fileName;
289289
}
290290

291-
public function getImageModel($identifier, $type = "library", $imageId = null)
291+
public function getImageModel($identifier, $type = 'library', $imageId = null)
292292
{
293293
$query = LibraryImageQuery::create();
294294

@@ -306,7 +306,6 @@ public function getImageModel($identifier, $type = "library", $imageId = null)
306306
return $query->find();
307307
}
308308

309-
310309
public function openImage($identifier)
311310
{
312311
$imageModel = LibraryImageQuery::create()->filterById($identifier)->findOne();
@@ -379,7 +378,7 @@ public function getImageDataWithType(array $params): array
379378
'description' => $image?->getDescription(),
380379
'chapo' => $image?->getChapo(),
381380
'postscriptum' => $image?->getPostscriptum(),
382-
'id' => $image?->getId()
381+
'id' => $image?->getId(),
383382
];
384383
}
385384

@@ -390,7 +389,7 @@ private function createSearchQuery($source, $sourceId, $imageId = null)
390389
{
391390
$queryClass = 'Thelia\\Model\\'.ucfirst($source).'ImageQuery';
392391

393-
$filterMethod = sprintf('filterBy%sId', $imageId ? '' : $source);
392+
$filterMethod = \sprintf('filterBy%sId', $imageId ? '' : $source);
394393
// xxxImageQuery::create()
395394
$method = new \ReflectionMethod($queryClass, 'create');
396395
$search = $method->invoke(null); // Static !
@@ -402,10 +401,10 @@ private function createSearchQuery($source, $sourceId, $imageId = null)
402401
return $search;
403402
}
404403

405-
public function getProcessedImages(array $imagesData, array | string $filters): array
404+
public function getProcessedImages(array $imagesData, array|string $filters, ?string $placeholder): array
406405
{
407406
$processedImages = [];
408-
if (!is_array($filters)) {
407+
if (!\is_array($filters)) {
409408
$filters = ['default' => $filters];
410409
}
411410

@@ -419,7 +418,7 @@ public function getProcessedImages(array $imagesData, array | string $filters):
419418
);
420419
$sources[] = [
421420
'breakpoint' => $breakpoint,
422-
'url' => $url
421+
'url' => $url,
423422
];
424423
}
425424
}
@@ -429,20 +428,21 @@ public function getProcessedImages(array $imagesData, array | string $filters):
429428
];
430429
}
431430

432-
return $processedImages;
431+
return !empty($processedImages) ? $processedImages : $this->getFallbackImg($placeholder);
433432
}
434433

435434
protected function getLibraryImageData($imageQuery)
436435
{
437436
$locale = $this->requestStack?->getCurrentRequest()?->getSession()?->getLang()->getLocale();
438437

439438
$images = [];
439+
440440
foreach ($imageQuery as $image) {
441441
$image->setlocale($locale);
442442
$images[] = [
443443
'path' => $this->getImageFileName($image),
444444
'title' => $image->getTitle(),
445-
'id' => $image->getTitle()
445+
'id' => $image->getTitle(),
446446
];
447447
}
448448

@@ -453,12 +453,27 @@ public function getImages(array $params): array
453453
{
454454
if ($params['source_type'] === self::LIBRARY || $params['source_type'] === self::PAGE) {
455455
$imageModel = $this->getImageModel($params['source_id'] ?? null, $params['source_type'], $params['img_id'] ?? null);
456-
457456
$imagesData = $imageModel ? $this->getLibraryImageData($imageModel) : [];
458457
} else {
459458
$imagesData = $this->getImageDataWithType($params);
460459
}
461-
return $this->getProcessedImages($imagesData, $params['filters']);
462460

461+
return $this->getProcessedImages($imagesData, $params['filters'], $params['placeholder'] ?? null);
462+
}
463+
464+
465+
private function getFallbackImg(?string $placeholder): array
466+
{
467+
return [
468+
[
469+
'sources' => [
470+
[
471+
'breakpoint' => 'default',
472+
'url' => $placeholder ?? ''
473+
]
474+
],
475+
'data' => []
476+
]
477+
];
463478
}
464479
}

0 commit comments

Comments
 (0)