om = $om; $this->token_storage = $token_storage; } protected function getHeight($image) { $size = getimagesize($image); $height = $size[1]; return $height; } // Cacul de la largeur protected function getWidth($image) { $size = getimagesize($image); $width = $size[0]; return $width; } protected function resizeImage($image,$width,$height,$scale) { list($imagewidth, $imageheight, $imageType) = getimagesize($image); $imageType = image_type_to_mime_type($imageType); $newImageWidth = ceil($width * $scale); $newImageHeight = ceil($height * $scale); $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight); switch($imageType) { case "image/gif": $source=imagecreatefromgif($image); break; case "image/pjpeg": case "image/jpeg": case "image/jpg": $source=imagecreatefromjpeg($image); break; case "image/png": case "image/x-png": $source=imagecreatefrompng($image); break; } $newImage = imagecreatetruecolor( $newImageWidth, $newImageHeight ); imagealphablending( $newImage, false ); imagesavealpha( $newImage, true ); imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height); switch($imageType) { case "image/gif": imagegif($newImage,$image); break; case "image/pjpeg": case "image/jpeg": case "image/jpg": imagejpeg($newImage,$image,90); break; case "image/png": case "image/x-png": imagepng($newImage,$image); break; } chmod($image, 0640); return $image; } public function onUpload(PostPersistEvent $event) { $type=$event->getType(); switch($type) { case "avatar": $file=$event->getFile(); $filename=$file->getFilename(); $session = new Session(); $session->set('uploadavatar', $filename); break; case "file": $file=$event->getFile(); $filename=$file->getFilename(); $pathname=$file->getPath(); // Déplacer le fichier dans la cible $request = $event->getRequest(); $directory = $request->get('directory'); $file->move($pathname."/".$directory, $filename); // Création d'un thumb dans le cas d'un fichier de type imapge if (in_array(strtolower($file->GetExtension()), array('jpg', 'jpeg', 'jpe', 'png', 'gif', 'bmp'))) { $fs = new Filesystem(); $fs->copy($pathname."/".$directory."/".$filename,$pathname."/".$directory."/thumb/".$filename); $max_width=350; $width = $this->getWidth($pathname."/".$directory."/thumb/".$filename); $height = $this->getHeight($pathname."/".$directory."/thumb/".$filename); $scale = $max_width/$width; $this->resizeImage($pathname."/".$directory."/thumb/".$filename,$width,$height,$scale); } $response = $event->getResponse(); $response['file'] = $filename; break; case "blogarticle": $file=$event->getFile(); $filename=$file->getFilename(); $pathname=$file->getPath(); // Creation d'un thumb $fs = new Filesystem(); $fs->copy($pathname."/".$filename,$pathname."/thumb-".$filename); $max_width=350; $width = $this->getWidth($pathname."/thumb-".$filename); $height = $this->getHeight($pathname."/thumb-".$filename); $scale = $max_width/$width; $this->resizeImage($pathname."/thumb-".$filename,$width,$height,$scale); $response = $event->getResponse(); $response['file'] = $filename; break; default: $file=$event->getFile(); $filename=$file->getFilename(); $response = $event->getResponse(); $response['file'] = $filename; break; } } }