소스 검색

liip-imagine

Vincent 2 년 전
부모
커밋
6fc871fe2d

+ 8 - 0
.gitignore

@@ -46,3 +46,11 @@ public/phpstorm_debug_validator.phar
 public/phpstorm_debug.php
 
 public/phpstorm_index.php
+
+###> phpstan/phpstan ###
+phpstan.neon
+###< phpstan/phpstan ###
+
+###> liip/imagine-bundle ###
+/public/media/cache/
+###< liip/imagine-bundle ###

+ 2 - 1
composer.json

@@ -29,10 +29,12 @@
     "knplabs/knp-snappy-bundle": "^1.9",
     "lcobucci/jwt": "^4.1",
     "lexik/jwt-authentication-bundle": "^2.8",
+    "liip/imagine-bundle": "^2.12",
     "lorenzo/pinky": "^1.0",
     "myclabs/php-enum": "^1.7",
     "nelmio/cors-bundle": "^2.1",
     "odolbeau/phone-number-bundle": "^3.1",
+    "opentalent/phpdocx": "dev-master",
     "phpdocumentor/reflection-docblock": "^5.2",
     "ramsey/uuid": "^4.2",
     "ramsey/uuid-doctrine": "^2.0",
@@ -67,7 +69,6 @@
     "twig/extra-bundle": "^3.4",
     "twig/inky-extra": "^3.4",
     "vincent/foselastica": "1.3.1",
-    "opentalent/phpdocx": "dev-master",
     "webonyx/graphql-php": "^14.3",
     "xantios/mimey": "*"
   },

+ 9 - 10
config/bundles.php

@@ -1,7 +1,5 @@
 <?php
 
-$devEnvs = ['dev' => true, 'docker' => true, 'test' => false, 'staging' => true];
-
 return [
     Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
     Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
@@ -19,12 +17,13 @@ return [
     Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
     Symfony\Bundle\MercureBundle\MercureBundle::class => ['all' => true],
     Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true],
-    Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => $devEnvs,
-    Symfony\Bundle\MakerBundle\MakerBundle::class => $devEnvs,
-    Symfony\Bundle\DebugBundle\DebugBundle::class => $devEnvs,
-    Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => $devEnvs,
-    Zenstruck\Foundry\ZenstruckFoundryBundle::class => $devEnvs,
-    Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle::class => $devEnvs,
-    Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle::class => $devEnvs,
-    Hautelook\AliceBundle\HautelookAliceBundle::class => $devEnvs,
+    Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'docker' => true, 'test' => false, 'staging' => true],
+    Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true, 'docker' => true, 'test' => false, 'staging' => true],
+    Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'docker' => true, 'test' => false, 'staging' => true],
+    Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'docker' => true, 'test' => false, 'staging' => true],
+    Zenstruck\Foundry\ZenstruckFoundryBundle::class => ['dev' => true, 'docker' => true, 'test' => false, 'staging' => true],
+    Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle::class => ['dev' => true, 'docker' => true, 'test' => false, 'staging' => true],
+    Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle::class => ['dev' => true, 'docker' => true, 'test' => false, 'staging' => true],
+    Hautelook\AliceBundle\HautelookAliceBundle::class => ['dev' => true, 'docker' => true, 'test' => false, 'staging' => true],
+    Liip\ImagineBundle\LiipImagineBundle::class => ['all' => true],
 ];

+ 2 - 0
config/packages/knp_gaufrette.yaml

@@ -9,3 +9,5 @@ knp_gaufrette:
   filesystems:
     storage:
       adapter: storage
+
+  stream_wrapper: ~

+ 21 - 0
config/packages/liip_imagine.yaml

@@ -0,0 +1,21 @@
+services:
+    opentalent.liip_imagine.binary.loader.stream.profile_photos:
+        class: Liip\ImagineBundle\Binary\Loader\StreamLoader
+        arguments:
+            - 'gaufrette://storage/'
+        tags:
+            - { name: 'liip_imagine.binary.loader', loader: 'stream.storage' }
+
+# Documentation on how to configure the bundle can be found at: https://symfony.com/doc/current/bundles/LiipImagineBundle/basic-usage.html
+liip_imagine:
+    # valid drivers options include "gd" or "gmagick" or "imagick"
+    driver: "imagick"
+
+    data_loader: stream.storage
+
+    filter_sets:
+        crop_filter:
+            filters:
+                crop:
+                    size: ~
+                    start: ~

+ 2 - 0
config/routes/liip_imagine.yaml

@@ -0,0 +1,2 @@
+_liip_imagine:
+    resource: "@LiipImagineBundle/Resources/config/routing.yaml"

+ 8 - 0
phpstan.dist.neon

@@ -0,0 +1,8 @@
+parameters:
+    level: 6
+    paths:
+        - bin/
+        - config/
+        - public/
+        - src/
+        - tests/

+ 0 - 6
src/Service/File/Storage/ApiLegacyStorage.php

@@ -5,12 +5,6 @@ namespace App\Service\File\Storage;
 
 use App\Entity\Core\File;
 use App\Service\ApiLegacy\ApiLegacyRequestService;
-use App\Service\Utils\UrlBuilder;
-use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
-use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
-use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
-use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
-use Symfony\Contracts\HttpClient\HttpClientInterface;
 
 /**
  * Read and write files into the Opentalent API v1 storage

+ 8 - 2
src/Service/File/Storage/LocalStorage.php

@@ -11,6 +11,7 @@ use App\Enum\Core\FileStatusEnum;
 use App\Enum\Core\FileTypeEnum;
 use App\Repository\Access\AccessRepository;
 use App\Service\File\FileManager;
+use App\Service\File\Utils\ImageUtils;
 use App\Service\Utils\Path;
 use App\Service\Utils\Uuid;
 use DateTime;
@@ -35,7 +36,8 @@ class LocalStorage implements FileStorageInterface
     public function __construct(
         protected FilesystemMap $filesystemMap,
         protected EntityManagerInterface $entityManager,
-        protected AccessRepository $accessRepository
+        protected AccessRepository $accessRepository,
+        protected ImageUtils $imageUtils
     )
     {
         $this->filesystem = $filesystemMap->get(static::FS_KEY);
@@ -76,7 +78,11 @@ class LocalStorage implements FileStorageInterface
      */
     public function read(File $file): string
     {
-        return $this->filesystem->read($file->getSlug());
+        if (!preg_match('#^image#', $file->getMimeType())) {
+            return $this->filesystem->read($file->getSlug());
+        } else {
+            return $this->imageUtils->cropImage($file);
+        }
     }
 
     /**

+ 54 - 0
src/Service/File/Utils/ImageUtils.php

@@ -0,0 +1,54 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Service\File\Utils;
+
+use App\Entity\Core\File;
+use Liip\ImagineBundle\Imagine\Data\DataManager;
+use Liip\ImagineBundle\Imagine\Filter\FilterManager;
+
+class ImageUtils
+{
+    protected const FILTER_CROP = 'crop_filter';
+
+    public function __construct(
+        private DataManager $dataManager,
+        private FilterManager $filterManager
+    ){}
+
+    /**
+     * Permet de cropper et retourner une image
+     * @param File $file : File contenant la définition de l'image
+     * @return string : Image en Base64
+     */
+    public function cropImage(File $file): string{
+        //On récupère l'image via le dataloader d'imagine liip (dataloader stream qui s'appuie sur gaufrette)
+        $binary = $this->dataManager->find(self::FILTER_CROP, $file->getPath());
+
+        //Si on a une config, on créer l'image
+        if(!empty($file->getConfig())){
+            //On applique le filtre crop et on retourne l'image
+            $binary = $this->filterManager->applyFilter($binary, self::FILTER_CROP, $this->getCroppingConfig($file->getConfig()));
+        }
+
+        return $binary->getContent();
+    }
+
+    /**
+     * Définit et retourne le tableau de config servant à cropper
+     * @param string $config : Configuration du File
+     * @return array[] : tableau de configuration
+     */
+    private function getCroppingConfig(string $config): array{
+        $config = json_decode($config, true);
+        return [
+            'filters' => [
+                'crop' => [
+                    'size' => [intval($config['width']), intval($config['height'])],
+                    'start' => [intval($config['x']), intval($config['y'])]
+                ]
+            ]
+        ];
+    }
+}