aboutsummaryrefslogtreecommitdiff
path: root/src/controller/ImageUploadController.php
blob: af9a5532405cfe8192bed76ad053bb93aac77fdf (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
// src/controller/ImageUploadController.php

declare(strict_types=1);

use Symfony\Component\HttpFoundation\JsonResponse;

class ImageUploadController
{
	const ALLOWED_EXTENSIONS = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'tif'];

	static public function imagickCleanAndWriteImage(string $image_data, string $local_path): bool // "string" parce que file_get_contents...
	{
		$format = strtolower(pathinfo($local_path)['extension']);
	    try{
	        $imagick = new Imagick();
	        $imagick->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 <img>, 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
	    }
	}
}