diff options
Diffstat (limited to 'src/controller/ajax.php')
-rw-r--r-- | src/controller/ajax.php | 42 |
1 files changed, 31 insertions, 11 deletions
diff --git a/src/controller/ajax.php b/src/controller/ajax.php index 943c027..6813d45 100644 --- a/src/controller/ajax.php +++ b/src/controller/ajax.php | |||
@@ -9,15 +9,17 @@ use App\Entity\Article; | |||
9 | 9 | ||
10 | 10 | ||
11 | // mettre ça ailleurs | 11 | // mettre ça ailleurs |
12 | function imagickCleanImage(string $image_data, string $local_path): bool // "string" parce que file_get_contents... | 12 | function imagickCleanImage(string $image_data, string $local_path, string $format = 'jpeg'): bool // "string" parce que file_get_contents... |
13 | { | 13 | { |
14 | try{ | 14 | try{ |
15 | $imagick = new Imagick(); | 15 | $imagick = new Imagick(); |
16 | $imagick->readImageBlob($image_data); | 16 | $imagick->readImageBlob($image_data); |
17 | $imagick->stripImage(); // nettoyage métadonnées | 17 | $imagick->stripImage(); // nettoyage métadonnées |
18 | $imagick->setImageFormat('jpeg'); | 18 | $imagick->setImageFormat($format); |
19 | $imagick->setImageCompression(Imagick::COMPRESSION_JPEG); | 19 | if($format === 'jpeg'){ |
20 | $imagick->setImageCompressionQuality(85); // optionnel | 20 | $imagick->setImageCompression(Imagick::COMPRESSION_JPEG); |
21 | $imagick->setImageCompressionQuality(85); // optionnel | ||
22 | } | ||
21 | $imagick->writeImage($local_path); // enregistrement | 23 | $imagick->writeImage($local_path); // enregistrement |
22 | $imagick->clear(); | 24 | $imagick->clear(); |
23 | $imagick->destroy(); | 25 | $imagick->destroy(); |
@@ -75,18 +77,23 @@ if(strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') !== false && isset($_ | |||
75 | mkdir($dest_mini, 0700, true); | 77 | mkdir($dest_mini, 0700, true); |
76 | } | 78 | } |
77 | 79 | ||
78 | $filePath = $dest . basename($file['name']); | 80 | $allowed_extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'tif']; |
81 | $name = Security::secureFileName(pathinfo($file['name'], PATHINFO_FILENAME)); | ||
82 | $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); | ||
83 | if(!in_array($extension, $allowed_extensions) || $extension === 'jpg'){ | ||
84 | $extension = 'jpeg'; | ||
85 | } | ||
86 | $file_path = $dest . $name . '_' . uniqid() . '.' . $extension; | ||
79 | 87 | ||
80 | // créer une miniature de l'image | 88 | // créer une miniature de l'image |
81 | // | 89 | // |
82 | 90 | ||
83 | if(move_uploaded_file($file['tmp_name'], $filePath)) { | 91 | if(imagickCleanImage(file_get_contents($file['tmp_name']), $file_path, $extension)){ // recréer l’image pour la nettoyer |
84 | $image_url = str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']); | 92 | echo json_encode(['location' => $file_path]); // renvoyer l'URL de l'image téléchargée |
85 | echo json_encode(['location' => $image_url . $filePath]); // renvoyer l'URL de l'image téléchargée | ||
86 | } | 93 | } |
87 | else{ | 94 | else{ |
88 | http_response_code(500); | 95 | http_response_code(500); |
89 | echo json_encode(['message' => 'Erreur 500: Internal Server Error']); | 96 | echo json_encode(['message' => 'Erreur image non valide']); |
90 | } | 97 | } |
91 | } | 98 | } |
92 | else{ | 99 | else{ |
@@ -101,15 +108,28 @@ elseif(isset($_GET['action']) && $_GET['action'] == 'upload_image_url'){ | |||
101 | 108 | ||
102 | if(isset($json['image_url'])){ | 109 | if(isset($json['image_url'])){ |
103 | $image_data = curlDownloadImage($json['image_url']); // téléchargement de l’image par le serveur avec cURL au lieu de file_get_contents | 110 | $image_data = curlDownloadImage($json['image_url']); // téléchargement de l’image par le serveur avec cURL au lieu de file_get_contents |
111 | $dest = 'images/'; | ||
104 | 112 | ||
113 | if(!is_dir($dest)) { // Vérifier si le répertoire existe, sinon le créer | ||
114 | mkdir($dest, 0777, true); | ||
115 | } | ||
116 | |||
105 | if($image_data === false){ | 117 | if($image_data === false){ |
106 | http_response_code(400); | 118 | http_response_code(400); |
107 | echo json_encode(['message' => "Erreur, le serveur n'a pas réussi à télécharger l'image."]); | 119 | echo json_encode(['message' => "Erreur, le serveur n'a pas réussi à télécharger l'image."]); |
108 | die; | 120 | die; |
109 | } | 121 | } |
110 | 122 | ||
111 | $local_path = 'images/' . uniqid() . '.jpg'; | 123 | $allowed_extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'tif']; |
112 | if(imagickCleanImage($image_data, $local_path)){ // recréer l’image pour la nettoyer | 124 | $url_path = parse_url($json['image_url'], PHP_URL_PATH); |
125 | $name = Security::secureFileName(pathinfo($url_path, PATHINFO_FILENAME)); | ||
126 | $extension = strtolower(pathinfo($url_path, PATHINFO_EXTENSION)); | ||
127 | if(!in_array($extension, $allowed_extensions) || $extension === 'jpg'){ | ||
128 | $extension = 'jpeg'; | ||
129 | } | ||
130 | $local_path = $dest . $name . '_' . uniqid() . '.' . $extension; | ||
131 | |||
132 | if(imagickCleanImage($image_data, $local_path, $extension)){ // recréer l’image pour la nettoyer | ||
113 | echo json_encode(['location' => $local_path]); // nouvelle adresse | 133 | echo json_encode(['location' => $local_path]); // nouvelle adresse |
114 | } | 134 | } |
115 | else{ | 135 | else{ |