<?php
declare(strict_types=1);
namespace App\Controller\ImageCropper;
use App\Helper\Image\ImageHelper;
use App\Helper\Image\ImageResponseInterface;
use App\Helper\Image\ImageTypeEnum;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* @IsGranted("PUBLIC_ACCESS")
*/
class ImageController extends AbstractController
{
public function crop(ImageHelper $imageHelper, string $type, string $dimensions, string $image): Response
{
if (false === \in_array($type, array_column(ImageTypeEnum::cases(), 'value'), true)) {
throw new NotFoundHttpException(ImageResponseInterface::IMAGE_CROP_TYPE_NOT_EXIST);
}
return new BinaryFileResponse(
$imageHelper->crop($type, $dimensions, $image),
Response::HTTP_OK,
[
'Content-Type' => 'image',
AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER => 'true',
'Cache-Control' => 'max-age=31536000, public',
]
);
}
}