-
Notifications
You must be signed in to change notification settings - Fork 39
/
PictureService.php
112 lines (90 loc) · 3.38 KB
/
PictureService.php
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
<?php
namespace App\Service;
use Exception;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class PictureService
{
private $params;
public function __construct(ParameterBagInterface $params)
{
$this->params = $params;
}
public function add(UploadedFile $picture, ?string $folder = '', ?int $width = 250, ?int $height = 250)
{
// On donne un nouveau nom à l'image
$fichier = md5(uniqid(rand(), true)) . '.webp';
// On récupère les infos de l'image
$picture_infos = getimagesize($picture);
if($picture_infos === false){
throw new Exception('Format d\'image incorrect');
}
// On vérifie le 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;
default:
throw new Exception('Format d\'image incorrect');
}
// On recadre l'image
// On récupère les dimensions
$imageWidth = $picture_infos[0];
$imageHeight = $picture_infos[1];
// On vérifie l'orientation de l'image
switch ($imageWidth <=> $imageHeight){
case -1: // portrait
$squareSize = $imageWidth;
$src_x = 0;
$src_y = ($imageHeight - $squareSize) / 2;
break;
case 0: // carré
$squareSize = $imageWidth;
$src_x = 0;
$src_y = 0;
break;
case 1: // paysage
$squareSize = $imageHeight;
$src_x = ($imageWidth - $squareSize) / 2;
$src_y = 0;
break;
}
// On crée une nouvelle image "vierge"
$resized_picture = imagecreatetruecolor($width, $height);
imagecopyresampled($resized_picture, $picture_source, 0, 0, $src_x, $src_y, $width, $height, $squareSize, $squareSize);
$path = $this->params->get('images_directory') . $folder;
// On crée le dossier de destination s'il n'existe pas
if(!file_exists($path . '/mini/')){
mkdir($path . '/mini/', 0755, true);
}
// On stocke l'image recadrée
imagewebp($resized_picture, $path . '/mini/' . $width . 'x' . $height . '-' . $fichier);
$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;
$mini = $path . '/mini/' . $width . 'x' . $height . '-' . $fichier;
if(file_exists($mini)){
unlink($mini);
$success = true;
}
$original = $path . '/' . $fichier;
if(file_exists($original)){
unlink($original);
$success = true;
}
return $success;
}
return false;
}
}