1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| <?php
namespace App\Service;
use Exception;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use function PHPUnit\Framework\fileExists;
class PicturesService
{
private $params;
public function __construct(ParameterBagInterface $params)
{
$this->params = $params;
}
public function add(UploadedFile $picture, ?string $folder = '', ?int $width = 250, ?int $height = 250, $moveFile = true)
{
//Création d'un nouveau nom d'image
$fichier = md5(uniqid(rand(), true)) . '.webp';
//Récupération des infos de l'image
$picture_infos = getimagesize($picture);
if ($picture_infos === false) {
throw new Exception('Format d\'image incorrect');
}
//Vérification du format de l'image
switch ($picture_infos['mime']) {
case 'image/png':
$picture_source = imagecreatefrompng($picture);
break;
case 'image/jpeg':
$picture_source = imagecreatefromjpeg($picture);
break;
case 'image/webp':
$picture_source = imagecreatefromwebp($picture);
break;
case 'image/gif':
$picture_source = imagecreatefromwebp($picture);
break;
default:
throw new Exception('Format d\'image incorrect');
}
//Recadrer l'image et récupération des dimensions
$imageWidth = $picture_infos[0];
$imageHeight = $picture_infos[1];
//Vérification de l'orientation de l'image
switch ($imageWidth <=> $imageHeight) {
case -1: //Portrait
$squareSize = $imageWidth;
$src_x = 0;
$src_y = (int)($imageHeight - $squareSize) / 2;
break;
case 0: //Carré
$squareSize = $imageWidth;
$src_x = 0;
$src_y = 0;
break;
case 1: //Paysage
$squareSize = $imageHeight;
$src_x = (int)($imageWidth - $squareSize) / 2;
$src_y = 0;
break;
}
//Création d'une nouvelle image vierge
$resized_picture = imagecreatetruecolor($width, $height);
imagecopyresampled($resized_picture, $picture_source, 0, 0, $src_x, $src_y, $width, $height, $squareSize, $squareSize);
//Création du chemin
$path = $this->params->get('images_directory') . $folder;
//Création du dossier
if (!file_exists($path . '/mini/')) {
mkdir($path . '/mini/', 0755, true);
}
//Stockage de l'image recadrée
imagewebp($resized_picture, $path . '/mini/' . $width . 'x' . $height . '-' . $fichier);
if ($moveFile) {
$picture->move($path . '/', $fichier);
}
return $fichier;
}
public function delete(string $fichier, ?string $folder = '', ?int $width = 250, ?int $height = 250)
{
if ($fichier !== 'default.webp') {
$success = false;
$path = $this->params->get('images_directory') . $folder;
$thumbnail = $path . '/mini/' . $width . 'x' . $height . '-' . $fichier;
if (fileExists($thumbnail)) {
unlink($thumbnail);
$success = true;
}
$original = $path . '/' . $fichier;
if (fileExists($original)) {
unlink($original);
$success = true;
}
return $success;
}
return false;
}
} |
Partager