|
27 | 27 | namespace PrestaShop\PrestaShop\Adapter\Image;
|
28 | 28 |
|
29 | 29 | use Category;
|
| 30 | +use Configuration; |
30 | 31 | use Image;
|
31 | 32 | use ImageManager;
|
32 | 33 | use ImageType;
|
@@ -353,25 +354,74 @@ public function getCustomizationImage($imageHash)
|
353 | 354 | public function getNoPictureImage(Language $language)
|
354 | 355 | {
|
355 | 356 | $urls = [];
|
356 |
| - $type = 'products'; |
357 |
| - $imageTypes = ImageType::getImagesTypes($type, true); |
358 | 357 |
|
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 | + ]; |
362 | 366 |
|
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); |
369 | 370 |
|
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 | + } |
375 | 425 | }
|
376 | 426 |
|
377 | 427 | uasort($urls, function (array $a, array $b) {
|
|
0 commit comments