/**
  * @Route("/save", name="fbeen_croppic_save")
  * @Method({"POST"})
  */
 public function saveAction(Request $request)
 {
     $token = $this->get('security.csrf.token_manager')->getToken('fbeen_croppic');
     if ($request->get('crsf_token') != $token) {
         return new JsonResponse(array('status' => 'error', 'message' => 'csrf token is ongeldig. Probeer het nog eens.'));
     }
     if (!$request->files->has('img')) {
         return new JsonResponse(array('status' => 'error', 'message' => 'did not find any uploaded file'));
     }
     $em = $this->getDoctrine()->getManager();
     $image = new Image();
     $image->setImg($request->files->get('img'));
     $errors = $this->get('validator')->validate($image);
     if (count($errors)) {
         return new JsonResponse(array('status' => 'error', 'message' => $errors[0]->getMessage()));
     }
     $helper = $this->get('fbeen.imagehelper');
     $helper->upload($image);
     $em->persist($image);
     $em->flush();
     list($width, $height) = getimagesize($helper->getPath($image->getFilename(), 'original'));
     return new JsonResponse(array('status' => 'success', 'url' => $helper->getUrl($image->getFilename(), 'original'), 'width' => $width, 'height' => $height));
 }
Beispiel #2
0
 public function upload(Image $image)
 {
     // the file property can be empty if the field is not required
     if (null === $image->getImg()) {
         return;
     }
     $uploadDir = $this->container->getParameter('fbeen_croppic.upload.filepath') . $this->container->getParameter('fbeen_croppic.upload.original');
     $extension = '.' . $image->getImg()->guessExtension();
     if ($extension == '.jpeg') {
         $extension = '.jpg';
     }
     $filename = uniqid() . $extension;
     // move takes the target directory and then the
     // target filename to move to
     $image->getImg()->move($uploadDir, $filename);
     @mkdir($this->container->getParameter('fbeen_croppic.upload.filepath') . $this->container->getParameter('fbeen_croppic.upload.cropped'), 0777, TRUE);
     $image->setFilename($filename);
     $image->setOriginalname($image->getImg()->getClientOriginalName());
 }