public function uploadAction() { /** @var $request Request */ $request = $this->get('request'); /** @var $user \Vendor\GalleryBundle\Entity\User */ $user = $this->get('security.context')->getToken()->getUser(); $userName = $user->getUsername(); $data = $request->request->all(); if (!$request->isMethod("post")) { return $this->renderUploadTemplate(new Img()); } if (!$data['title']) { return $this->renderUploadTemplate(new Img(), array('info' => 'Uzupełnij tytuł użytkowniku ' . $userName)); } $file = $request->files->get('file0'); if (!$file->getClientOriginalName()) { $img = new Img(); $img->setTitle($data['title']); return $this->renderUploadTemplate($img, array('info' => 'Uzupełnij zdjęcie użytkowniku ' . $userName)); } try { $this->getImageService()->uploadAndCreateThumbnail($request); return $this->redirect($this->generateUrl('vendor_gallery_homepage')); } catch (FileUploadException $e) { return $this->renderUploadTemplate(new Img(), array('errors' => $e->getMessage())); } }
private function createThumbnail(Img $img, $newHeight, $newWidth) { $tmpImgPath = $img->getPath(); $fullPath = $this->getBaseWebPath() . $tmpImgPath; $image = $this->imageCreateFromFile($fullPath); $newImage = imagecreatetruecolor($newWidth, $newHeight); $bundlePath = $this->getBaseWebPath() . $this->getBundleWebPath(); $thumbnailName = $newHeight . 'x' . $newWidth . $img->getFileName(); $serverPath = $bundlePath . $thumbnailName; $webPath = $this->getBundleWebPath() . $thumbnailName; $this->scaleImage($image, $newImage); imagejpeg($newImage, $serverPath); $thumbnail = new ImgThumbnail(); $thumbnail->setImage($img); $thumbnail->setHeight($newHeight); $thumbnail->setWidth($newWidth); $thumbnail->setPath($webPath); return $thumbnail; }