src/Controller/ImageController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Image;
  4. use App\Entity\ImageLang;
  5. use App\Repository\ImageLangRepository;
  6. use App\Repository\ImageRepository;
  7. use App\Repository\LangRepository;
  8. use Doctrine\ORM\EntityManager;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  13. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\String\Slugger\SluggerInterface;
  19. #[Route('/image'name'app_image')]
  20. class ImageController extends AbstractController
  21. {
  22.     private $em null;
  23.     public function __construct(EntityManagerInterface $em)
  24.     {
  25.         $this->em $em;
  26.     }
  27.     #[Route('/{imageName}'name'_display'methods:['GET'])]
  28.     public function display($imageNameRequest $requestSluggerInterface $sluggerImageRepository $imageRepository): Response
  29.     {
  30.         $image $imageRepository->findOneBy(['name'=>$imageName]);
  31.         if($image->getProduct() != false){
  32.             $filename $this->getParameter('general_image_directory') . "/products/" $imageName;
  33.         }else{
  34.             $filename $this->getParameter('wysiwyg_image_directory') . "/" $imageName;
  35.         }
  36.         
  37.         if (file_exists($filename)) {
  38.             //return a new BinaryFileResponse with the file name
  39.             return new BinaryFileResponse($filename);
  40.         } else {
  41.             return new JsonResponse(null404);
  42.         }
  43.     }
  44.     #[IsGranted('ROLE_CONTENT_MANAGER')]
  45.     #[Route('/add'name'_add'methods:['POST'])]
  46.     public function add(Request $requestSluggerInterface $sluggerImageRepository $imageRepositoryLangRepository $langRepositoryImageLangRepository $imageLangRepository): Response
  47.     {
  48.         $file $request->files->get('image');
  49.         if ($file) {
  50.             $originalFilename pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
  51.             // this is needed to safely include the file name as part of the URL
  52.             $safeFilename $slugger->slug($originalFilename);
  53.             $newFilename $safeFilename.'-'.uniqid().'.'.$file->guessExtension();
  54.             // Move the file to the directory where product image are stored
  55.             try {
  56.                 $file->move(
  57.                     $this->getParameter('wysiwyg_image_directory'),
  58.                     $newFilename
  59.                 );
  60.                 $image = new Image();
  61.                 $image->setName($newFilename);
  62.                 $image->setProduct(null);
  63.                 $image->setCover(false);
  64.                 $image->setPosition(0);
  65.                 $langs $langRepository->findAll();
  66.                 foreach($langs as $lang){
  67.                     $imageLang = new ImageLang();
  68.                     $imageLang->setLang($lang);
  69.                     $imageLang->setImage($image);
  70.                     $imageLang->setDescription('');
  71.                     $imageLangRepository->add($imageLang);
  72.                 }
  73.                 
  74.                 $imageRepository->add($imagetrue);
  75.                 return $this->json(['code'=>200'success'=>1'file'=>['url'=>$this->generateUrl('app_image_display',['imageName'=>$newFilename])]]);
  76.             } catch (FileException $e) {
  77.                 return new Response($eResponse::HTTP_INTERNAL_SERVER_ERROR);
  78.             }
  79.         }else{
  80.             return $this->json(['code'=>Response::HTTP_NO_CONTENT'success'=>0]);
  81.         }
  82.     }
  83.     #[IsGranted('ROLE_CONTENT_MANAGER')]
  84.     #[Route('/editposition'name'_editposition'methods:['POST'])]
  85.     public function editPosition(Request $requestImageRepository $imageRepository): Response
  86.     {
  87.         $data $request->request->get('data');
  88.         $datas json_decode($data);
  89.         foreach($datas as $key => $value){
  90.             $image $imageRepository->find($value->imageId);
  91.             $image->setPosition($value->position);
  92.             $imageRepository->add($image);
  93.         }
  94.         $this->em->flush();
  95.         return $this->json(['code'=>200'success'=>1]);
  96.     }
  97. }