src/Controller/ImageController.php line 159

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Request as Request;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\File\File;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use App\Service\UserService;
  11. use App\Service\ImageService;
  12. use App\Entity\Image;
  13. use App\Entity\Product;
  14. class ImageController extends AbstractController
  15. {
  16.     private $messageServ;
  17.     private $userServ;
  18.     private $entityMana;
  19.     private $imageServ;
  20.     public function __construct(ImageService $imageServUserService $userEntityManagerInterface $em){
  21.         $this->imageServ $imageServ;
  22.         $this->userServ $user;
  23.         $this->entityMana $em;
  24.     }
  25.     /**
  26.      * @Route("/image/{id}", name="getImage")
  27.      */
  28.     public function getImage(Image $image){
  29.         header('Content-Type:'.$image->getMime());
  30.         header('Content-Length: ' $image->getWeight());
  31.         return new Response(readfile($image->getImage()));
  32.     }
  33.     /**
  34.      * @Route("/nutritionFact/{id}", 
  35.      *      name="getNutritionFactImage",
  36.      * )
  37.      */
  38.     public function getNutritionFactImage(Product $productUserService $user){
  39.         $userId $user->getId();
  40.         $filename$this->getParameter('ProductImagesDir').$userId.'/images/'.$product->getNutritionFact();
  41.         $image = new File($filename);
  42.         header('Content-Type:'.$image->getMimeType());
  43.         header('Content-Length: ' $image->getWeight());
  44.         return new Response(readfile($filename));
  45.     }
  46.     /**
  47.      * @Route("/image/resize/{id}/{height}/{width}", name="getImageResized")
  48.      */
  49.     public function getImageResized(Image $image=NULL$height$width){
  50.         if($image){
  51.             if(!$this->checkCache($image$height$width'resize')){
  52.                 if(file_exists($image->getImage()))
  53.                     $img = new \Gumlet\ImageResize($image->getImage());
  54.                 else
  55.                     $img = new \Gumlet\ImageResize('assets/shared/images/defaultAvatar.jpg');
  56.             }else
  57.                 return new Response();
  58.             $img->resize($width$height);
  59.             $filename $image->getImage().'_resize_'.$height.'x'.$width;
  60.             $img->save($filenameIMAGETYPE_JPEG);
  61.             return new Response($img->output());
  62.         }else{
  63.             $img = new \Gumlet\ImageResize('assets/frontend/images/noProductPicture.jpg');
  64.             return new Response($img->output());
  65.         }
  66.     }
  67.     /**
  68.      * @Route("/image/thumbnail/{id}/{height}/{width}", name="getImageAsThumb")
  69.      */
  70.     public function getImageAsThumbnail(Image $image=NULL$height$width){
  71.         if($image){
  72.             if(!$this->checkCache($image$height$width'thumbnail')){
  73.                 if(file_exists($image->getImage()))
  74.                     $img = new \Gumlet\ImageResize($image->getImage());
  75.                 else
  76.                     $img = new \Gumlet\ImageResize('assets/shared/images/defaultAvatar.jpg');
  77.             }else
  78.                 return new Response();
  79.             $img->resizeToBestfit($width$height);
  80.             $filename $image->getImage().'_thumbnail_'.$height.'x'.$width;
  81.             $img->save($filenameIMAGETYPE_JPEG);
  82.             return new Response($img->output());
  83.         }else{
  84.             $img = new \Gumlet\ImageResize('assets/frontend/images/noProductPicture.jpg');
  85.             return new Response();
  86.         }
  87.     }
  88.     /**
  89.      * @Route(
  90.      *      "/image/product/{id}/crop/{height}/{width}",
  91.      *      options = { "expose" = true },
  92.      *      name="getProductImageAsCroppedThumb"
  93.      *  )
  94.      */
  95.     public function getProductImageAsCroppedThumb(Product $product$height$width){
  96.         return $this->getImageAsCroppedThumbnail($product->getMainImage(), $height$width);
  97.     }
  98.     /**
  99.      * @Route("/image/crop/{id}/{height}/{width}", name="getImageAsCroppedThumb")
  100.      */
  101.     public function getImageAsCroppedThumbnail(Image $image=NULL$height$width){
  102.         $save true;
  103.         if($image && file_exists($image->getImage())){
  104.             if(!$this->checkCache($image$height$width'cropThumb')){
  105.                 if(file_exists($image->getImage()))
  106.                     $img = new \Gumlet\ImageResize($image->getImage());
  107.                 else{
  108.                    $img = new \Gumlet\ImageResize('assets/shared/images/defaultAvatar.jpg');
  109.                    $save false;
  110.                 }
  111.             }else{
  112.                 return new Response();
  113.         }
  114.         }else{
  115.             $img = new \Gumlet\ImageResize('assets/frontend/images/noProductPicture.jpg');
  116.             $save false;
  117.         }
  118.         //$img->crop($height, $width);
  119.         $img->crop($width$height);
  120.         if($image){
  121.             $filename $image->getImage().'_cropThumb_'.$height.'x'.$width;
  122.             if($save)
  123.                 $img->save($filenameIMAGETYPE_JPEG);
  124.         }
  125.         return new Response($img->output());
  126.     }
  127.     /**
  128.      * @Route("/image/fixedHeight/{id}/{height}", name="getImageAsFixedHeight")
  129.      */
  130.     public function getImageAsFixedHeight(Image $image$height){
  131.         if(file_exists($image->getImage())){
  132.             if(!$this->checkCache($image$height'X''fixedHeight'))
  133.                 $img = new \Gumlet\ImageResize($image->getImage());
  134.             else
  135.                 return new Response();
  136.         }else
  137.             $img = new \Gumlet\ImageResize('assets/shared/images/defaultAvatar.jpg');
  138.         $img->resizeToHeight($height);
  139.         $filename $image->getImage().'_fixedHeight_'.$height.'xX';
  140.         $img->save($filenameIMAGETYPE_JPEG);
  141.         return new Response($img->output());
  142.     }
  143.     /**
  144.      * @Route("/image/fixedWidth/{id}/{width}", name="getImageAsFixedWidth")
  145.      */
  146.     public function getImageAsFixedWidth(Image $image$width){
  147.         if(file_exists($image->getImage())){
  148.             if(!$this->checkCache($image'X'$width'fixedWidth'))
  149.                 $img = new \Gumlet\ImageResize($image->getImage());
  150.             else
  151.                 return new Response();
  152.         }else
  153.             $img = new \Gumlet\ImageResize('assets/shared/images/defaultAvatar.jpg');
  154.         $img->resizeToWidth($width);
  155.         $filename $image->getImage().'_fixedWidth_Xx'.$width;
  156.         $img->save($filenameIMAGETYPE_JPEG);
  157.         return new Response($img->output());
  158.     }
  159.     /*
  160.      * Check if a cached version exists
  161.      */
  162.     public function checkCache($image$height=''$width=''$suffix=''){
  163.         if(!$image)
  164.             return false;
  165.         $filename $image->getImage().'_'.$suffix.'_'.$height.'x'.$width;
  166.         //Check if file exists if so we assure there is no update since last crop
  167.         if(file_exists($filename)){
  168.             $fileDate = new \DateTime();
  169.             $fileDate->setTimestamp(filemtime($filename));
  170.             if($fileDate $image->getLastUpdated()){
  171.                 $file = new File($filename);
  172.                 header('Content-Type:'.$file->getMimeType());
  173.                 header('Content-Length: ' filesize($filename));
  174.                 return new Response(readfile($filename));
  175.             }else
  176.                 return false;
  177.         }
  178.         return false;
  179.     }
  180.     /**
  181.      * @Route("/API/getImage/{action}/{opt}", 
  182.      *      options = { "expose" = true },
  183.      *      name="getImageByAction"
  184.      *  )
  185.      *
  186.      *  NOTE This is for Data gathering only
  187.      *  Use setImageByActionFromAPI if you need to set a Iamge from API
  188.      */
  189.     public function getImageByActionFromAPI($action$opt=falseRequest $request){
  190.         $action strtolower($action);
  191.         $return = array();
  192.         switch($action){
  193.         case 'test':
  194.             break;
  195.         };
  196.         $response = new JsonResponse();
  197.         $response->setData($return);
  198.         return $response;
  199.     }
  200.     /**
  201.      * @Route("/API/setImage/{action}/{opt}", 
  202.      *      options = { "expose" = true },
  203.      *      name="setImageByAction"
  204.      *  )
  205.      *
  206.      *  NOTE This is for Data Setting only
  207.      *  Use getImageByActionFromAPI if you need to gather a Image from API
  208.      */
  209.     public function setImageByActionFromAPI($action$opt=falseRequest $request){
  210.         $action strtolower($action);
  211.         $return = array();
  212.         switch($action){
  213.         case 'attributes':
  214.             $descs json_decode($request->get('data'));
  215.             foreach($descs as $desc){
  216.                 $this->imageServ->updateDescription($desc->id$desc->description);
  217.             }
  218.             $orders json_decode($request->get('order'));
  219.             $cmtp 1;
  220.             foreach($orders as $order){
  221.                 $this->imageServ->updateDisplayOrder($order$cmtp);
  222.                 $cmtp++;
  223.             }
  224.             $return['success'] = true;
  225.             break;
  226.         case 'delforproduct':
  227.             $product $request->get('product');
  228.             $id $request->get('id');
  229.             $return['success'] = $this->imageServ->deleteImage($idfalse$product);
  230.             break;
  231.         case 'delbyid':
  232.             $id $request->get('id');
  233.             $return['success'] = $this->imageServ->deleteImage($id);
  234.             break;
  235.         case 'blobupload':
  236.             $file $request->files->get('picture');
  237.             $image $this->imageServ->saveUploadedFile($file'product');
  238.             if($image){
  239.                 $return = array(
  240.                     "size" => $image->getWeight(),
  241.                     "url" => $this->generateURL('getImage', array('id' => $image->getId())),
  242.                     "thumbnailUrl" => $this->generateURL('getImageAsThumb', array('id' => $image->getId(), 'height' => '100''width' => '100')),
  243.                 );
  244.             }
  245.             break;
  246.         case 'profileimage':
  247.             $file $request->files->get('picture');
  248.             $image $this->imageServ->saveUploadedFile($file'profile');
  249.             if($image){
  250.                 $user $this->userServ->getUser();
  251.                 $user->setProfileImage($image);
  252.                 //Saving in the database
  253.                 $this->entityMana->persist($user);
  254.                 $this->entityMana->flush();
  255.                 $return = array(
  256.                     "id" => $image->getId(),
  257.                     "url" => $this->generateURL('getImageAsThumb', array('id' => $image->getId(), 'height' => '150''width' => '150')),
  258.                 );
  259.             }
  260.             break;
  261.         case 'updateimage':
  262.             $file $request->files->get('picture');
  263.             if(empty($file)){
  264.                 $file $request->files->get('file');
  265.             }
  266.             $id $request->get('id');
  267.             $image $this->imageServ->updateImageFile($id$file);
  268.             if($image){
  269.                 $ver uniqid();
  270.                 $return = array(
  271.                     "weight" => $image->getWeight(),
  272.                     'size' => array($image->getWidth(), $image->getHeight()),
  273.                     "fullUrl" => $this->generateURL('getImage', array('id' => $image->getId())),
  274.                     "url" => $this->generateURL('getImageAsThumb', array('id' => $image->getId(), 'height' => '150''width' => '150')).'?'.$ver,
  275.                     "thumbnailUrl" => $this->generateURL('getImageAsThumb', array('id' => $image->getId(), 'height' => '100''width' => '100')).'?'.$ver,
  276.                 );
  277.             }
  278.             break;
  279.         case 'newimage':
  280.             $file $request->files->get('file');
  281.             $image $this->imageServ->saveUploadedFile($file'newProduct');
  282.             if($image){
  283.                 $return = array(
  284.                     "id" => $image->getId(),
  285.                     "weight" => $image->getWeight(),
  286.                     'size' => array($image->getWidth(), $image->getHeight()),
  287.                     "url" => $this->generateURL('getImageAsThumb', array('id' => $image->getId(), 'height' => '150''width' => '150')),
  288.                     "editUrl" => $this->generateURL('getImage', array('id' => $image->getId())),
  289.                     "fullUrl" => $this->generateURL('getImage', array('id' => $image->getId())),
  290.                     "thumbnailUrl" => $this->generateURL('getImageAsThumb', array('id' => $image->getId(), 'height' => '100''width' => '100')),
  291.                 );
  292.             }
  293.             break;
  294.         };
  295.         $response = new JsonResponse();
  296.         $response->setData($return);
  297.         return $response;
  298.     }
  299. }