src/Service/UploadListener.php line 87

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Doctrine\ORM\EntityManager;
  4. use Oneup\UploaderBundle\Event\PostPersistEvent;
  5. use Symfony\Component\Filesystem\Filesystem;
  6. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  7. use Symfony\Component\DependencyInjection\ContainerInterface as Container;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use App\Service\MessageService;
  10. class UploadListener
  11. {
  12.     private $em;
  13.     private $token;
  14.     private $requeststack;
  15.     private $wss;
  16.     private $container;
  17.     public function __construct(EntityManager $emTokenStorageInterface $token_storageRequestStack $requeststackMessageService $wssContainer $container)
  18.     {
  19.         $this->em $em;
  20.         $this->token $token_storage;
  21.         $this->requeststack $requeststack;
  22.         $this->wss $wss;
  23.         $this->container $container;
  24.     }
  25.     protected function getHeight($image) {
  26.         $size getimagesize($image);
  27.         $height $size[1];
  28.         return $height;
  29.     }
  30.     // Cacul de la largeur
  31.     protected function getWidth($image) {
  32.         $size getimagesize($image);
  33.         $width $size[0];
  34.         return $width;
  35.     }
  36.     protected function resizeImage($image,$width,$height,$scale) {
  37.         list($imagewidth$imageheight$imageType) = getimagesize($image);
  38.         $imageType image_type_to_mime_type($imageType);
  39.         $newImageWidth ceil($width $scale);
  40.         $newImageHeight ceil($height $scale);
  41.         $newImage imagecreatetruecolor($newImageWidth,$newImageHeight);
  42.         switch($imageType) {
  43.             case "image/gif":
  44.                 $source=imagecreatefromgif($image);
  45.                 break;
  46.             case "image/pjpeg":
  47.             case "image/jpeg":
  48.             case "image/jpg":
  49.                 $source=imagecreatefromjpeg($image);
  50.                 break;
  51.             case "image/png":
  52.             case "image/x-png":
  53.                 $source=imagecreatefrompng($image);
  54.                 break;
  55.         }
  56.         $newImage imagecreatetruecolor$newImageWidth$newImageHeight );
  57.         imagealphablending$newImagefalse );
  58.         imagesavealpha$newImagetrue );
  59.         imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
  60.         switch($imageType) {
  61.             case "image/gif":
  62.                 imagegif($newImage,$image);
  63.                 break;
  64.             case "image/pjpeg":
  65.             case "image/jpeg":
  66.             case "image/jpg":
  67.                 imagejpeg($newImage,$image,90);
  68.                 break;
  69.             case "image/png":
  70.             case "image/x-png":
  71.                 imagepng($newImage,$image);
  72.                 break;
  73.         }
  74.         chmod($image0640);
  75.         return $image;
  76.     }
  77.     public function onUpload(PostPersistEvent $event)
  78.     {
  79.         $type=$event->getType();
  80.         switch($type) {
  81.             case "avatar":
  82.                 $session $this->requeststack->getSession();
  83.                 $file=$event->getFile();
  84.                 $filename=$file->getFilename();
  85.                 $session->set('uploadavatar'$filename);
  86.             break;
  87.             case "file":
  88.                 $file=$event->getFile();
  89.                 $filename=$file->getFilename();
  90.                 $pathname=$file->getPath();
  91.                 // Déplacer le fichier dans la cible
  92.                 $request $event->getRequest();
  93.                 $directory $request->get('directory');
  94.                 $subdirectory $request->get('subdirectory');
  95.                 if($subdirectory$directory.="/".$subdirectory;
  96.                 
  97.                 $fs = new Filesystem();
  98.                 $fgexit=$fs->exists($pathname."/".$directory."/".$filename);
  99.                 $file->move($pathname."/".$directory$filename);
  100.                 // Création d'un thumb dans le cas d'un fichier de type imapge
  101.                 if (in_array(strtolower($file->GetExtension()), array('jpg''jpeg''jpe''png''gif''bmp'))) {
  102.                     $fs = new Filesystem();
  103.                     $fs->copy($pathname."/".$directory."/".$filename,$pathname."/".$directory."/thumb/".$filename);
  104.                     $max_width=300;
  105.                     $width $this->getWidth($pathname."/".$directory."/thumb/".$filename);
  106.                     $height $this->getHeight($pathname."/".$directory."/thumb/".$filename);                    
  107.                     $scale $max_width/$width;
  108.                     $this->resizeImage($pathname."/".$directory."/thumb/".$filename,$width,$height,$scale);
  109.                 }
  110.                 if (in_array(strtolower($file->GetExtension()), array('pdf'))) {
  111.                     $im = new \Imagick();
  112.                     $im->setResolution(300300);
  113.                     $im->readImage($pathname."/".$directory."/".$filename."[0]");
  114.                     $im->setImageBackgroundColor('#ffffff');
  115.                     $im $im->flattenImages();
  116.                     $im->setImageFormat('jpeg');
  117.                     $fs->mkdir($pathname."/".$directory."/thumb");
  118.                     $im->writeImage($pathname."/".$directory."/thumb/".$filename.".jpg");
  119.                     $fs->rename($pathname."/".$directory."/thumb/".$filename.".jpg"$pathname."/".$directory."/thumb/".$filename);
  120.                 }
  121.                 if($fs->exists($pathname."/".$directory."/thumb/".$filename)) {
  122.                     $fs->mkdir($pathname."/".$directory."/thumbmini");
  123.                     $fs->copy($pathname."/".$directory."/thumb/".$filename,$pathname."/".$directory."/thumbmini/".$filename);
  124.                     $max_width=60;
  125.                     $width $this->getWidth($pathname."/".$directory."/thumbmini/".$filename);
  126.                     $height $this->getHeight($pathname."/".$directory."/thumbmini/".$filename);                    
  127.                     $scale $max_width/$width;
  128.                     $this->resizeImage($pathname."/".$directory."/thumbmini/".$filename,$width,$height,$scale);
  129.                 }
  130.                 if (strpos($directory'widget') === 0) {
  131.                     $tmp=explode("-",$directory);
  132.                     $widgetid=$tmp[1];
  133.                     $widget=$this->em->getRepository("App\Entity\Pagewidget")->find($widgetid);
  134.                     if($widget) {
  135.                         foreach($widget->getPage()->getGroups() as $group) {
  136.                             if($group->getFgcanshare()) {
  137.                                 if($fgexit$message="Modification fichier<br>".$filename;
  138.                                 else $message="Création fichier<br>".$filename;
  139.                                 $this->wss->addMessage($this->token->getToken()->getUser()->getApikey(),$group->getId(),$message);
  140.                             }
  141.                         }
  142.                     }
  143.                 }
  144.                 if (strpos($directory'projecttask') === 0) {
  145.                     $tmp=explode("-",$directory);
  146.                     $projecttaskid=$tmp[1];
  147.                     $projecttask=$this->em->getRepository("App\Entity\Projecttask")->find($projecttaskid);
  148.                     if($projecttask) {
  149.                         foreach($projecttask->getProject()->getGroups() as $group) {
  150.                             if($group->getFgcanshare()) {
  151.                                 if($fgexit$message="Modification fichier dans la tâche ".$projecttask->getName()."<br>".$filename;
  152.                                 else $message="Création fichier dans la tâche ".$projecttask->getName()."<br>".$filename;
  153.                                 $this->wss->addMessage($this->token->getToken()->getUser()->getApikey(),$group->getId(),$message);
  154.                             }
  155.                         }
  156.                     }
  157.                 }
  158.                 $response $event->getResponse();
  159.                 $response['file'] = $filename;
  160.             break;
  161.             case "blogarticle":
  162.                 $file=$event->getFile();
  163.                 $filename=$file->getFilename();
  164.                 $pathname=$file->getPath();
  165.                 // Creation d'un thumb
  166.                 $fs = new Filesystem();
  167.                 $fs->copy($pathname."/".$filename,$pathname."/thumb-".$filename);
  168.                 $max_width=350;
  169.                 $width $this->getWidth($pathname."/thumb-".$filename);
  170.                 $height $this->getHeight($pathname."/thumb-".$filename);                    
  171.                 $scale $max_width/$width;
  172.                 $this->resizeImage($pathname."/thumb-".$filename,$width,$height,$scale);
  173.                 $response $event->getResponse();
  174.                 $response['file'] = $filename;                
  175.             break;
  176.             default:
  177.                 $file=$event->getFile();
  178.                 $filename=$file->getFilename();
  179.                 $response $event->getResponse();
  180.                 $response['file'] = $filename;
  181.             break;            
  182.         }
  183.     }
  184. }