src/Controller/ImageController.php line 65

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Helper\ImageHelper;
  5. use App\Service\ImageService;
  6. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. class ImageController extends BaseController
  10. {
  11.     private ImageService $imageService;
  12.     public function __construct(
  13.         ImageService $imageService
  14.     ) {
  15.         $this->imageService $imageService;
  16.     }
  17.     public function getAction(Request $request): BinaryFileResponse|JsonResponse
  18.     {
  19.         /** @var User $user */
  20.         $user $this->getUser();
  21.         $imageId $request->get('imageId');
  22.         $width $request->get('w');
  23.         if ($imageId === 'placeholder') {
  24.             // return placeholder image
  25.             return new BinaryFileResponse(__DIR__ '/../../data/placeholder_images/crop.jpg');
  26.         }
  27.         $image $this->imageService->getImage($imageId);
  28.         if (empty($image)) {
  29.             return new JsonResponse(['error' => 'bad_request'], 400);
  30.         }
  31.         // allow crop images that are shared or belong to user without access token
  32.         // because we need this for the library at our website;
  33.         // also allow profile garden images
  34.         if ($image->isRestricted() && $image->getType() !== ImageService::TYPE_CROP && $user === null) {
  35.             return new JsonResponse(['error' => 'forbidden'], 403);
  36.         }
  37.         // todo: check if crop is shared -> then allow image
  38.         $allowed $this->imageService->checkAllowed($user$image);
  39.         if (!$allowed) {
  40.             return new JsonResponse(['error' => 'bad_request'], 400);
  41.         }
  42.         $path $image->getPath();
  43.         if ($width !== null) {
  44.             $width = (int)$width;
  45.             if (!is_numeric($width)) {
  46.                 return new JsonResponse(['error' => 'bad_request'], 400);
  47.             }
  48.             // if width is given check if resized version exists and if not create one
  49.             $ext substr($image->getPath(), -44);
  50.             $resizedPath substr($image->getPath(), 0, -4) . '-w' $width $ext;
  51.             if (!file_exists($resizedPath)) {
  52.                 $img imagecreatefromstring(file_get_contents($image->getPath()));
  53.                 $type mime_content_type($image->getPath());
  54.                 $originalWidth imagesx($img);
  55.                 $originalHeight imagesy($img);
  56.                 $height $width * ($originalHeight $originalWidth);
  57.                 if ($width $originalWidth && $height $originalHeight) {
  58.                     $resizedImg ImageHelper::resize($img$width$heighttruetrue);
  59.                     if ($type === 'image/jpeg') {
  60.                         imagejpeg($resizedImg$resizedPath80);
  61.                     } else {
  62.                         imagepng($resizedImg$resizedPath);
  63.                     }
  64.                 }
  65.             }
  66.             if (file_exists($resizedPath)) {
  67.                 $path $resizedPath;
  68.             }
  69.         }
  70.         if($path === null || !file_exists($path)) {
  71.             return new JsonResponse(['error' => 'bad_request'], 400);
  72.         }
  73.         return $this->file($path);
  74.     }
  75.     public function deleteAction(Request $request): JsonResponse
  76.     {
  77.         /** @var User $user */
  78.         $user $this->getUser();
  79.         if (empty($user)) {
  80.             return new JsonResponse(['error' => 'forbidden'], 403);
  81.         }
  82.         $imageId $request->get('imageId');
  83.         $result $this->imageService->deleteImage($user$imageId);
  84.         if ($result === false) {
  85.             return new JsonResponse(['error' => 'bad_request'], 400);
  86.         }
  87.         return new JsonResponse(['success' => $result]);
  88.     }
  89.     public function saveAction(Request $request): JsonResponse
  90.     {
  91.         /** @var User $user */
  92.         $user $this->getUser();
  93.         if (empty($user)) {
  94.             return new JsonResponse(['error' => 'forbidden'], 403);
  95.         }
  96.         $data json_decode($request->getContent(), true);
  97.         $type $data['type'];
  98.         $base64 $data['base64'];
  99.         $referenceId $data['referenceId'];
  100.         $types = ['crop''package'];
  101.         if (!in_array($type$types)) {
  102.             return new JsonResponse(['error' => 'bad_request''details' => 'type not allowed'], 400);
  103.         }
  104.         $result $this->imageService->save($base64$user$referenceId$type);
  105.         if ($result === false) {
  106.             return new JsonResponse(['error' => 'bad_request''details' => 'save not allowed'], 400);
  107.         }
  108.         return new JsonResponse(['image' => ['id' => $result->getId()]]);
  109.     }
  110. }