diff --git a/img/noimageavailable.jpg b/img/noimageavailable.jpg new file mode 100644 index 0000000000000..b924ed7a8b371 Binary files /dev/null and b/img/noimageavailable.jpg differ diff --git a/src/Adapter/Image/ImageRetriever.php b/src/Adapter/Image/ImageRetriever.php index 8b50d9dd73d15..187536666a61e 100644 --- a/src/Adapter/Image/ImageRetriever.php +++ b/src/Adapter/Image/ImageRetriever.php @@ -27,6 +27,7 @@ namespace PrestaShop\PrestaShop\Adapter\Image; use Category; +use Configuration; use Image; use ImageManager; use ImageType; @@ -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) { diff --git a/src/Adapter/ImageThumbnailsRegenerator.php b/src/Adapter/ImageThumbnailsRegenerator.php index 3b258ac17596f..85dc21e4e8ddf 100644 --- a/src/Adapter/ImageThumbnailsRegenerator.php +++ b/src/Adapter/ImageThumbnailsRegenerator.php @@ -27,7 +27,6 @@ namespace PrestaShop\PrestaShop\Adapter; use Db; -use Image; use ImageManager as LegacyImageManager; use ImageType as LegacyImageType; use Module as LegacyModule; @@ -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(