/**
  * @param DownloadSession $downloadSession
  *
  * @return resource
  */
 public function resizeLogo(DownloadSession $downloadSession)
 {
     $suffix = "x" . $downloadSession->getWidth();
     // Get new sizes
     $image = $this->createImage($downloadSession->getLogo(), $suffix);
     $width = imagesx($image);
     $height = imagesy($image);
     $percent = $downloadSession->getWidth() / $width;
     $percent = round($percent, 4);
     $newwidth = round($width * $percent);
     $newheight = round($height * $percent);
     // Resize
     $resize = imagecreatetruecolor($newwidth, $newheight);
     imagealphablending($resize, false);
     imagesavealpha($resize, true);
     $transparent = imagecolorallocatealpha($resize, 255, 255, 255, 127);
     imagefilledrectangle($resize, 0, 0, $newwidth, $newheight, $transparent);
     imagecopyresampled($resize, $image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
     //Far better quality than the other one
     $logopath = $this->renameWithSuffix($downloadSession->getLogo()->getWebPath(), $suffix);
     // Output and free memory
     imagepng($resize, $logopath);
     return $resize;
 }
 public function upload(DownloadSession $downloadSession)
 {
     // the file property can be empty if the field is not required
     if (null === $this->getFile()) {
         return false;
     }
     // use the original file name here but you should
     // sanitize it at least to avoid any security issues
     $path = $downloadSession->getId();
     if (!is_dir($this->getUploadRootDir() . '/' . $path)) {
         if (!mkdir($this->getUploadRootDir() . '/' . $path)) {
             throw new \Exception('Create dir exception');
         }
     }
     $filename = sha1(uniqid(mt_rand(), true)) . '.' . $this->getFile()->guessExtension();
     // move takes the target directory and then the
     // target filename to move to
     $this->getFile()->move($this->getUploadRootDir() . '/' . $path, $filename);
     // set the path property to the filename where you've saved the file
     $this->path = $path . '/' . $filename;
     // clean up the file property as you won't need it anymore
     $this->file = null;
     return true;
 }
 public function uploadFilesAction(Request $request, $width, $position, $logo_id)
 {
     if (!$request->isXmlHttpRequest()) {
         throw $this->createAccessDeniedException();
     }
     $em = $this->getDoctrine()->getEntityManager();
     $pm = $this->getPicturesManager();
     $downloadsession = null;
     $code = "error";
     $message = "";
     /** @var Logo $logo */
     $logo = $this->getDoctrine()->getManager()->getRepository('MainBundle:Logo')->find($logo_id);
     if (!$logo) {
         throw $this->createNotFoundException();
     }
     if ($request->getSession()->get('downloadSession_id')) {
         /** @var DownloadSession $downloadsession */
         $downloadsession = $this->getDoctrine()->getManager()->getRepository('MainBundle:DownloadSession')->find($request->getSession()->get('downloadSession_id'));
     }
     if (!$downloadsession) {
         $downloadsession = new DownloadSession();
         $downloadsession->setOwner($this->getUser());
         $downloadsession->setLogo($logo);
         $downloadsession->setCreatedAt(new \DateTime("now"));
         $downloadsession->setWidth($width);
         $downloadsession->setPosition($position);
         $em->persist($downloadsession);
         $em->flush();
         $request->getSession()->set('downloadSession_id', $downloadsession->getId());
     }
     if (isset($_FILES['upl'])) {
         if ($_FILES['upl']['error'] == 0) {
             $extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);
             if (in_array(strtolower($extension), Picture::$AUTHORIZED_EXTENSION)) {
                 $code = "success";
             }
             //Image is ready to upload
             if ($code != "error") {
                 $picture = new Picture();
                 $picture->setCreatedAt(new \DateTime("now"));
                 $picture->setName($_FILES['upl']['name']);
                 /** @var UploadedFile $file */
                 $file = new UploadedFile($_FILES['upl']['tmp_name'], $_FILES['upl']['name']);
                 $picture->setFile($file);
                 if ($picture->upload($downloadsession)) {
                     //Resize, crop and merge with logo
                     $pm->resizeAndCrop($picture, $downloadsession->getWidth());
                     $pm->mergeWithLogo($picture, $downloadsession);
                     $code = "success";
                 }
                 $em->persist($picture);
                 $request->getSession()->set('path', $picture->getUploadDir());
                 $downloadsession->addPicture($picture);
                 $em->flush();
             }
         } else {
             switch ($_FILES['upl']['error']) {
                 case UPLOAD_ERR_INI_SIZE:
                     $message = "La taille du fichier téléchargé excède la valeur de upload_max_filesize (" . ini_get('upload_max_filesize') . "), configurée dans le php.ini";
                     break;
                 case UPLOAD_ERR_FORM_SIZE:
                     $message = "La taille du fichier téléchargé excède la valeur de MAX_FILE_SIZE (" . ini_get('MAX_FILE_SIZE') . "), qui a été spécifiée dans le formulaire HTML";
                     break;
                 case UPLOAD_ERR_PARTIAL:
                     $message = "Le fichier n'a été que partiellement téléchargé";
                     break;
                 case UPLOAD_ERR_NO_FILE:
                     $message = "Aucun fichier n'a été téléchargé";
                     break;
                 case UPLOAD_ERR_NO_TMP_DIR:
                     $message = "Un dossier temporaire est manquant. Introduit en PHP 5.0.3.";
                     break;
                 case UPLOAD_ERR_CANT_WRITE:
                     $message = "Échec de l'écriture du fichier sur le disque. Introduit en PHP 5.1.0.";
                     break;
                 case UPLOAD_ERR_EXTENSION:
                     $message = "Une extension PHP a arrêté l'envoi de fichier. PHP ne propose aucun moyen de déterminer quelle extension est en cause. L'examen du phpinfo() peut aider. Introduit en PHP 5.2.0.";
                     break;
                 default:
                     $message = "Autre probleme";
                     break;
             }
         }
     } else {
         $message = "No uploaded Files";
     }
     return new JsonResponse(array('code' => $code, 'message' => $message));
 }