From f27cca08ff786ceff1870147f6d6bad14d71117f Mon Sep 17 00:00:00 2001 From: polo Date: Fri, 23 Oct 2026 00:47:32 +0200 Subject: =?UTF-8?q?dossiers=20avec=20majuscule,=20namespace=20pour=20toute?= =?UTF-8?q?s=20les=20classes,=20autoload=20psr-4,=20UserDataService::creat?= =?UTF-8?q?eZip=20d=C3=A9plac=C3=A9=20dans=20Backup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/ImageUploadController.php | 191 ------------------------------- 1 file changed, 191 deletions(-) delete mode 100644 src/controller/ImageUploadController.php (limited to 'src/controller/ImageUploadController.php') diff --git a/src/controller/ImageUploadController.php b/src/controller/ImageUploadController.php deleted file mode 100644 index af9a553..0000000 --- a/src/controller/ImageUploadController.php +++ /dev/null @@ -1,191 +0,0 @@ -readImageBlob($image_data); - $imagick->stripImage(); // nettoyage métadonnées - //$imagick->setImageFormat($format); // inutile, writeImage force la conversion - - // compression - switch($format){ - case 'jpeg': // particularité du switch, si 'jpeg' le test de 'jpg' est ignoré et on va jusqu'au break - case 'jpg': - $imagick->setImageCompression(Imagick::COMPRESSION_JPEG); - $imagick->setImageCompressionQuality(85); - break; - case 'webp': - $imagick->setImageCompression(Imagick::COMPRESSION_WEBP); - $imagick->setImageCompressionQuality(85); - break; - case 'png': - $imagick->setImageCompression(Imagick::COMPRESSION_ZIP); - $imagick->setImageCompressionQuality(7); // 9 est sans perte - break; - case 'tiff': - $imagick->setImageCompression(Imagick::COMPRESSION_LZW); // LZW est sans perte - break; - } - - // enregistrement - // writeImage utilise l'extension du fichier et ignore le format détecté - // imagemagick est à l'origine une appli console, elle considère que l'extension montre l'intention de l'utilisateur - $imagick->writeImage($local_path); // enregistrement - $imagick->clear(); - $imagick->destroy(); - return true; - } - catch(Exception $e){ - return false; - } - } - static public function curlDownloadImage(string $url, int $maxRetries = 3, int $timeout = 10): string|false - { - $attempt = 0; - $imageData = false; - - while($attempt < $maxRetries){ - $ch = curl_init($url); // instance de CurlHandle - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); - curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); - curl_setopt($ch, CURLOPT_USERAGENT, 'TinyMCE-Image-Downloader'); - - $imageData = curl_exec($ch); - $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - //$curlError = curl_error($ch); - - if($imageData !== false && $httpCode >= 200 && $httpCode < 300){ - return $imageData; - } - - $attempt++; - sleep(1); - } - - return false; // échec après trois tentatives - } - - // téléchargement par le plugin (bouton "insérer une image") - static public function imageUploadTinyMce(): JsonResponse - { - if(!isset($_FILES['file'])){ - return new JsonResponse(['message' => 'Erreur 400: Bad Request'], JsonResponse::HTTP_BAD_REQUEST); // code 400 - } - if(!is_uploaded_file($_FILES['file']['tmp_name'])) { - return new JsonResponse(['message' => "Le fichier n'a pas été téléchargé correctement."], JsonResponse::HTTP_BAD_REQUEST); // code 400 - } - - $dest = 'user_data/images/'; - $dest_mini = 'user_data/images-mini/'; - - // Vérifier si les répertoires existent, sinon les créer - if(!is_dir($dest)){ - mkdir($dest, 0755, true); - } - if(!is_dir($dest_mini)){ - mkdir($dest_mini, 0755, true); - } - - $name = Security::secureFileName(pathinfo($_FILES['file']['name'], PATHINFO_FILENAME)); - $extension = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION)); - $image_data = file_get_contents($_FILES['file']['tmp_name']); - if(!in_array($extension, self::ALLOWED_EXTENSIONS)){ - $extension = 'jpeg'; - } - $local_path = uniqid($dest . $name . '_') . '.' . $extension; - - // créer une miniature de l'image - // - - if(self::imagickCleanAndWriteImage($image_data, $local_path)){ // recréer l’image pour la nettoyer - return new JsonResponse(['location' => $local_path]); // renvoyer l'URL de l'image téléchargée - } - else{ - return new JsonResponse(['message' => 'Erreur image non valide'], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); // code 500 - } - } - - // collage de HTML => recherche de balises , téléchargement côté serveur et renvoi de l'adresse sur le serveur - static public function uploadImageHtml(): JsonResponse - { - $json = json_decode(file_get_contents('php://input'), true); - - if(!isset($json['image_url'])){ - return new JsonResponse(['message' => "Erreur 400: Bad Request"], JsonResponse::HTTP_BAD_REQUEST); // code 400 - } - - $image_data = self::curlDownloadImage($json['image_url']); // téléchargement de l’image par le serveur avec cURL au lieu de file_get_contents - if(!$image_data){ - return new JsonResponse(['message' => "Erreur, le serveur n'a pas réussi à télécharger l'image."], JsonResponse::HTTP_BAD_REQUEST); // code 400 - } - - $dest = 'user_data/images/'; - if(!is_dir($dest)){ // Vérifier si le répertoire existe, sinon le créer - mkdir($dest, 0755, true); - } - - $url_path = parse_url($json['image_url'], PHP_URL_PATH); - $name = Security::secureFileName(pathinfo($url_path, PATHINFO_FILENAME)); - $extension = strtolower(pathinfo($url_path, PATHINFO_EXTENSION)); - if(!in_array($extension, self::ALLOWED_EXTENSIONS) || $extension === 'jpg'){ - $extension = 'jpeg'; - } - $local_path = uniqid($dest . $name . '_') . '.' . $extension; - - if(self::imagickCleanAndWriteImage($image_data, $local_path)){ // recréer l’image pour la nettoyer - return new JsonResponse(['location' => $local_path]); // nouvelle adresse - } - else{ - return new JsonResponse(['message' => 'Erreur image non valide', 'format' => $extension], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); // code 500 - } - } - - // collage simple d'une image (base64 dans le presse-papier) non encapsulée dans du HTML - static public function uploadImageBase64(): JsonResponse - { - $json = json_decode(file_get_contents('php://input'), true); - $dest = 'user_data/images/'; - - if(!is_dir($dest)){ - mkdir($dest, 0755, true); - } - - // détection de data:image/ et de ;base64, et capture du format dans $type - if(!isset($json['image_base64']) || !preg_match('/^data:image\/(\w+);base64,/', $json['image_base64'], $type)){ - return new JsonResponse(['message' => 'Données image base64 manquantes ou invalides'], JsonResponse::HTTP_BAD_REQUEST); // code 400 - } - - $extension = strtolower($type[1]); // dans (\w+) - if(!in_array($extension, self::ALLOWED_EXTENSIONS)){ - $extension = 'jpeg'; - } - - $name = 'pasted_image'; - $image_data = base64_decode(substr($json['image_base64'], strpos($json['image_base64'], ',') + 1)); // découpe la chaine à la virgule puis convertit en binaire - if($image_data === false){ - return new JsonResponse(['message' => 'Décodage base64 invalide'], JsonResponse::HTTP_BAD_REQUEST); // code 400 - } - $local_path = uniqid($dest . $name . '_') . '.' . $extension; - - if(self::imagickCleanAndWriteImage($image_data, $local_path)){ - return new JsonResponse(['location' => $local_path]); - } - else{ - return new JsonResponse(['message' => 'Erreur image non valide', 'format' => $extension], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); // code 500 - } - } -} \ No newline at end of file -- cgit v1.2.3