Skip to content

Commit

Permalink
Merge pull request PrestaShop#36730 from Hlavtox/dynamic-nopicture-image
Browse files Browse the repository at this point in the history
Automatically generate no image thumbnails
  • Loading branch information
kpodemski authored Aug 30, 2024
2 parents c719b30 + f4548a6 commit 3678e63
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 18 deletions.
Binary file added img/noimageavailable.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 66 additions & 16 deletions src/Adapter/Image/ImageRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace PrestaShop\PrestaShop\Adapter\Image;

use Category;
use Configuration;
use Image;
use ImageManager;
use ImageType;
Expand Down Expand Up @@ -353,25 +354,74 @@ public function getCustomizationImage($imageHash)
public function getNoPictureImage(Language $language)
{
$urls = [];
$type = 'products';
$imageTypes = ImageType::getImagesTypes($type, true);

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

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

$urls[$imageType['name']] = [
'url' => $url,
'width' => (int) $imageType['width'],
'height' => (int) $imageType['height'],
];
// We get the "no image available" in the folder of the object
$originalImagePath = implode(DIRECTORY_SEPARATOR, [
rtrim($object['dir'], DIRECTORY_SEPARATOR),
$language->getIsoCode() . '.jpg',
]);

if (!file_exists($originalImagePath)) {
// If it doesn't exist, we use an image for default language
$originalImagePath = implode(DIRECTORY_SEPARATOR, [
rtrim($object['dir'], DIRECTORY_SEPARATOR),
Language::getIsoById((int) Configuration::get('PS_LANG_DEFAULT')) . '.jpg',
]);

if (!file_exists($originalImagePath)) {
// If it doesn't exist, we use a fallback one in the root of img directory
$originalImagePath = implode(DIRECTORY_SEPARATOR, [
rtrim(_PS_IMG_DIR_, DIRECTORY_SEPARATOR),
'noimageavailable.jpg',
]);
}
}

// Get all image sizes for product objects
foreach ($imageTypes as $imageType) {
// Get path of the final thumbnail
$resizedImagePath = implode(DIRECTORY_SEPARATOR, [
rtrim($object['dir'], DIRECTORY_SEPARATOR),
$language->getIsoCode() . '-default-' . $imageType['name'] . '.jpg',
]);

// Check if the thumbnail exists and generate it if needed
if (!file_exists($resizedImagePath)) {
ImageManager::resize(
$originalImagePath,
$resizedImagePath,
(int) $imageType['width'],
(int) $imageType['height']
);
}

// Build image URL for that thumbnail
$imageUrl = $this->link->getImageLink(
'',
$language->iso_code . '-default',
$imageType['name']
);

// And add it to the list
$urls[$imageType['name']] = [
'url' => $imageUrl,
'width' => (int) $imageType['width'],
'height' => (int) $imageType['height'],
];
}
}

uasort($urls, function (array $a, array $b) {
Expand Down
12 changes: 10 additions & 2 deletions src/Adapter/ImageThumbnailsRegenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
namespace PrestaShop\PrestaShop\Adapter;

use Db;
use Image;
use ImageManager as LegacyImageManager;
use ImageType as LegacyImageType;
use Module as LegacyModule;
Expand Down Expand Up @@ -275,10 +274,19 @@ public function regenerateNoPictureImages(string $dir, array $type, array $langu

foreach ($type as $image_type) {
foreach ($languages as $language) {
// We get the "no image available" in the folder of the object
$file = $dir . $language->getIsoCode() . '.jpg';

if (!file_exists($file)) {
$file = _PS_PRODUCT_IMG_DIR_ . $defaultLang->getIsoCode() . '.jpg';
// If it doesn't exist, we use an image for default language
$file = $dir . $defaultLang->getIsoCode() . '.jpg';

if (!file_exists($file)) {
// If it doesn't exist, we use a fallback one in the root of img directory
$file = _PS_IMG_DIR_ . 'noimageavailable.jpg';
}
}

foreach ($configuredImageFormats as $imageFormat) {
if (!file_exists($dir . $language->getIsoCode() . '-default-' . stripslashes($image_type->getName()) . '.' . $imageFormat)) {
if (!LegacyImageManager::resize(
Expand Down

0 comments on commit 3678e63

Please sign in to comment.