src/Controller/ThumbnailController.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Pimcore\Controller\FrontendController;
  4. use Pimcore\Model\DataObject\OnixProduct;
  5. use Pimcore\Model\WebsiteSetting;
  6. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. class ThumbnailController extends FrontendController
  13. {
  14.     #[Route(
  15.         '/generate/{ean}-{size}',
  16.         name: 'edition_thumbnail',
  17.         requirements: ['ean' => '\\d+', 'size' => 'lg|md|xs'],
  18.         methods: ['GET']
  19.     )]
  20.     public function __invoke(string $ean, string $size): Response
  21.     {
  22.         $edition = OnixProduct::getByEan($ean, ['limit' => 1]);
  23.         if (empty($edition)) {
  24.             throw new NotFoundHttpException('Edition not found');
  25.         }
  26.         $relativePath = $edition->getItemThumbnail($size);
  27.         if ($relativePath === '' || $relativePath === null) {
  28.             $defaultPath = $this->getDefaultBookImagePath();
  29.             if ($defaultPath !== null) {
  30.                 return new RedirectResponse($defaultPath);
  31.             }
  32.             throw new NotFoundHttpException('Thumbnail not available');
  33.         }
  34.         $filePath = $relativePath[0] === '/' ? PIMCORE_WEB_ROOT . $relativePath : $relativePath;
  35.         if (!is_file($filePath)) {
  36.             $defaultPath = $this->getDefaultBookImagePath();
  37.             if ($defaultPath !== null) {
  38.                 return new RedirectResponse($defaultPath);
  39.             }
  40.             throw new NotFoundHttpException('Thumbnail file missing');
  41.         }
  42.         $response = new BinaryFileResponse($filePath);
  43.         $response->headers->set('Content-Type', 'image/webp');
  44.         $response->setContentDisposition(
  45.             ResponseHeaderBag::DISPOSITION_INLINE,
  46.             sprintf('%s-%s.webp', $ean, $size)
  47.         );
  48.         $response->setPublic();
  49.         $response->setMaxAge(86400);
  50.         return $response;
  51.     }
  52.     private function getDefaultBookImagePath(): ?string
  53.     {
  54.         $defaultSetting = WebsiteSetting::getByName('defaultImageBook');
  55.         if ($defaultSetting === null) {
  56.             return null;
  57.         }
  58.         $imageDefault = $defaultSetting->getData();
  59.         if ($imageDefault === null || !method_exists($imageDefault, 'getFullPath')) {
  60.             return null;
  61.         }
  62.         $path = $imageDefault->getFullPath();
  63.         return $path !== '' ? $path : null;
  64.     }
  65. }