Skip to content

Commit 3678e63

Browse files
authored
Merge pull request PrestaShop#36730 from Hlavtox/dynamic-nopicture-image
Automatically generate no image thumbnails
2 parents c719b30 + f4548a6 commit 3678e63

File tree

3 files changed

+76
-18
lines changed

3 files changed

+76
-18
lines changed

img/noimageavailable.jpg

6.1 KB
Loading

src/Adapter/Image/ImageRetriever.php

+66-16
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
namespace PrestaShop\PrestaShop\Adapter\Image;
2828

2929
use Category;
30+
use Configuration;
3031
use Image;
3132
use ImageManager;
3233
use ImageType;
@@ -353,25 +354,74 @@ public function getCustomizationImage($imageHash)
353354
public function getNoPictureImage(Language $language)
354355
{
355356
$urls = [];
356-
$type = 'products';
357-
$imageTypes = ImageType::getImagesTypes($type, true);
358357

359-
if (empty($imageTypes)) {
360-
throw new PrestaShopException(sprintf('There is no image type defined for "%s".', $type));
361-
}
358+
// Set images to regenerate with all theirs specific directories
359+
$objectsToRegenerate = [
360+
['type' => 'categories', 'dir' => _PS_CAT_IMG_DIR_],
361+
['type' => 'manufacturers', 'dir' => _PS_MANU_IMG_DIR_],
362+
['type' => 'suppliers', 'dir' => _PS_SUPP_IMG_DIR_],
363+
['type' => 'products', 'dir' => _PS_PRODUCT_IMG_DIR_],
364+
['type' => 'stores', 'dir' => _PS_STORE_IMG_DIR_],
365+
];
362366

363-
foreach ($imageTypes as $imageType) {
364-
$url = $this->link->getImageLink(
365-
'',
366-
$language->iso_code . '-default',
367-
$imageType['name']
368-
);
367+
foreach ($objectsToRegenerate as $object) {
368+
// Get all image types present on shops for this object
369+
$imageTypes = ImageType::getImagesTypes($object['type'], true);
369370

370-
$urls[$imageType['name']] = [
371-
'url' => $url,
372-
'width' => (int) $imageType['width'],
373-
'height' => (int) $imageType['height'],
374-
];
371+
// We get the "no image available" in the folder of the object
372+
$originalImagePath = implode(DIRECTORY_SEPARATOR, [
373+
rtrim($object['dir'], DIRECTORY_SEPARATOR),
374+
$language->getIsoCode() . '.jpg',
375+
]);
376+
377+
if (!file_exists($originalImagePath)) {
378+
// If it doesn't exist, we use an image for default language
379+
$originalImagePath = implode(DIRECTORY_SEPARATOR, [
380+
rtrim($object['dir'], DIRECTORY_SEPARATOR),
381+
Language::getIsoById((int) Configuration::get('PS_LANG_DEFAULT')) . '.jpg',
382+
]);
383+
384+
if (!file_exists($originalImagePath)) {
385+
// If it doesn't exist, we use a fallback one in the root of img directory
386+
$originalImagePath = implode(DIRECTORY_SEPARATOR, [
387+
rtrim(_PS_IMG_DIR_, DIRECTORY_SEPARATOR),
388+
'noimageavailable.jpg',
389+
]);
390+
}
391+
}
392+
393+
// Get all image sizes for product objects
394+
foreach ($imageTypes as $imageType) {
395+
// Get path of the final thumbnail
396+
$resizedImagePath = implode(DIRECTORY_SEPARATOR, [
397+
rtrim($object['dir'], DIRECTORY_SEPARATOR),
398+
$language->getIsoCode() . '-default-' . $imageType['name'] . '.jpg',
399+
]);
400+
401+
// Check if the thumbnail exists and generate it if needed
402+
if (!file_exists($resizedImagePath)) {
403+
ImageManager::resize(
404+
$originalImagePath,
405+
$resizedImagePath,
406+
(int) $imageType['width'],
407+
(int) $imageType['height']
408+
);
409+
}
410+
411+
// Build image URL for that thumbnail
412+
$imageUrl = $this->link->getImageLink(
413+
'',
414+
$language->iso_code . '-default',
415+
$imageType['name']
416+
);
417+
418+
// And add it to the list
419+
$urls[$imageType['name']] = [
420+
'url' => $imageUrl,
421+
'width' => (int) $imageType['width'],
422+
'height' => (int) $imageType['height'],
423+
];
424+
}
375425
}
376426

377427
uasort($urls, function (array $a, array $b) {

src/Adapter/ImageThumbnailsRegenerator.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
namespace PrestaShop\PrestaShop\Adapter;
2828

2929
use Db;
30-
use Image;
3130
use ImageManager as LegacyImageManager;
3231
use ImageType as LegacyImageType;
3332
use Module as LegacyModule;
@@ -275,10 +274,19 @@ public function regenerateNoPictureImages(string $dir, array $type, array $langu
275274

276275
foreach ($type as $image_type) {
277276
foreach ($languages as $language) {
277+
// We get the "no image available" in the folder of the object
278278
$file = $dir . $language->getIsoCode() . '.jpg';
279+
279280
if (!file_exists($file)) {
280-
$file = _PS_PRODUCT_IMG_DIR_ . $defaultLang->getIsoCode() . '.jpg';
281+
// If it doesn't exist, we use an image for default language
282+
$file = $dir . $defaultLang->getIsoCode() . '.jpg';
283+
284+
if (!file_exists($file)) {
285+
// If it doesn't exist, we use a fallback one in the root of img directory
286+
$file = _PS_IMG_DIR_ . 'noimageavailable.jpg';
287+
}
281288
}
289+
282290
foreach ($configuredImageFormats as $imageFormat) {
283291
if (!file_exists($dir . $language->getIsoCode() . '-default-' . stripslashes($image_type->getName()) . '.' . $imageFormat)) {
284292
if (!LegacyImageManager::resize(

0 commit comments

Comments
 (0)