ninesurvey/src/ninesurvey-1.0/src/Service/uploadListener.php

100 lines
3.1 KiB
PHP

<?php
namespace App\Service;
use Doctrine\ORM\EntityManager;
use Oneup\UploaderBundle\Event\PostPersistEvent;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
class uploadListener
{
private $em;
private $session;
private $token;
private $container;
public function __construct(EntityManager $em, TokenStorageInterface $token_storage, Session $session, Container $container)
{
$this->em = $em;
$this->session = $session;
$this->token = $token_storage;
$this->container = $container;
}
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) {
default:
$file=$event->getFile();
$filename=$file->getFilename();
$response = $event->getResponse();
$response['file'] = $filename;
break;
}
}
}