فهرست منبع

add $uncropped option to LocalStorage::getImageUrl

Olivier Massot 7 ماه پیش
والد
کامیت
d2f74a2d22

+ 2 - 2
src/Service/File/Factory/ImageFactory.php

@@ -27,9 +27,9 @@ class ImageFactory
      * @param File   $file       : File contenant l'image de base
      * @param string $filterName : nom du filtre liip à appliquer
      */
-    public function createImageContent(File $file, string $filterName): void
+    public function createImageContent(File $file, string $filterName, bool $uncropped = false): void
     {
-        $filters_options = $this->getCropFilterOptions($file->getConfig());
+        $filters_options = !$uncropped ? $this->getCropFilterOptions($file->getConfig()) : [];
         $path = $file->getSlug();
         $this->createAndStore($path, $filterName, $filters_options);
     }

+ 2 - 2
src/Service/File/FileManager.php

@@ -64,11 +64,11 @@ class FileManager
      *
      * @throws FileNotFoundException
      */
-    public function getImageUrl(File $file, string $size, bool $relativePath = false): string
+    public function getImageUrl(File $file, string $size, bool $relativePath = false, bool $uncropped = false): string
     {
         $storage = $this->getStorageFor($file);
 
-        return $storage->getImageUrl($file, $size, $relativePath);
+        return $storage->getImageUrl($file, $size, $relativePath, $uncropped);
     }
 
     /**

+ 1 - 1
src/Service/File/Storage/ApiLegacyStorage.php

@@ -42,7 +42,7 @@ class ApiLegacyStorage implements FileStorageInterface
     /**
      * Retoune l'URL de l'image, à la bonne taille, contenu dans la File.
      */
-    public function getImageUrl(File $file, string $size, bool $relativePath): string
+    public function getImageUrl(File $file, string $size, bool $relativePath, bool $uncropped = false): string
     {
         $url = sprintf('api/files/%s/download/%s?relativePath=1', $file->getId(), $size);
 

+ 1 - 1
src/Service/File/Storage/FileStorageInterface.php

@@ -14,7 +14,7 @@ interface FileStorageInterface
     public function read(File $file): string;
 
     // TODO: remplacer `string $size` par `FileSizeEnum $size`
-    public function getImageUrl(File $file, string $size, bool $relativePath): string;
+    public function getImageUrl(File $file, string $size, bool $relativePath, bool $uncropped = false): string;
 
     public function support(File $file): bool;
 

+ 11 - 7
src/Service/File/Storage/LocalStorage.php

@@ -93,20 +93,24 @@ class LocalStorage implements FileStorageInterface
     /**
      * Retourne l'URL de l'image, à la bonne taille, contenu dans le File.
      */
-    public function getImageUrl(File $file, string $size, bool $relativePath): string
+    public function getImageUrl(File $file, string $size, bool $relativePath, bool $uncropped = false): string
     {
-        $filterName = $this->getFilterFromSizeAndConfig($size, !empty($file->getConfig()));
+        $crop = !empty($file->getConfig()) && !$uncropped;
+
+        $filterName = $this->getFilterFromSizeAndConfig($size, $crop);
+
         $path = $file->getSlug();
 
         if (!$this->cacheManager->isStored($path, $filterName)) {
             try {
-                $this->imageFactory->createImageContent($file, $filterName);
+                $this->imageFactory->createImageContent($file, $filterName, $uncropped);
             } catch (\Exception $e) {
                 $path = 'images/missing-file.png';
 
                 return $relativePath ? $path : $this->urlBuilder->getAbsoluteUrl($path);
             }
         }
+
         $url = $this->cacheManager->resolve($path, $filterName);
 
         return $relativePath ? $this->urlBuilder->getRelativeUrl($url) : $url;
@@ -115,18 +119,18 @@ class LocalStorage implements FileStorageInterface
     /**
      * Retourne le filtre Liip correspondant à la taille désirée.
      */
-    protected function getFilterFromSizeAndConfig(string $size, bool $configExist): string
+    protected function getFilterFromSizeAndConfig(string $size, bool $crop = true): string
     {
         switch ($size) {
             case FileSizeEnum::SM->value :
-                $filter = $configExist ? self::CROP_SM : self::SM_FOLDER;
+                $filter = $crop ? self::CROP_SM : self::SM_FOLDER;
                 break;
             case FileSizeEnum::MD->value :
             default:
-                $filter = $configExist ? self::CROP_MD : self::MD_FOLDER;
+                $filter = $crop ? self::CROP_MD : self::MD_FOLDER;
                 break;
             case FileSizeEnum::LG->value :
-                $filter = $configExist ? self::CROP_LG : self::LG_FOLDER;
+                $filter = $crop ? self::CROP_LG : self::LG_FOLDER;
                 break;
         }
 

+ 7 - 3
src/State/Provider/Core/ImageProvider.php

@@ -39,13 +39,17 @@ final class ImageProvider implements ProviderInterface
             throw new \RuntimeException('not supported', 500);
         }
 
-        return $this->getImage($uriVariables['fileId'], $uriVariables['size']);
+        $uncropped =
+            isset($context['filters']['uncropped']) &&
+            ($context['filters']['uncropped'] === '1' || $context['filters']['uncropped'] === 'true');
+
+        return $this->getImage($uriVariables['fileId'], $uriVariables['size'], $uncropped);
     }
 
     /**
      * @throws FileNotFoundException
      */
-    protected function getImage(int $fileId, string $size): Response
+    protected function getImage(int $fileId, string $size, bool $uncropped = false): Response
     {
         $file = $this->fileRepository->find($fileId);
 
@@ -59,7 +63,7 @@ final class ImageProvider implements ProviderInterface
             throw new \RuntimeException('File '.$fileId.' is not an image.');
         }
 
-        $content = $this->fileManager->getImageUrl($file, $size);
+        $content = $this->fileManager->getImageUrl($file, $size, false, $uncropped);
 
         return new Response($content);
     }