Completed
Push — master ( b3a07f...3ef0d6 )
by cam
01:23
created
ecrire/inc/filtres_images_lib_mini.php 1 patch
Indentation   +1351 added lines, -1351 removed lines patch added patch discarded remove patch
@@ -18,7 +18,7 @@  discard block
 block discarded – undo
18 18
  */
19 19
 
20 20
 if (!defined('_ECRIRE_INC_VERSION')) {
21
-	return;
21
+    return;
22 22
 }
23 23
 include_spip('inc/filtres'); // par precaution
24 24
 include_spip('inc/filtres_images_mini'); // par precaution
@@ -38,21 +38,21 @@  discard block
 block discarded – undo
38 38
  *     Le code de la couleur en hexadécimal.
39 39
  */
40 40
 function _couleur_dec_to_hex($red, $green, $blue) {
41
-	$red = dechex($red);
42
-	$green = dechex($green);
43
-	$blue = dechex($blue);
44
-
45
-	if (strlen($red) == 1) {
46
-		$red = '0' . $red;
47
-	}
48
-	if (strlen($green) == 1) {
49
-		$green = '0' . $green;
50
-	}
51
-	if (strlen($blue) == 1) {
52
-		$blue = '0' . $blue;
53
-	}
54
-
55
-	return "$red$green$blue";
41
+    $red = dechex($red);
42
+    $green = dechex($green);
43
+    $blue = dechex($blue);
44
+
45
+    if (strlen($red) == 1) {
46
+        $red = '0' . $red;
47
+    }
48
+    if (strlen($green) == 1) {
49
+        $green = '0' . $green;
50
+    }
51
+    if (strlen($blue) == 1) {
52
+        $blue = '0' . $blue;
53
+    }
54
+
55
+    return "$red$green$blue";
56 56
 }
57 57
 
58 58
 /**
@@ -64,17 +64,17 @@  discard block
 block discarded – undo
64 64
  *     Un tableau des 3 éléments : rouge, vert, bleu.
65 65
  */
66 66
 function _couleur_hex_to_dec($couleur) {
67
-	$couleur = couleur_html_to_hex($couleur);
68
-	$couleur = ltrim($couleur, '#');
69
-	if (strlen($couleur) === 3) {
70
-		$couleur = $couleur[0] . $couleur[0] . $couleur[1] . $couleur[1] . $couleur[2] . $couleur[2];
71
-	}
72
-	$retour = [];
73
-	$retour['red'] = hexdec(substr($couleur, 0, 2));
74
-	$retour['green'] = hexdec(substr($couleur, 2, 2));
75
-	$retour['blue'] = hexdec(substr($couleur, 4, 2));
76
-
77
-	return $retour;
67
+    $couleur = couleur_html_to_hex($couleur);
68
+    $couleur = ltrim($couleur, '#');
69
+    if (strlen($couleur) === 3) {
70
+        $couleur = $couleur[0] . $couleur[0] . $couleur[1] . $couleur[1] . $couleur[2] . $couleur[2];
71
+    }
72
+    $retour = [];
73
+    $retour['red'] = hexdec(substr($couleur, 0, 2));
74
+    $retour['green'] = hexdec(substr($couleur, 2, 2));
75
+    $retour['blue'] = hexdec(substr($couleur, 4, 2));
76
+
77
+    return $retour;
78 78
 }
79 79
 
80 80
 
@@ -91,8 +91,8 @@  discard block
 block discarded – undo
91 91
  *     Le code de la couleur en hexadécimal.
92 92
  */
93 93
 function _couleur_hsl_to_hex($hue, $saturation, $lightness) {
94
-	$rgb = _couleur_hsl_to_rgb($hue, $saturation, $lightness);
95
-	return _couleur_dec_to_hex($rgb['r'], $rgb['g'], $rgb['b']);
94
+    $rgb = _couleur_hsl_to_rgb($hue, $saturation, $lightness);
95
+    return _couleur_dec_to_hex($rgb['r'], $rgb['g'], $rgb['b']);
96 96
 }
97 97
 
98 98
 /**
@@ -104,8 +104,8 @@  discard block
 block discarded – undo
104 104
  *     Un tableau des 3 éléments : teinte, saturation, luminosité.
105 105
  */
106 106
 function _couleur_hex_to_hsl($couleur) {
107
-	$rgb = _couleur_hex_to_dec($couleur);
108
-	return _couleur_rgb_to_hsl($rgb['red'], $rgb['green'], $rgb['blue']);
107
+    $rgb = _couleur_hex_to_dec($couleur);
108
+    return _couleur_rgb_to_hsl($rgb['red'], $rgb['green'], $rgb['blue']);
109 109
 }
110 110
 
111 111
 /**
@@ -120,59 +120,59 @@  discard block
 block discarded – undo
120 120
  * @return array
121 121
  */
122 122
 function _couleur_rgb_to_hsl($R, $G, $B) {
123
-	$H = null;
124
-	$var_R = ($R / 255); // Where RGB values = 0 ÷ 255
125
-	$var_G = ($G / 255);
126
-	$var_B = ($B / 255);
127
-
128
-	$var_Min = min($var_R, $var_G, $var_B);   //Min. value of RGB
129
-	$var_Max = max($var_R, $var_G, $var_B);   //Max. value of RGB
130
-	$del_Max = $var_Max - $var_Min;           //Delta RGB value
131
-
132
-	$L = ($var_Max + $var_Min) / 2;
133
-
134
-	if ($del_Max == 0) {
135
-		//This is a gray, no chroma...
136
-		$H = 0; //HSL results = 0 ÷ 1
137
-		$S = 0;
138
-	} else {
139
-		// Chromatic data...
140
-		if ($L < 0.5) {
141
-			$S = $del_Max / ($var_Max + $var_Min);
142
-		} else {
143
-			$S = $del_Max / (2 - $var_Max - $var_Min);
144
-		}
145
-
146
-		$del_R = ((($var_Max - $var_R) / 6) + ($del_Max / 2)) / $del_Max;
147
-		$del_G = ((($var_Max - $var_G) / 6) + ($del_Max / 2)) / $del_Max;
148
-		$del_B = ((($var_Max - $var_B) / 6) + ($del_Max / 2)) / $del_Max;
149
-
150
-		if ($var_R == $var_Max) {
151
-			$H = $del_B - $del_G;
152
-		} else {
153
-			if ($var_G == $var_Max) {
154
-				$H = (1 / 3) + $del_R - $del_B;
155
-			} else {
156
-				if ($var_B == $var_Max) {
157
-					$H = (2 / 3) + $del_G - $del_R;
158
-				}
159
-			}
160
-		}
161
-
162
-		if ($H < 0) {
163
-			$H += 1;
164
-		}
165
-		if ($H > 1) {
166
-			$H -= 1;
167
-		}
168
-	}
169
-
170
-	$ret = [];
171
-	$ret['h'] = $H;
172
-	$ret['s'] = $S;
173
-	$ret['l'] = $L;
174
-
175
-	return $ret;
123
+    $H = null;
124
+    $var_R = ($R / 255); // Where RGB values = 0 ÷ 255
125
+    $var_G = ($G / 255);
126
+    $var_B = ($B / 255);
127
+
128
+    $var_Min = min($var_R, $var_G, $var_B);   //Min. value of RGB
129
+    $var_Max = max($var_R, $var_G, $var_B);   //Max. value of RGB
130
+    $del_Max = $var_Max - $var_Min;           //Delta RGB value
131
+
132
+    $L = ($var_Max + $var_Min) / 2;
133
+
134
+    if ($del_Max == 0) {
135
+        //This is a gray, no chroma...
136
+        $H = 0; //HSL results = 0 ÷ 1
137
+        $S = 0;
138
+    } else {
139
+        // Chromatic data...
140
+        if ($L < 0.5) {
141
+            $S = $del_Max / ($var_Max + $var_Min);
142
+        } else {
143
+            $S = $del_Max / (2 - $var_Max - $var_Min);
144
+        }
145
+
146
+        $del_R = ((($var_Max - $var_R) / 6) + ($del_Max / 2)) / $del_Max;
147
+        $del_G = ((($var_Max - $var_G) / 6) + ($del_Max / 2)) / $del_Max;
148
+        $del_B = ((($var_Max - $var_B) / 6) + ($del_Max / 2)) / $del_Max;
149
+
150
+        if ($var_R == $var_Max) {
151
+            $H = $del_B - $del_G;
152
+        } else {
153
+            if ($var_G == $var_Max) {
154
+                $H = (1 / 3) + $del_R - $del_B;
155
+            } else {
156
+                if ($var_B == $var_Max) {
157
+                    $H = (2 / 3) + $del_G - $del_R;
158
+                }
159
+            }
160
+        }
161
+
162
+        if ($H < 0) {
163
+            $H += 1;
164
+        }
165
+        if ($H > 1) {
166
+            $H -= 1;
167
+        }
168
+    }
169
+
170
+    $ret = [];
171
+    $ret['h'] = $H;
172
+    $ret['s'] = $S;
173
+    $ret['l'] = $L;
174
+
175
+    return $ret;
176 176
 }
177 177
 
178 178
 
@@ -188,52 +188,52 @@  discard block
 block discarded – undo
188 188
  * @return array
189 189
  */
190 190
 function _couleur_hsl_to_rgb($H, $S, $L) {
191
-	// helper
192
-	$hue_2_rgb = function ($v1, $v2, $vH) {
193
-		if ($vH < 0) {
194
-			$vH += 1;
195
-		}
196
-		if ($vH > 1) {
197
-			$vH -= 1;
198
-		}
199
-		if ((6 * $vH) < 1) {
200
-			return ($v1 + ($v2 - $v1) * 6 * $vH);
201
-		}
202
-		if ((2 * $vH) < 1) {
203
-			return ($v2);
204
-		}
205
-		if ((3 * $vH) < 2) {
206
-			return ($v1 + ($v2 - $v1) * ((2 / 3) - $vH) * 6);
207
-		}
208
-
209
-		return ($v1);
210
-	};
211
-
212
-	if ($S == 0) {
213
-		// HSV values = 0 -> 1
214
-		$R = $L * 255;
215
-		$G = $L * 255;
216
-		$B = $L * 255;
217
-	} else {
218
-		if ($L < 0.5) {
219
-			$var_2 = $L * (1 + $S);
220
-		} else {
221
-			$var_2 = ($L + $S) - ($S * $L);
222
-		}
223
-
224
-		$var_1 = 2 * $L - $var_2;
225
-
226
-		$R = 255 * $hue_2_rgb($var_1, $var_2, $H + (1 / 3));
227
-		$G = 255 * $hue_2_rgb($var_1, $var_2, $H);
228
-		$B = 255 * $hue_2_rgb($var_1, $var_2, $H - (1 / 3));
229
-	}
230
-
231
-	$ret = [];
232
-	$ret['r'] = floor($R);
233
-	$ret['g'] = floor($G);
234
-	$ret['b'] = floor($B);
235
-
236
-	return $ret;
191
+    // helper
192
+    $hue_2_rgb = function ($v1, $v2, $vH) {
193
+        if ($vH < 0) {
194
+            $vH += 1;
195
+        }
196
+        if ($vH > 1) {
197
+            $vH -= 1;
198
+        }
199
+        if ((6 * $vH) < 1) {
200
+            return ($v1 + ($v2 - $v1) * 6 * $vH);
201
+        }
202
+        if ((2 * $vH) < 1) {
203
+            return ($v2);
204
+        }
205
+        if ((3 * $vH) < 2) {
206
+            return ($v1 + ($v2 - $v1) * ((2 / 3) - $vH) * 6);
207
+        }
208
+
209
+        return ($v1);
210
+    };
211
+
212
+    if ($S == 0) {
213
+        // HSV values = 0 -> 1
214
+        $R = $L * 255;
215
+        $G = $L * 255;
216
+        $B = $L * 255;
217
+    } else {
218
+        if ($L < 0.5) {
219
+            $var_2 = $L * (1 + $S);
220
+        } else {
221
+            $var_2 = ($L + $S) - ($S * $L);
222
+        }
223
+
224
+        $var_1 = 2 * $L - $var_2;
225
+
226
+        $R = 255 * $hue_2_rgb($var_1, $var_2, $H + (1 / 3));
227
+        $G = 255 * $hue_2_rgb($var_1, $var_2, $H);
228
+        $B = 255 * $hue_2_rgb($var_1, $var_2, $H - (1 / 3));
229
+    }
230
+
231
+    $ret = [];
232
+    $ret['r'] = floor($R);
233
+    $ret['g'] = floor($G);
234
+    $ret['b'] = floor($B);
235
+
236
+    return $ret;
237 237
 }
238 238
 
239 239
 /**
@@ -250,11 +250,11 @@  discard block
 block discarded – undo
250 250
  *     true si il faut supprimer le fichier temporaire ; false sinon.
251 251
  */
252 252
 function statut_effacer_images_temporaires($stat) {
253
-	static $statut = false; // par defaut on grave toute les images
254
-	if ($stat === 'get') {
255
-		return $statut;
256
-	}
257
-	$statut = $stat ? true : false;
253
+    static $statut = false; // par defaut on grave toute les images
254
+    if ($stat === 'get') {
255
+        return $statut;
256
+    }
257
+    $statut = $stat ? true : false;
258 258
 }
259 259
 
260 260
 
@@ -307,243 +307,243 @@  discard block
 block discarded – undo
307 307
  *     - array : tableau décrivant de l'image
308 308
  */
309 309
 function _image_valeurs_trans($img, $effet, $forcer_format = false, $fonction_creation = null, $find_in_path = false, $support_svg = false) {
310
-	$ret = [];
311
-	$f = null;
312
-	static $images_recalcul = [];
313
-	if (strlen($img) == 0) {
314
-		return false;
315
-	}
316
-
317
-	$source = trim(extraire_attribut($img, 'src') ?? '');
318
-	if (strlen($source) < 1) {
319
-		$source = $img;
320
-		$img = "<img src='$source' />";
321
-	} elseif (
322
-		preg_match('@^data:image/([^;]*);base64,(.*)$@isS', $source, $regs)
323
-		and $extension = _image_trouver_extension_depuis_mime('image/' . $regs[1])
324
-		and in_array($extension, _image_extensions_acceptees_en_entree())
325
-	) {
326
-		# gerer img src="data:....base64"
327
-		$local = sous_repertoire(_DIR_VAR, 'image-data') . md5($regs[2]) . '.' . _image_extension_normalisee($extension);
328
-		if (!file_exists($local)) {
329
-			ecrire_fichier($local, base64_decode($regs[2]));
330
-		}
331
-		if ($sanitizer = charger_fonction($extension, 'sanitizer', true)) {
332
-			$sanitizer($local);
333
-		}
334
-		$source = $local;
335
-		$img = inserer_attribut($img, 'src', $source);
336
-		# eviter les mauvaises surprises lors de conversions de format
337
-		$img = inserer_attribut($img, 'width', '');
338
-		$img = inserer_attribut($img, 'height', '');
339
-	}
340
-
341
-	// les protocoles web prennent au moins 3 lettres
342
-	if (tester_url_absolue($source)) {
343
-		include_spip('inc/distant');
344
-		$fichier = _DIR_RACINE . copie_locale($source);
345
-		if (!$fichier) {
346
-			return '';
347
-		}
348
-		if (
349
-			$extension = _image_trouver_extension($fichier)
350
-			and $sanitizer = charger_fonction($extension, 'sanitizer', true)
351
-		) {
352
-			$sanitizer($fichier);
353
-		}
354
-	} else {
355
-		// enlever le timestamp eventuel
356
-		if (strpos($source, '?') !== false) {
357
-			$source = preg_replace(',[?][0-9]+$,', '', $source);
358
-		}
359
-		if (
360
-			strpos($source, '?') !== false
361
-			and strncmp($source, _DIR_IMG, strlen(_DIR_IMG)) == 0
362
-			and file_exists($f = preg_replace(',[?].*$,', '', $source))
363
-		) {
364
-			$source = $f;
365
-		}
366
-		$fichier = $source;
367
-	}
368
-
369
-	$terminaison_dest = '';
370
-	if ($terminaison = _image_trouver_extension($fichier)) {
371
-		$terminaison_dest = ($terminaison == 'gif') ? 'png' : $terminaison;
372
-	}
373
-
374
-	if (
375
-		$forcer_format !== false
376
-		// ignorer forcer_format si on a une image svg, que le filtre appelant ne supporte pas SVG, et que le forcage est un autre format image
377
-		and ($terminaison_dest !== 'svg' or $support_svg or !in_array($forcer_format, _image_extensions_acceptees_en_sortie()))
378
-	) {
379
-		$terminaison_dest = $forcer_format;
380
-	}
381
-
382
-	if (!$terminaison_dest) {
383
-		return false;
384
-	}
385
-
386
-	$nom_fichier = substr($fichier, 0, strlen($fichier) - (strlen($terminaison) + 1));
387
-	$fichier_dest = $nom_fichier;
388
-	if (
389
-		($find_in_path and $f = find_in_path($fichier) and $fichier = $f)
390
-		or @file_exists($f = $fichier)
391
-	) {
392
-		// on passe la balise img a taille image qui exraira les attributs si possible
393
-		// au lieu de faire un acces disque sur le fichier
394
-		[$ret['hauteur'], $ret['largeur']] = taille_image($find_in_path ? $f : $img);
395
-		$date_src = @filemtime($f);
396
-	} elseif (
397
-		@file_exists($f = "$fichier.src")
398
-		and lire_fichier($f, $valeurs)
399
-		and $valeurs = unserialize($valeurs)
400
-		and isset($valeurs['hauteur_dest'])
401
-		and isset($valeurs['largeur_dest'])
402
-	) {
403
-		$ret['hauteur'] = $valeurs['hauteur_dest'];
404
-		$ret['largeur'] = $valeurs['largeur_dest'];
405
-		$date_src = $valeurs['date'];
406
-	} // pas de fichier source par la
407
-	else {
408
-		return false;
409
-	}
410
-
411
-	// pas de taille mesurable
412
-	if (!($ret['hauteur'] or $ret['largeur'])) {
413
-		return false;
414
-	}
415
-
416
-	// les images calculees dependent du chemin du fichier source
417
-	// pour une meme image source et un meme filtre on aboutira a 2 fichiers selon si l'appel est dans le public ou dans le prive
418
-	// ce n'est pas totalement optimal en terme de stockage, mais chaque image est associee a un fichier .src
419
-	// qui contient la methode de reconstrucion (le filtre + les arguments d'appel) et les arguments different entre prive et public
420
-	// la mise en commun du fichier image cree donc un bug et des problemes qui necessiteraient beaucoup de complexite de code
421
-	// alors que ca concerne peu de site au final
422
-	// la release de r23632+r23633+r23634 a provoque peu de remontee de bug attestant du peu de sites impactes
423
-	$identifiant = $fichier;
424
-
425
-	// cas general :
426
-	// on a un dossier cache commun et un nom de fichier qui varie avec l'effet
427
-	// cas particulier de reduire :
428
-	// un cache par dimension, et le nom de fichier est conserve, suffixe par la dimension aussi
429
-	$cache = 'cache-gd2';
430
-	if (substr($effet, 0, 7) == 'reduire') {
431
-		[, $maxWidth, $maxHeight] = explode('-', $effet);
432
-		[$destWidth, $destHeight] = _image_ratio($ret['largeur'], $ret['hauteur'], $maxWidth, $maxHeight);
433
-		$ret['largeur_dest'] = $destWidth;
434
-		$ret['hauteur_dest'] = $destHeight;
435
-		$effet = "L{$destWidth}xH$destHeight";
436
-		$cache = 'cache-vignettes';
437
-		$fichier_dest = basename($fichier_dest);
438
-		if (($ret['largeur'] <= $maxWidth) && ($ret['hauteur'] <= $maxHeight)) {
439
-			// on garde la terminaison initiale car image simplement copiee
440
-			// et on postfixe son nom avec un md5 du path
441
-			$terminaison_dest = $terminaison;
442
-			$fichier_dest .= '-' . substr(md5("$identifiant"), 0, 5);
443
-		} else {
444
-			$fichier_dest .= '-' . substr(md5("$identifiant-$effet"), 0, 5);
445
-		}
446
-		$cache = sous_repertoire(_DIR_VAR, $cache);
447
-		$cache = sous_repertoire($cache, $effet);
448
-	} else {
449
-		$fichier_dest = md5("$identifiant-$effet");
450
-		$cache = sous_repertoire(_DIR_VAR, $cache);
451
-		$cache = sous_repertoire($cache, substr($fichier_dest, 0, 2));
452
-		$fichier_dest = substr($fichier_dest, 2);
453
-	}
454
-
455
-	$fichier_dest = $cache . $fichier_dest . '.' . $terminaison_dest;
456
-
457
-	$GLOBALS['images_calculees'][] = $fichier_dest;
458
-
459
-	$creer = true;
460
-	// si recalcul des images demande, recalculer chaque image une fois
461
-	if (defined('_VAR_IMAGES') and _VAR_IMAGES and !isset($images_recalcul[$fichier_dest])) {
462
-		$images_recalcul[$fichier_dest] = true;
463
-	} else {
464
-		if (@file_exists($f = $fichier_dest)) {
465
-			if (filemtime($f) >= $date_src) {
466
-				$creer = false;
467
-			}
468
-		} else {
469
-			if (
470
-				@file_exists($f = "$fichier_dest.src")
471
-				and lire_fichier($f, $valeurs)
472
-				and $valeurs = unserialize($valeurs)
473
-				and $valeurs['date'] >= $date_src
474
-			) {
475
-				$creer = false;
476
-			}
477
-		}
478
-	}
479
-	if ($creer) {
480
-		if (!@file_exists($fichier)) {
481
-			if (!@file_exists("$fichier.src")) {
482
-				spip_log("Image absente : $fichier");
483
-
484
-				return false;
485
-			}
486
-			# on reconstruit l'image source absente a partir de la chaine des .src
487
-			reconstruire_image_intermediaire($fichier);
488
-		}
489
-	}
490
-
491
-	if ($creer) {
492
-		spip_log(
493
-			'filtre image ' . ($fonction_creation ? reset($fonction_creation) : '') . "[$effet] sur $fichier",
494
-			'images' . _LOG_DEBUG
495
-		);
496
-	}
497
-
498
-	$term_fonction = _image_trouver_extension_pertinente($fichier);
499
-	$ret['fonction_imagecreatefrom'] = '_imagecreatefrom' . $term_fonction;
500
-	$ret['fichier'] = $fichier;
501
-	$ret['fonction_image'] = '_image_image' . $terminaison_dest;
502
-	$ret['fichier_dest'] = $fichier_dest;
503
-	$ret['format_source'] = _image_extension_normalisee($terminaison);
504
-	$ret['format_dest'] = $terminaison_dest;
505
-	$ret['date_src'] = $date_src;
506
-	$ret['creer'] = $creer;
507
-	$ret['class'] = extraire_attribut($img, 'class');
508
-	$ret['alt'] = extraire_attribut($img, 'alt');
509
-	$ret['style'] = extraire_attribut($img, 'style');
510
-	$ret['tag'] = $img;
511
-	if ($fonction_creation) {
512
-		$ret['reconstruction'] = $fonction_creation;
513
-		# ecrire ici comment creer le fichier, car il est pas sur qu'on l'ecrira reelement
514
-		# cas de image_reduire qui finalement ne reduit pas l'image source
515
-		# ca evite d'essayer de le creer au prochain hit si il n'est pas la
516
-		#ecrire_fichier($ret['fichier_dest'].'.src',serialize($ret),true);
517
-	}
518
-
519
-	$ret = pipeline('image_preparer_filtre', [
520
-			'args' => [
521
-				'img' => $img,
522
-				'effet' => $effet,
523
-				'forcer_format' => $forcer_format,
524
-				'fonction_creation' => $fonction_creation,
525
-				'find_in_path' => $find_in_path,
526
-			],
527
-			'data' => $ret
528
-		]);
529
-
530
-	// une globale pour le debug en cas de crash memoire
531
-	$GLOBALS['derniere_image_calculee'] = $ret;
532
-
533
-	// traiter le cas particulier des SVG : si le filtre n'a pas annonce explicitement qu'il savait faire, on delegue
534
-	if ($term_fonction === 'svg') {
535
-		if ($creer and !$support_svg) {
536
-			process_image_svg_identite($ret);
537
-			$ret['creer'] = false;
538
-		}
539
-	}
540
-	else {
541
-		if (!function_exists($ret['fonction_imagecreatefrom'])) {
542
-			return false;
543
-		}
544
-	}
545
-
546
-	return $ret;
310
+    $ret = [];
311
+    $f = null;
312
+    static $images_recalcul = [];
313
+    if (strlen($img) == 0) {
314
+        return false;
315
+    }
316
+
317
+    $source = trim(extraire_attribut($img, 'src') ?? '');
318
+    if (strlen($source) < 1) {
319
+        $source = $img;
320
+        $img = "<img src='$source' />";
321
+    } elseif (
322
+        preg_match('@^data:image/([^;]*);base64,(.*)$@isS', $source, $regs)
323
+        and $extension = _image_trouver_extension_depuis_mime('image/' . $regs[1])
324
+        and in_array($extension, _image_extensions_acceptees_en_entree())
325
+    ) {
326
+        # gerer img src="data:....base64"
327
+        $local = sous_repertoire(_DIR_VAR, 'image-data') . md5($regs[2]) . '.' . _image_extension_normalisee($extension);
328
+        if (!file_exists($local)) {
329
+            ecrire_fichier($local, base64_decode($regs[2]));
330
+        }
331
+        if ($sanitizer = charger_fonction($extension, 'sanitizer', true)) {
332
+            $sanitizer($local);
333
+        }
334
+        $source = $local;
335
+        $img = inserer_attribut($img, 'src', $source);
336
+        # eviter les mauvaises surprises lors de conversions de format
337
+        $img = inserer_attribut($img, 'width', '');
338
+        $img = inserer_attribut($img, 'height', '');
339
+    }
340
+
341
+    // les protocoles web prennent au moins 3 lettres
342
+    if (tester_url_absolue($source)) {
343
+        include_spip('inc/distant');
344
+        $fichier = _DIR_RACINE . copie_locale($source);
345
+        if (!$fichier) {
346
+            return '';
347
+        }
348
+        if (
349
+            $extension = _image_trouver_extension($fichier)
350
+            and $sanitizer = charger_fonction($extension, 'sanitizer', true)
351
+        ) {
352
+            $sanitizer($fichier);
353
+        }
354
+    } else {
355
+        // enlever le timestamp eventuel
356
+        if (strpos($source, '?') !== false) {
357
+            $source = preg_replace(',[?][0-9]+$,', '', $source);
358
+        }
359
+        if (
360
+            strpos($source, '?') !== false
361
+            and strncmp($source, _DIR_IMG, strlen(_DIR_IMG)) == 0
362
+            and file_exists($f = preg_replace(',[?].*$,', '', $source))
363
+        ) {
364
+            $source = $f;
365
+        }
366
+        $fichier = $source;
367
+    }
368
+
369
+    $terminaison_dest = '';
370
+    if ($terminaison = _image_trouver_extension($fichier)) {
371
+        $terminaison_dest = ($terminaison == 'gif') ? 'png' : $terminaison;
372
+    }
373
+
374
+    if (
375
+        $forcer_format !== false
376
+        // ignorer forcer_format si on a une image svg, que le filtre appelant ne supporte pas SVG, et que le forcage est un autre format image
377
+        and ($terminaison_dest !== 'svg' or $support_svg or !in_array($forcer_format, _image_extensions_acceptees_en_sortie()))
378
+    ) {
379
+        $terminaison_dest = $forcer_format;
380
+    }
381
+
382
+    if (!$terminaison_dest) {
383
+        return false;
384
+    }
385
+
386
+    $nom_fichier = substr($fichier, 0, strlen($fichier) - (strlen($terminaison) + 1));
387
+    $fichier_dest = $nom_fichier;
388
+    if (
389
+        ($find_in_path and $f = find_in_path($fichier) and $fichier = $f)
390
+        or @file_exists($f = $fichier)
391
+    ) {
392
+        // on passe la balise img a taille image qui exraira les attributs si possible
393
+        // au lieu de faire un acces disque sur le fichier
394
+        [$ret['hauteur'], $ret['largeur']] = taille_image($find_in_path ? $f : $img);
395
+        $date_src = @filemtime($f);
396
+    } elseif (
397
+        @file_exists($f = "$fichier.src")
398
+        and lire_fichier($f, $valeurs)
399
+        and $valeurs = unserialize($valeurs)
400
+        and isset($valeurs['hauteur_dest'])
401
+        and isset($valeurs['largeur_dest'])
402
+    ) {
403
+        $ret['hauteur'] = $valeurs['hauteur_dest'];
404
+        $ret['largeur'] = $valeurs['largeur_dest'];
405
+        $date_src = $valeurs['date'];
406
+    } // pas de fichier source par la
407
+    else {
408
+        return false;
409
+    }
410
+
411
+    // pas de taille mesurable
412
+    if (!($ret['hauteur'] or $ret['largeur'])) {
413
+        return false;
414
+    }
415
+
416
+    // les images calculees dependent du chemin du fichier source
417
+    // pour une meme image source et un meme filtre on aboutira a 2 fichiers selon si l'appel est dans le public ou dans le prive
418
+    // ce n'est pas totalement optimal en terme de stockage, mais chaque image est associee a un fichier .src
419
+    // qui contient la methode de reconstrucion (le filtre + les arguments d'appel) et les arguments different entre prive et public
420
+    // la mise en commun du fichier image cree donc un bug et des problemes qui necessiteraient beaucoup de complexite de code
421
+    // alors que ca concerne peu de site au final
422
+    // la release de r23632+r23633+r23634 a provoque peu de remontee de bug attestant du peu de sites impactes
423
+    $identifiant = $fichier;
424
+
425
+    // cas general :
426
+    // on a un dossier cache commun et un nom de fichier qui varie avec l'effet
427
+    // cas particulier de reduire :
428
+    // un cache par dimension, et le nom de fichier est conserve, suffixe par la dimension aussi
429
+    $cache = 'cache-gd2';
430
+    if (substr($effet, 0, 7) == 'reduire') {
431
+        [, $maxWidth, $maxHeight] = explode('-', $effet);
432
+        [$destWidth, $destHeight] = _image_ratio($ret['largeur'], $ret['hauteur'], $maxWidth, $maxHeight);
433
+        $ret['largeur_dest'] = $destWidth;
434
+        $ret['hauteur_dest'] = $destHeight;
435
+        $effet = "L{$destWidth}xH$destHeight";
436
+        $cache = 'cache-vignettes';
437
+        $fichier_dest = basename($fichier_dest);
438
+        if (($ret['largeur'] <= $maxWidth) && ($ret['hauteur'] <= $maxHeight)) {
439
+            // on garde la terminaison initiale car image simplement copiee
440
+            // et on postfixe son nom avec un md5 du path
441
+            $terminaison_dest = $terminaison;
442
+            $fichier_dest .= '-' . substr(md5("$identifiant"), 0, 5);
443
+        } else {
444
+            $fichier_dest .= '-' . substr(md5("$identifiant-$effet"), 0, 5);
445
+        }
446
+        $cache = sous_repertoire(_DIR_VAR, $cache);
447
+        $cache = sous_repertoire($cache, $effet);
448
+    } else {
449
+        $fichier_dest = md5("$identifiant-$effet");
450
+        $cache = sous_repertoire(_DIR_VAR, $cache);
451
+        $cache = sous_repertoire($cache, substr($fichier_dest, 0, 2));
452
+        $fichier_dest = substr($fichier_dest, 2);
453
+    }
454
+
455
+    $fichier_dest = $cache . $fichier_dest . '.' . $terminaison_dest;
456
+
457
+    $GLOBALS['images_calculees'][] = $fichier_dest;
458
+
459
+    $creer = true;
460
+    // si recalcul des images demande, recalculer chaque image une fois
461
+    if (defined('_VAR_IMAGES') and _VAR_IMAGES and !isset($images_recalcul[$fichier_dest])) {
462
+        $images_recalcul[$fichier_dest] = true;
463
+    } else {
464
+        if (@file_exists($f = $fichier_dest)) {
465
+            if (filemtime($f) >= $date_src) {
466
+                $creer = false;
467
+            }
468
+        } else {
469
+            if (
470
+                @file_exists($f = "$fichier_dest.src")
471
+                and lire_fichier($f, $valeurs)
472
+                and $valeurs = unserialize($valeurs)
473
+                and $valeurs['date'] >= $date_src
474
+            ) {
475
+                $creer = false;
476
+            }
477
+        }
478
+    }
479
+    if ($creer) {
480
+        if (!@file_exists($fichier)) {
481
+            if (!@file_exists("$fichier.src")) {
482
+                spip_log("Image absente : $fichier");
483
+
484
+                return false;
485
+            }
486
+            # on reconstruit l'image source absente a partir de la chaine des .src
487
+            reconstruire_image_intermediaire($fichier);
488
+        }
489
+    }
490
+
491
+    if ($creer) {
492
+        spip_log(
493
+            'filtre image ' . ($fonction_creation ? reset($fonction_creation) : '') . "[$effet] sur $fichier",
494
+            'images' . _LOG_DEBUG
495
+        );
496
+    }
497
+
498
+    $term_fonction = _image_trouver_extension_pertinente($fichier);
499
+    $ret['fonction_imagecreatefrom'] = '_imagecreatefrom' . $term_fonction;
500
+    $ret['fichier'] = $fichier;
501
+    $ret['fonction_image'] = '_image_image' . $terminaison_dest;
502
+    $ret['fichier_dest'] = $fichier_dest;
503
+    $ret['format_source'] = _image_extension_normalisee($terminaison);
504
+    $ret['format_dest'] = $terminaison_dest;
505
+    $ret['date_src'] = $date_src;
506
+    $ret['creer'] = $creer;
507
+    $ret['class'] = extraire_attribut($img, 'class');
508
+    $ret['alt'] = extraire_attribut($img, 'alt');
509
+    $ret['style'] = extraire_attribut($img, 'style');
510
+    $ret['tag'] = $img;
511
+    if ($fonction_creation) {
512
+        $ret['reconstruction'] = $fonction_creation;
513
+        # ecrire ici comment creer le fichier, car il est pas sur qu'on l'ecrira reelement
514
+        # cas de image_reduire qui finalement ne reduit pas l'image source
515
+        # ca evite d'essayer de le creer au prochain hit si il n'est pas la
516
+        #ecrire_fichier($ret['fichier_dest'].'.src',serialize($ret),true);
517
+    }
518
+
519
+    $ret = pipeline('image_preparer_filtre', [
520
+            'args' => [
521
+                'img' => $img,
522
+                'effet' => $effet,
523
+                'forcer_format' => $forcer_format,
524
+                'fonction_creation' => $fonction_creation,
525
+                'find_in_path' => $find_in_path,
526
+            ],
527
+            'data' => $ret
528
+        ]);
529
+
530
+    // une globale pour le debug en cas de crash memoire
531
+    $GLOBALS['derniere_image_calculee'] = $ret;
532
+
533
+    // traiter le cas particulier des SVG : si le filtre n'a pas annonce explicitement qu'il savait faire, on delegue
534
+    if ($term_fonction === 'svg') {
535
+        if ($creer and !$support_svg) {
536
+            process_image_svg_identite($ret);
537
+            $ret['creer'] = false;
538
+        }
539
+    }
540
+    else {
541
+        if (!function_exists($ret['fonction_imagecreatefrom'])) {
542
+            return false;
543
+        }
544
+    }
545
+
546
+    return $ret;
547 547
 }
548 548
 
549 549
 
@@ -552,53 +552,53 @@  discard block
 block discarded – undo
552 552
  * @return array
553 553
  */
554 554
 function _image_extensions_acceptees_en_entree() {
555
-	static $extensions = null;
556
-	if (empty($extensions)) {
557
-		$extensions = ['png', 'gif', 'jpg', 'jpeg'];
558
-		if (!empty($GLOBALS['meta']['gd_formats'])) {
559
-			// action=tester renseigne gd_formats et detecte le support de webp
560
-			$extensions = array_merge(explode(',', $GLOBALS['meta']['gd_formats']));
561
-			$extensions = array_map('trim', $extensions);
562
-			$extensions = array_filter($extensions);
563
-			$extensions = array_unique($extensions);
564
-			if (in_array('jpg', $extensions)) { $extensions[] = 'jpeg';
565
-			}
566
-		}
567
-		$extensions[] = 'svg'; // on le supporte toujours avec des fonctions specifiques
568
-	}
569
-
570
-	return $extensions;
555
+    static $extensions = null;
556
+    if (empty($extensions)) {
557
+        $extensions = ['png', 'gif', 'jpg', 'jpeg'];
558
+        if (!empty($GLOBALS['meta']['gd_formats'])) {
559
+            // action=tester renseigne gd_formats et detecte le support de webp
560
+            $extensions = array_merge(explode(',', $GLOBALS['meta']['gd_formats']));
561
+            $extensions = array_map('trim', $extensions);
562
+            $extensions = array_filter($extensions);
563
+            $extensions = array_unique($extensions);
564
+            if (in_array('jpg', $extensions)) { $extensions[] = 'jpeg';
565
+            }
566
+        }
567
+        $extensions[] = 'svg'; // on le supporte toujours avec des fonctions specifiques
568
+    }
569
+
570
+    return $extensions;
571 571
 }
572 572
 
573 573
 /**
574 574
  * @return array|string[]|null
575 575
  */
576 576
 function _image_extensions_acceptees_en_sortie() {
577
-	static $extensions = null;
578
-	if (empty($extensions)) {
579
-		$extensions = _image_extensions_acceptees_en_entree();
580
-		$extensions = array_diff($extensions, ['jpeg']);
581
-		if (in_array('gif', $extensions) and !function_exists('imagegif')) {
582
-			$extensions = array_diff($extensions, ['gif']);
583
-		}
584
-		if (in_array('webp', $extensions) and !function_exists('imagewebp')) {
585
-			$extensions = array_diff($extensions, ['webp']);
586
-		}
587
-	}
588
-
589
-	return $extensions;
577
+    static $extensions = null;
578
+    if (empty($extensions)) {
579
+        $extensions = _image_extensions_acceptees_en_entree();
580
+        $extensions = array_diff($extensions, ['jpeg']);
581
+        if (in_array('gif', $extensions) and !function_exists('imagegif')) {
582
+            $extensions = array_diff($extensions, ['gif']);
583
+        }
584
+        if (in_array('webp', $extensions) and !function_exists('imagewebp')) {
585
+            $extensions = array_diff($extensions, ['webp']);
586
+        }
587
+    }
588
+
589
+    return $extensions;
590 590
 }
591 591
 
592 592
 function _image_extension_normalisee($extension) {
593
-	$extension = strtolower($extension);
594
-	if ($extension === 'jpeg') {
595
-		$extension = 'jpg';
596
-	}
597
-	return $extension;
593
+    $extension = strtolower($extension);
594
+    if ($extension === 'jpeg') {
595
+        $extension = 'jpg';
596
+    }
597
+    return $extension;
598 598
 }
599 599
 
600 600
 function _image_extensions_conservent_transparence() {
601
-	return ['png', 'webp'];
601
+    return ['png', 'webp'];
602 602
 }
603 603
 
604 604
 
@@ -608,12 +608,12 @@  discard block
 block discarded – undo
608 608
  * @return string
609 609
  */
610 610
 function _image_trouver_extension($path) {
611
-	$preg_extensions = implode('|', _image_extensions_acceptees_en_entree());
612
-	if (preg_match(",\.($preg_extensions)($|[?]),i", $path, $regs)) {
613
-		$terminaison = strtolower($regs[1]);
614
-		return $terminaison;
615
-	}
616
-	return '';
611
+    $preg_extensions = implode('|', _image_extensions_acceptees_en_entree());
612
+    if (preg_match(",\.($preg_extensions)($|[?]),i", $path, $regs)) {
613
+        $terminaison = strtolower($regs[1]);
614
+        return $terminaison;
615
+    }
616
+    return '';
617 617
 }
618 618
 
619 619
 /**
@@ -624,33 +624,33 @@  discard block
 block discarded – undo
624 624
  * @return string Extension, dans le format attendu par les fonctions 'gd' ('jpeg' pour les .jpg par exemple)
625 625
  */
626 626
 function _image_trouver_extension_pertinente($path) {
627
-	$path = supprimer_timestamp($path);
628
-	$terminaison = _image_trouver_extension($path);
629
-	if ($terminaison == 'jpg') {
630
-		$terminaison = 'jpeg';
631
-	}
632
-
633
-	if (!file_exists($path)) {
634
-		return $terminaison;
635
-	}
636
-
637
-	if (!$info = @spip_getimagesize($path)) {
638
-		return $terminaison;
639
-	}
640
-
641
-	if (isset($info['mime'])) {
642
-		$mime = $info['mime'];
643
-	}
644
-	else {
645
-		$mime = image_type_to_mime_type($info[2]);
646
-	}
647
-
648
-	$_terminaison = _image_trouver_extension_depuis_mime($mime);
649
-	if ($_terminaison and $_terminaison !== $terminaison) {
650
-		spip_log("Mauvaise extension du fichier : $path . Son type mime est : $mime", 'images.' . _LOG_INFO_IMPORTANTE);
651
-		$terminaison = $_terminaison;
652
-	}
653
-	return $terminaison;
627
+    $path = supprimer_timestamp($path);
628
+    $terminaison = _image_trouver_extension($path);
629
+    if ($terminaison == 'jpg') {
630
+        $terminaison = 'jpeg';
631
+    }
632
+
633
+    if (!file_exists($path)) {
634
+        return $terminaison;
635
+    }
636
+
637
+    if (!$info = @spip_getimagesize($path)) {
638
+        return $terminaison;
639
+    }
640
+
641
+    if (isset($info['mime'])) {
642
+        $mime = $info['mime'];
643
+    }
644
+    else {
645
+        $mime = image_type_to_mime_type($info[2]);
646
+    }
647
+
648
+    $_terminaison = _image_trouver_extension_depuis_mime($mime);
649
+    if ($_terminaison and $_terminaison !== $terminaison) {
650
+        spip_log("Mauvaise extension du fichier : $path . Son type mime est : $mime", 'images.' . _LOG_INFO_IMPORTANTE);
651
+        $terminaison = $_terminaison;
652
+    }
653
+    return $terminaison;
654 654
 }
655 655
 
656 656
 /**
@@ -658,36 +658,36 @@  discard block
 block discarded – undo
658 658
  * @return string
659 659
  */
660 660
 function _image_trouver_extension_depuis_mime($mime) {
661
-	switch (strtolower($mime)) {
662
-		case 'image/png':
663
-		case 'image/x-png':
664
-			$terminaison = 'png';
665
-			break;
666
-
667
-		case 'image/jpg':
668
-		case 'image/jpeg':
669
-		case 'image/pjpeg':
670
-			$terminaison = 'jpeg';
671
-			break;
672
-
673
-		case 'image/gif':
674
-			$terminaison = 'gif';
675
-			break;
676
-
677
-		case 'image/webp':
678
-		case 'image/x-webp':
679
-			$terminaison = 'webp';
680
-			break;
681
-
682
-		case 'image/svg+xml':
683
-			$terminaison = 'svg';
684
-			break;
685
-
686
-		default:
687
-			$terminaison = '';
688
-	}
689
-
690
-	return $terminaison;
661
+    switch (strtolower($mime)) {
662
+        case 'image/png':
663
+        case 'image/x-png':
664
+            $terminaison = 'png';
665
+            break;
666
+
667
+        case 'image/jpg':
668
+        case 'image/jpeg':
669
+        case 'image/pjpeg':
670
+            $terminaison = 'jpeg';
671
+            break;
672
+
673
+        case 'image/gif':
674
+            $terminaison = 'gif';
675
+            break;
676
+
677
+        case 'image/webp':
678
+        case 'image/x-webp':
679
+            $terminaison = 'webp';
680
+            break;
681
+
682
+        case 'image/svg+xml':
683
+            $terminaison = 'svg';
684
+            break;
685
+
686
+        default:
687
+            $terminaison = '';
688
+    }
689
+
690
+    return $terminaison;
691 691
 }
692 692
 
693 693
 
@@ -707,18 +707,18 @@  discard block
 block discarded – undo
707 707
  *     Une ressource de type Image GD.
708 708
  */
709 709
 function _imagecreatefrom_func(string $func, string $filename) {
710
-	if (!function_exists($func)) {
711
-		spip_log("GD indisponible : $func inexistante. Traitement $filename impossible.", _LOG_CRITIQUE);
712
-		erreur_squelette("GD indisponible : $func inexistante. Traitement $filename impossible.");
713
-		return null;
714
-	}
715
-	$img = @$func($filename);
716
-	if (!$img) {
717
-		spip_log("Erreur lecture imagecreatefromjpeg $filename", _LOG_CRITIQUE);
718
-		erreur_squelette("Erreur lecture imagecreatefromjpeg $filename");
719
-		$img = imagecreate(10, 10);
720
-	}
721
-	return $img;
710
+    if (!function_exists($func)) {
711
+        spip_log("GD indisponible : $func inexistante. Traitement $filename impossible.", _LOG_CRITIQUE);
712
+        erreur_squelette("GD indisponible : $func inexistante. Traitement $filename impossible.");
713
+        return null;
714
+    }
715
+    $img = @$func($filename);
716
+    if (!$img) {
717
+        spip_log("Erreur lecture imagecreatefromjpeg $filename", _LOG_CRITIQUE);
718
+        erreur_squelette("Erreur lecture imagecreatefromjpeg $filename");
719
+        $img = imagecreate(10, 10);
720
+    }
721
+    return $img;
722 722
 }
723 723
 
724 724
 /**
@@ -734,7 +734,7 @@  discard block
 block discarded – undo
734 734
  *     Une ressource de type Image GD.
735 735
  */
736 736
 function _imagecreatefromjpeg($filename) {
737
-	return _imagecreatefrom_func('imagecreatefromjpeg', $filename);
737
+    return _imagecreatefrom_func('imagecreatefromjpeg', $filename);
738 738
 }
739 739
 
740 740
 /**
@@ -750,7 +750,7 @@  discard block
 block discarded – undo
750 750
  *     Une ressource de type Image GD.
751 751
  */
752 752
 function _imagecreatefrompng($filename) {
753
-	return _imagecreatefrom_func('imagecreatefrompng', $filename);
753
+    return _imagecreatefrom_func('imagecreatefrompng', $filename);
754 754
 }
755 755
 
756 756
 /**
@@ -766,7 +766,7 @@  discard block
 block discarded – undo
766 766
  *     Une ressource de type Image GD.
767 767
  */
768 768
 function _imagecreatefromgif($filename) {
769
-	return _imagecreatefrom_func('imagecreatefromgif', $filename);
769
+    return _imagecreatefrom_func('imagecreatefromgif', $filename);
770 770
 }
771 771
 
772 772
 
@@ -783,7 +783,7 @@  discard block
 block discarded – undo
783 783
  *     Une ressource de type Image GD.
784 784
  */
785 785
 function _imagecreatefromwebp($filename) {
786
-	return _imagecreatefrom_func('imagecreatefromwebp', $filename);
786
+    return _imagecreatefrom_func('imagecreatefromwebp', $filename);
787 787
 }
788 788
 
789 789
 /**
@@ -801,24 +801,24 @@  discard block
 block discarded – undo
801 801
  *     - true si une image est bien retournée.
802 802
  */
803 803
 function _image_imagepng($img, $fichier) {
804
-	if (!function_exists('imagepng')) {
805
-		return false;
806
-	}
807
-	$tmp = $fichier . '.tmp';
808
-	$ret = imagepng($img, $tmp);
809
-	if (file_exists($tmp)) {
810
-		$taille_test = getimagesize($tmp);
811
-		if ($taille_test[0] < 1) {
812
-			return false;
813
-		}
814
-
815
-		spip_unlink($fichier); // le fichier peut deja exister
816
-		@rename($tmp, $fichier);
817
-
818
-		return $ret;
819
-	}
820
-
821
-	return false;
804
+    if (!function_exists('imagepng')) {
805
+        return false;
806
+    }
807
+    $tmp = $fichier . '.tmp';
808
+    $ret = imagepng($img, $tmp);
809
+    if (file_exists($tmp)) {
810
+        $taille_test = getimagesize($tmp);
811
+        if ($taille_test[0] < 1) {
812
+            return false;
813
+        }
814
+
815
+        spip_unlink($fichier); // le fichier peut deja exister
816
+        @rename($tmp, $fichier);
817
+
818
+        return $ret;
819
+    }
820
+
821
+    return false;
822 822
 }
823 823
 
824 824
 /**
@@ -836,24 +836,24 @@  discard block
 block discarded – undo
836 836
  *     - true si une image est bien retournée.
837 837
  */
838 838
 function _image_imagegif($img, $fichier) {
839
-	if (!function_exists('imagegif')) {
840
-		return false;
841
-	}
842
-	$tmp = $fichier . '.tmp';
843
-	$ret = imagegif($img, $tmp);
844
-	if (file_exists($tmp)) {
845
-		$taille_test = getimagesize($tmp);
846
-		if ($taille_test[0] < 1) {
847
-			return false;
848
-		}
849
-
850
-		spip_unlink($fichier); // le fichier peut deja exister
851
-		@rename($tmp, $fichier);
852
-
853
-		return $ret;
854
-	}
855
-
856
-	return false;
839
+    if (!function_exists('imagegif')) {
840
+        return false;
841
+    }
842
+    $tmp = $fichier . '.tmp';
843
+    $ret = imagegif($img, $tmp);
844
+    if (file_exists($tmp)) {
845
+        $taille_test = getimagesize($tmp);
846
+        if ($taille_test[0] < 1) {
847
+            return false;
848
+        }
849
+
850
+        spip_unlink($fichier); // le fichier peut deja exister
851
+        @rename($tmp, $fichier);
852
+
853
+        return $ret;
854
+    }
855
+
856
+    return false;
857 857
 }
858 858
 
859 859
 /**
@@ -876,29 +876,29 @@  discard block
 block discarded – undo
876 876
  *     - true si une image est bien retournée.
877 877
  */
878 878
 function _image_imagejpg($img, $fichier, $qualite = _IMG_GD_QUALITE) {
879
-	if (!function_exists('imagejpeg')) {
880
-		return false;
881
-	}
882
-	$tmp = $fichier . '.tmp';
879
+    if (!function_exists('imagejpeg')) {
880
+        return false;
881
+    }
882
+    $tmp = $fichier . '.tmp';
883 883
 
884
-	// Enable interlancing
885
-	imageinterlace($img, true);
884
+    // Enable interlancing
885
+    imageinterlace($img, true);
886 886
 
887
-	$ret = imagejpeg($img, $tmp, $qualite);
887
+    $ret = imagejpeg($img, $tmp, $qualite);
888 888
 
889
-	if (file_exists($tmp)) {
890
-		$taille_test = getimagesize($tmp);
891
-		if ($taille_test[0] < 1) {
892
-			return false;
893
-		}
889
+    if (file_exists($tmp)) {
890
+        $taille_test = getimagesize($tmp);
891
+        if ($taille_test[0] < 1) {
892
+            return false;
893
+        }
894 894
 
895
-		spip_unlink($fichier); // le fichier peut deja exister
896
-		@rename($tmp, $fichier);
895
+        spip_unlink($fichier); // le fichier peut deja exister
896
+        @rename($tmp, $fichier);
897 897
 
898
-		return $ret;
899
-	}
898
+        return $ret;
899
+    }
900 900
 
901
-	return false;
901
+    return false;
902 902
 }
903 903
 
904 904
 /**
@@ -916,9 +916,9 @@  discard block
 block discarded – undo
916 916
  *     true si le fichier a bien été créé ; false sinon.
917 917
  */
918 918
 function _image_imageico($img, $fichier) {
919
-	$gd_image_array = [$img];
919
+    $gd_image_array = [$img];
920 920
 
921
-	return ecrire_fichier($fichier, phpthumb_functions::GD2ICOstring($gd_image_array));
921
+    return ecrire_fichier($fichier, phpthumb_functions::GD2ICOstring($gd_image_array));
922 922
 }
923 923
 
924 924
 
@@ -937,24 +937,24 @@  discard block
 block discarded – undo
937 937
  *     - true si une image est bien retournée.
938 938
  */
939 939
 function _image_imagewebp($img, $fichier, $qualite = _IMG_GD_QUALITE) {
940
-	if (!function_exists('imagewebp')) {
941
-		return false;
942
-	}
943
-	$tmp = $fichier . '.tmp';
944
-	$ret = imagewebp($img, $tmp, $qualite);
945
-	if (file_exists($tmp)) {
946
-		$taille_test = getimagesize($tmp);
947
-		if ($taille_test[0] < 1) {
948
-			return false;
949
-		}
950
-
951
-		spip_unlink($fichier); // le fichier peut deja exister
952
-		@rename($tmp, $fichier);
953
-
954
-		return $ret;
955
-	}
956
-
957
-	return false;
940
+    if (!function_exists('imagewebp')) {
941
+        return false;
942
+    }
943
+    $tmp = $fichier . '.tmp';
944
+    $ret = imagewebp($img, $tmp, $qualite);
945
+    if (file_exists($tmp)) {
946
+        $taille_test = getimagesize($tmp);
947
+        if ($taille_test[0] < 1) {
948
+            return false;
949
+        }
950
+
951
+        spip_unlink($fichier); // le fichier peut deja exister
952
+        @rename($tmp, $fichier);
953
+
954
+        return $ret;
955
+    }
956
+
957
+    return false;
958 958
 }
959 959
 
960 960
 /**
@@ -974,35 +974,35 @@  discard block
 block discarded – undo
974 974
  */
975 975
 function _image_imagesvg($img, $fichier) {
976 976
 
977
-	$tmp = $fichier . '.tmp';
978
-	if (strpos($img, '<') === false) {
979
-		$img = supprimer_timestamp($img);
980
-		if (!file_exists($img)) {
981
-			return false;
982
-		}
983
-		@copy($img, $tmp);
984
-		if (filesize($tmp) == filesize($img)) {
985
-			spip_unlink($fichier); // le fichier peut deja exister
986
-			@rename($tmp, $fichier);
987
-			return true;
988
-		}
989
-		return false;
990
-	}
991
-
992
-	file_put_contents($tmp, $img);
993
-	if (file_exists($tmp)) {
994
-		$taille_test = spip_getimagesize($tmp);
995
-		if ($taille_test[0] < 1) {
996
-			return false;
997
-		}
998
-
999
-		spip_unlink($fichier); // le fichier peut deja exister
1000
-		@rename($tmp, $fichier);
1001
-
1002
-		return true;
1003
-	}
1004
-
1005
-	return false;
977
+    $tmp = $fichier . '.tmp';
978
+    if (strpos($img, '<') === false) {
979
+        $img = supprimer_timestamp($img);
980
+        if (!file_exists($img)) {
981
+            return false;
982
+        }
983
+        @copy($img, $tmp);
984
+        if (filesize($tmp) == filesize($img)) {
985
+            spip_unlink($fichier); // le fichier peut deja exister
986
+            @rename($tmp, $fichier);
987
+            return true;
988
+        }
989
+        return false;
990
+    }
991
+
992
+    file_put_contents($tmp, $img);
993
+    if (file_exists($tmp)) {
994
+        $taille_test = spip_getimagesize($tmp);
995
+        if ($taille_test[0] < 1) {
996
+            return false;
997
+        }
998
+
999
+        spip_unlink($fichier); // le fichier peut deja exister
1000
+        @rename($tmp, $fichier);
1001
+
1002
+        return true;
1003
+    }
1004
+
1005
+    return false;
1006 1006
 }
1007 1007
 
1008 1008
 
@@ -1030,29 +1030,29 @@  discard block
 block discarded – undo
1030 1030
  *     - false sinon.
1031 1031
  */
1032 1032
 function _image_gd_output($img, $valeurs, $qualite = _IMG_GD_QUALITE, $fonction = null) {
1033
-	if (is_null($fonction)) {
1034
-		$fonction = '_image_image' . $valeurs['format_dest'];
1035
-	}
1036
-	$ret = false;
1037
-	#un flag pour reperer les images gravees
1038
-	$lock =
1039
-		!statut_effacer_images_temporaires('get') // si la fonction n'a pas ete activee, on grave tout
1040
-	or (@file_exists($valeurs['fichier_dest']) and !@file_exists($valeurs['fichier_dest'] . '.src'));
1041
-	if (
1042
-		function_exists($fonction)
1043
-		&& ($ret = $fonction($img, $valeurs['fichier_dest'], $qualite)) # on a reussi a creer l'image
1044
-		&& isset($valeurs['reconstruction']) # et on sait comment la resonctruire le cas echeant
1045
-		&& !$lock
1046
-	) {
1047
-		if (@file_exists($valeurs['fichier_dest'])) {
1048
-			// dans tous les cas mettre a jour la taille de l'image finale
1049
-			[$valeurs['hauteur_dest'], $valeurs['largeur_dest']] = taille_image($valeurs['fichier_dest']);
1050
-			$valeurs['date'] = @filemtime($valeurs['fichier_dest']); // pour la retrouver apres disparition
1051
-			ecrire_fichier($valeurs['fichier_dest'] . '.src', serialize($valeurs), true);
1052
-		}
1053
-	}
1054
-
1055
-	return $ret;
1033
+    if (is_null($fonction)) {
1034
+        $fonction = '_image_image' . $valeurs['format_dest'];
1035
+    }
1036
+    $ret = false;
1037
+    #un flag pour reperer les images gravees
1038
+    $lock =
1039
+        !statut_effacer_images_temporaires('get') // si la fonction n'a pas ete activee, on grave tout
1040
+    or (@file_exists($valeurs['fichier_dest']) and !@file_exists($valeurs['fichier_dest'] . '.src'));
1041
+    if (
1042
+        function_exists($fonction)
1043
+        && ($ret = $fonction($img, $valeurs['fichier_dest'], $qualite)) # on a reussi a creer l'image
1044
+        && isset($valeurs['reconstruction']) # et on sait comment la resonctruire le cas echeant
1045
+        && !$lock
1046
+    ) {
1047
+        if (@file_exists($valeurs['fichier_dest'])) {
1048
+            // dans tous les cas mettre a jour la taille de l'image finale
1049
+            [$valeurs['hauteur_dest'], $valeurs['largeur_dest']] = taille_image($valeurs['fichier_dest']);
1050
+            $valeurs['date'] = @filemtime($valeurs['fichier_dest']); // pour la retrouver apres disparition
1051
+            ecrire_fichier($valeurs['fichier_dest'] . '.src', serialize($valeurs), true);
1052
+        }
1053
+    }
1054
+
1055
+    return $ret;
1056 1056
 }
1057 1057
 
1058 1058
 /**
@@ -1065,27 +1065,27 @@  discard block
 block discarded – undo
1065 1065
  *     Chemin vers le fichier manquant
1066 1066
  **/
1067 1067
 function reconstruire_image_intermediaire($fichier_manquant) {
1068
-	$reconstruire = [];
1069
-	$fichier = $fichier_manquant;
1070
-	while (
1071
-		strpos($fichier, '://') === false
1072
-		and !@file_exists($fichier)
1073
-		and lire_fichier($src = "$fichier.src", $source)
1074
-		and $valeurs = unserialize($source)
1075
-		and ($fichier = $valeurs['fichier']) # l'origine est connue (on ne verifie pas son existence, qu'importe ...)
1076
-	) {
1077
-		spip_unlink($src); // si jamais on a un timeout pendant la reconstruction, elle se fera naturellement au hit suivant
1078
-		$reconstruire[] = $valeurs['reconstruction'];
1079
-	}
1080
-	while (count($reconstruire)) {
1081
-		$r = array_pop($reconstruire);
1082
-		$fonction = $r[0];
1083
-		$args = $r[1];
1084
-		call_user_func_array($fonction, $args);
1085
-	}
1086
-	// cette image intermediaire est commune a plusieurs series de filtre, il faut la conserver
1087
-	// mais l'on peut nettoyer les miettes de sa creation
1088
-	ramasse_miettes($fichier_manquant);
1068
+    $reconstruire = [];
1069
+    $fichier = $fichier_manquant;
1070
+    while (
1071
+        strpos($fichier, '://') === false
1072
+        and !@file_exists($fichier)
1073
+        and lire_fichier($src = "$fichier.src", $source)
1074
+        and $valeurs = unserialize($source)
1075
+        and ($fichier = $valeurs['fichier']) # l'origine est connue (on ne verifie pas son existence, qu'importe ...)
1076
+    ) {
1077
+        spip_unlink($src); // si jamais on a un timeout pendant la reconstruction, elle se fera naturellement au hit suivant
1078
+        $reconstruire[] = $valeurs['reconstruction'];
1079
+    }
1080
+    while (count($reconstruire)) {
1081
+        $r = array_pop($reconstruire);
1082
+        $fonction = $r[0];
1083
+        $args = $r[1];
1084
+        call_user_func_array($fonction, $args);
1085
+    }
1086
+    // cette image intermediaire est commune a plusieurs series de filtre, il faut la conserver
1087
+    // mais l'on peut nettoyer les miettes de sa creation
1088
+    ramasse_miettes($fichier_manquant);
1089 1089
 }
1090 1090
 
1091 1091
 /**
@@ -1105,28 +1105,28 @@  discard block
 block discarded – undo
1105 1105
  *     Chemin du fichier d'image calculé
1106 1106
  **/
1107 1107
 function ramasse_miettes($fichier) {
1108
-	if (
1109
-		strpos($fichier, '://') !== false
1110
-		or !lire_fichier($src = "$fichier.src", $source)
1111
-		or !$valeurs = unserialize($source)
1112
-	) {
1113
-		return;
1114
-	}
1115
-	spip_unlink($src); # on supprime la reference a sa source pour marquer cette image comme non intermediaire
1116
-	while (
1117
-		($fichier = $valeurs['fichier']) # l'origine est connue (on ne verifie pas son existence, qu'importe ...)
1118
-		and (substr($fichier, 0, strlen(_DIR_VAR)) == _DIR_VAR) # et est dans local
1119
-		and (lire_fichier(
1120
-			$src = "$fichier.src",
1121
-			$source
1122
-		)) # le fichier a une source connue (c'est donc une image calculee intermediaire)
1123
-		and ($valeurs = unserialize($source))  # et valide
1124
-	) {
1125
-		# on efface le fichier
1126
-		spip_unlink($fichier);
1127
-		# mais laisse le .src qui permet de savoir comment reconstruire l'image si besoin
1128
-		#spip_unlink($src);
1129
-	}
1108
+    if (
1109
+        strpos($fichier, '://') !== false
1110
+        or !lire_fichier($src = "$fichier.src", $source)
1111
+        or !$valeurs = unserialize($source)
1112
+    ) {
1113
+        return;
1114
+    }
1115
+    spip_unlink($src); # on supprime la reference a sa source pour marquer cette image comme non intermediaire
1116
+    while (
1117
+        ($fichier = $valeurs['fichier']) # l'origine est connue (on ne verifie pas son existence, qu'importe ...)
1118
+        and (substr($fichier, 0, strlen(_DIR_VAR)) == _DIR_VAR) # et est dans local
1119
+        and (lire_fichier(
1120
+            $src = "$fichier.src",
1121
+            $source
1122
+        )) # le fichier a une source connue (c'est donc une image calculee intermediaire)
1123
+        and ($valeurs = unserialize($source))  # et valide
1124
+    ) {
1125
+        # on efface le fichier
1126
+        spip_unlink($fichier);
1127
+        # mais laisse le .src qui permet de savoir comment reconstruire l'image si besoin
1128
+        #spip_unlink($src);
1129
+    }
1130 1130
 }
1131 1131
 
1132 1132
 
@@ -1151,71 +1151,71 @@  discard block
 block discarded – undo
1151 1151
  *     Code HTML de l'image
1152 1152
  **/
1153 1153
 function image_graver($img) {
1154
-	// appeler le filtre post_image_filtrer qui permet de faire
1155
-	// des traitements auto a la fin d'une serie de filtres
1156
-	$img = pipeline('post_image_filtrer', $img);
1157
-
1158
-	$fichier_ori = $fichier = extraire_attribut($img, 'src');
1159
-	if (($p = strpos($fichier, '?')) !== false) {
1160
-		$fichier = substr($fichier, 0, $p);
1161
-	}
1162
-	if (strlen($fichier) < 1) {
1163
-		$fichier = $img;
1164
-	}
1165
-	# si jamais le fichier final n'a pas ete calcule car suppose temporaire
1166
-	# et qu'il ne s'agit pas d'une URL
1167
-	if (strpos($fichier, '://') === false and !@file_exists($fichier)) {
1168
-		reconstruire_image_intermediaire($fichier);
1169
-	}
1170
-	ramasse_miettes($fichier);
1171
-
1172
-	// ajouter le timestamp si besoin
1173
-	if (strpos($fichier_ori, '?') === false) {
1174
-		// on utilise str_replace pour attraper le onmouseover des logo si besoin
1175
-		$img = str_replace($fichier_ori, timestamp($fichier_ori), $img);
1176
-	}
1177
-
1178
-	return $img;
1154
+    // appeler le filtre post_image_filtrer qui permet de faire
1155
+    // des traitements auto a la fin d'une serie de filtres
1156
+    $img = pipeline('post_image_filtrer', $img);
1157
+
1158
+    $fichier_ori = $fichier = extraire_attribut($img, 'src');
1159
+    if (($p = strpos($fichier, '?')) !== false) {
1160
+        $fichier = substr($fichier, 0, $p);
1161
+    }
1162
+    if (strlen($fichier) < 1) {
1163
+        $fichier = $img;
1164
+    }
1165
+    # si jamais le fichier final n'a pas ete calcule car suppose temporaire
1166
+    # et qu'il ne s'agit pas d'une URL
1167
+    if (strpos($fichier, '://') === false and !@file_exists($fichier)) {
1168
+        reconstruire_image_intermediaire($fichier);
1169
+    }
1170
+    ramasse_miettes($fichier);
1171
+
1172
+    // ajouter le timestamp si besoin
1173
+    if (strpos($fichier_ori, '?') === false) {
1174
+        // on utilise str_replace pour attraper le onmouseover des logo si besoin
1175
+        $img = str_replace($fichier_ori, timestamp($fichier_ori), $img);
1176
+    }
1177
+
1178
+    return $img;
1179 1179
 }
1180 1180
 
1181 1181
 
1182 1182
 if (!function_exists('imagepalettetotruecolor')) {
1183
-	/**
1184
-	 * Transforme une image à palette indexée (256 couleurs max) en "vraies" couleurs RGB
1185
-	 *
1186
-	 * @note Pour compatibilité avec PHP < 5.5
1187
-	 *
1188
-	 * @link http://php.net/manual/fr/function.imagepalettetotruecolor.php
1189
-	 *
1190
-	 * @param ressource $img
1191
-	 * @return bool
1192
-	 *     - true si l'image est déjà en vrai RGB ou peut être transformée
1193
-	 *     - false si la transformation ne peut être faite.
1194
-	 **/
1195
-	function imagepalettetotruecolor(&$img) {
1196
-		if (!$img or !function_exists('imagecreatetruecolor')) {
1197
-			return false;
1198
-		} elseif (!imageistruecolor($img)) {
1199
-			$w = imagesx($img);
1200
-			$h = imagesy($img);
1201
-			$img1 = imagecreatetruecolor($w, $h);
1202
-			//Conserver la transparence si possible
1203
-			if (function_exists('ImageCopyResampled')) {
1204
-				if (function_exists('imageAntiAlias')) {
1205
-					imageAntiAlias($img1, true);
1206
-				}
1207
-				@imagealphablending($img1, false);
1208
-				@imagesavealpha($img1, true);
1209
-				@ImageCopyResampled($img1, $img, 0, 0, 0, 0, $w, $h, $w, $h);
1210
-			} else {
1211
-				imagecopy($img1, $img, 0, 0, 0, 0, $w, $h);
1212
-			}
1213
-
1214
-			$img = $img1;
1215
-		}
1216
-
1217
-		return true;
1218
-	}
1183
+    /**
1184
+     * Transforme une image à palette indexée (256 couleurs max) en "vraies" couleurs RGB
1185
+     *
1186
+     * @note Pour compatibilité avec PHP < 5.5
1187
+     *
1188
+     * @link http://php.net/manual/fr/function.imagepalettetotruecolor.php
1189
+     *
1190
+     * @param ressource $img
1191
+     * @return bool
1192
+     *     - true si l'image est déjà en vrai RGB ou peut être transformée
1193
+     *     - false si la transformation ne peut être faite.
1194
+     **/
1195
+    function imagepalettetotruecolor(&$img) {
1196
+        if (!$img or !function_exists('imagecreatetruecolor')) {
1197
+            return false;
1198
+        } elseif (!imageistruecolor($img)) {
1199
+            $w = imagesx($img);
1200
+            $h = imagesy($img);
1201
+            $img1 = imagecreatetruecolor($w, $h);
1202
+            //Conserver la transparence si possible
1203
+            if (function_exists('ImageCopyResampled')) {
1204
+                if (function_exists('imageAntiAlias')) {
1205
+                    imageAntiAlias($img1, true);
1206
+                }
1207
+                @imagealphablending($img1, false);
1208
+                @imagesavealpha($img1, true);
1209
+                @ImageCopyResampled($img1, $img, 0, 0, 0, 0, $w, $h, $w, $h);
1210
+            } else {
1211
+                imagecopy($img1, $img, 0, 0, 0, 0, $w, $h);
1212
+            }
1213
+
1214
+            $img = $img1;
1215
+        }
1216
+
1217
+        return true;
1218
+    }
1219 1219
 }
1220 1220
 
1221 1221
 /**
@@ -1242,34 +1242,34 @@  discard block
 block discarded – undo
1242 1242
  *     Code html modifié de la balise.
1243 1243
  **/
1244 1244
 function _image_tag_changer_taille($tag, $width, $height, $style = false) {
1245
-	if ($style === false) {
1246
-		$style = extraire_attribut($tag, 'style');
1247
-	}
1248
-
1249
-	// enlever le width et height du style
1250
-	if ($style) {
1251
-		$style = preg_replace(',(^|;)\s*(width|height)\s*:\s*[^;]+,ims', '', $style);
1252
-	}
1253
-	if ($style and $style[0] === ';') {
1254
-		$style = substr($style, 1);
1255
-	}
1256
-
1257
-	// mettre des attributs de width et height sur les images,
1258
-	// ca accelere le rendu du navigateur
1259
-	// ca permet aux navigateurs de reserver la bonne taille
1260
-	// quand on a desactive l'affichage des images.
1261
-	$tag = inserer_attribut($tag, 'width', round($width));
1262
-	$tag = inserer_attribut($tag, 'height', round($height));
1263
-
1264
-	// attributs deprecies. Transformer en CSS
1265
-	if ($espace = extraire_attribut($tag, 'hspace')) {
1266
-		$style = "margin:${espace}px;" . $style;
1267
-		$tag = inserer_attribut($tag, 'hspace', '');
1268
-	}
1269
-
1270
-	$tag = inserer_attribut($tag, 'style', (string) $style, true, $style ? false : true);
1271
-
1272
-	return $tag;
1245
+    if ($style === false) {
1246
+        $style = extraire_attribut($tag, 'style');
1247
+    }
1248
+
1249
+    // enlever le width et height du style
1250
+    if ($style) {
1251
+        $style = preg_replace(',(^|;)\s*(width|height)\s*:\s*[^;]+,ims', '', $style);
1252
+    }
1253
+    if ($style and $style[0] === ';') {
1254
+        $style = substr($style, 1);
1255
+    }
1256
+
1257
+    // mettre des attributs de width et height sur les images,
1258
+    // ca accelere le rendu du navigateur
1259
+    // ca permet aux navigateurs de reserver la bonne taille
1260
+    // quand on a desactive l'affichage des images.
1261
+    $tag = inserer_attribut($tag, 'width', round($width));
1262
+    $tag = inserer_attribut($tag, 'height', round($height));
1263
+
1264
+    // attributs deprecies. Transformer en CSS
1265
+    if ($espace = extraire_attribut($tag, 'hspace')) {
1266
+        $style = "margin:${espace}px;" . $style;
1267
+        $tag = inserer_attribut($tag, 'hspace', '');
1268
+    }
1269
+
1270
+    $tag = inserer_attribut($tag, 'style', (string) $style, true, $style ? false : true);
1271
+
1272
+    return $tag;
1273 1273
 }
1274 1274
 
1275 1275
 
@@ -1295,72 +1295,72 @@  discard block
 block discarded – undo
1295 1295
  *     Retourne le code HTML de l'image
1296 1296
  **/
1297 1297
 function _image_ecrire_tag($valeurs, $surcharge = []) {
1298
-	$valeurs = pipeline('image_ecrire_tag_preparer', $valeurs);
1299
-
1300
-	// fermer les tags img pas bien fermes;
1301
-	$tag = str_replace('>', '/>', str_replace('/>', '>', $valeurs['tag']));
1302
-
1303
-	// le style
1304
-	$style = $valeurs['style'];
1305
-	if (isset($surcharge['style'])) {
1306
-		$style = $surcharge['style'];
1307
-		unset($surcharge['style']);
1308
-	}
1309
-
1310
-	// traiter specifiquement la largeur et la hauteur
1311
-	$width = $valeurs['largeur'];
1312
-	if (isset($surcharge['width'])) {
1313
-		$width = $surcharge['width'];
1314
-		unset($surcharge['width']);
1315
-	}
1316
-	$height = $valeurs['hauteur'];
1317
-	if (isset($surcharge['height'])) {
1318
-		$height = $surcharge['height'];
1319
-		unset($surcharge['height']);
1320
-	}
1321
-
1322
-	$tag = _image_tag_changer_taille($tag, $width, $height, $style);
1323
-	// traiter specifiquement le src qui peut etre repris dans un onmouseout
1324
-	// on remplace toute les ref a src dans le tag
1325
-	$src = extraire_attribut($tag, 'src');
1326
-	if (isset($surcharge['src'])) {
1327
-		$tag = str_replace($src, $surcharge['src'], $tag);
1328
-		// si il y a des & dans src, alors ils peuvent provenir d'un &amp
1329
-		// pas garanti comme methode, mais mieux que rien
1330
-		if (strpos($src, '&') !== false) {
1331
-			$tag = str_replace(str_replace('&', '&amp;', $src), $surcharge['src'], $tag);
1332
-		}
1333
-		$src = $surcharge['src'];
1334
-		unset($surcharge['src']);
1335
-	}
1336
-
1337
-	$class = $valeurs['class'];
1338
-	if (isset($surcharge['class'])) {
1339
-		$class = $surcharge['class'];
1340
-		unset($surcharge['class']);
1341
-	}
1342
-	if (is_scalar($class) && strlen($class)) {
1343
-		$tag = inserer_attribut($tag, 'class', $class);
1344
-	}
1345
-
1346
-	if (count($surcharge)) {
1347
-		foreach ($surcharge as $attribut => $valeur) {
1348
-			$tag = inserer_attribut($tag, $attribut, $valeur);
1349
-		}
1350
-	}
1351
-
1352
-	$tag = pipeline(
1353
-		'image_ecrire_tag_finir',
1354
-		[
1355
-			'args' => [
1356
-				'valeurs' => $valeurs,
1357
-				'surcharge' => $surcharge,
1358
-			],
1359
-			'data' => $tag
1360
-		]
1361
-	);
1362
-
1363
-	return $tag;
1298
+    $valeurs = pipeline('image_ecrire_tag_preparer', $valeurs);
1299
+
1300
+    // fermer les tags img pas bien fermes;
1301
+    $tag = str_replace('>', '/>', str_replace('/>', '>', $valeurs['tag']));
1302
+
1303
+    // le style
1304
+    $style = $valeurs['style'];
1305
+    if (isset($surcharge['style'])) {
1306
+        $style = $surcharge['style'];
1307
+        unset($surcharge['style']);
1308
+    }
1309
+
1310
+    // traiter specifiquement la largeur et la hauteur
1311
+    $width = $valeurs['largeur'];
1312
+    if (isset($surcharge['width'])) {
1313
+        $width = $surcharge['width'];
1314
+        unset($surcharge['width']);
1315
+    }
1316
+    $height = $valeurs['hauteur'];
1317
+    if (isset($surcharge['height'])) {
1318
+        $height = $surcharge['height'];
1319
+        unset($surcharge['height']);
1320
+    }
1321
+
1322
+    $tag = _image_tag_changer_taille($tag, $width, $height, $style);
1323
+    // traiter specifiquement le src qui peut etre repris dans un onmouseout
1324
+    // on remplace toute les ref a src dans le tag
1325
+    $src = extraire_attribut($tag, 'src');
1326
+    if (isset($surcharge['src'])) {
1327
+        $tag = str_replace($src, $surcharge['src'], $tag);
1328
+        // si il y a des & dans src, alors ils peuvent provenir d'un &amp
1329
+        // pas garanti comme methode, mais mieux que rien
1330
+        if (strpos($src, '&') !== false) {
1331
+            $tag = str_replace(str_replace('&', '&amp;', $src), $surcharge['src'], $tag);
1332
+        }
1333
+        $src = $surcharge['src'];
1334
+        unset($surcharge['src']);
1335
+    }
1336
+
1337
+    $class = $valeurs['class'];
1338
+    if (isset($surcharge['class'])) {
1339
+        $class = $surcharge['class'];
1340
+        unset($surcharge['class']);
1341
+    }
1342
+    if (is_scalar($class) && strlen($class)) {
1343
+        $tag = inserer_attribut($tag, 'class', $class);
1344
+    }
1345
+
1346
+    if (count($surcharge)) {
1347
+        foreach ($surcharge as $attribut => $valeur) {
1348
+            $tag = inserer_attribut($tag, $attribut, $valeur);
1349
+        }
1350
+    }
1351
+
1352
+    $tag = pipeline(
1353
+        'image_ecrire_tag_finir',
1354
+        [
1355
+            'args' => [
1356
+                'valeurs' => $valeurs,
1357
+                'surcharge' => $surcharge,
1358
+            ],
1359
+            'data' => $tag
1360
+        ]
1361
+    );
1362
+
1363
+    return $tag;
1364 1364
 }
1365 1365
 
1366 1366
 /**
@@ -1383,259 +1383,259 @@  discard block
 block discarded – undo
1383 1383
  *     Description de l'image, sinon null.
1384 1384
  **/
1385 1385
 function _image_creer_vignette($valeurs, $maxWidth, $maxHeight, $process = 'AUTO', $force = false) {
1386
-	$srcHeight = null;
1387
-	$retour = [];
1388
-	// ordre de preference des formats graphiques pour creer les vignettes
1389
-	// le premier format disponible, selon la methode demandee, est utilise
1390
-	$image = $valeurs['fichier'];
1391
-	$format = $valeurs['format_source'];
1392
-	$destdir = dirname($valeurs['fichier_dest']);
1393
-	$destfile = basename($valeurs['fichier_dest'], '.' . $valeurs['format_dest']);
1394
-
1395
-	$format_sortie = $valeurs['format_dest'];
1396
-
1397
-	if (($process == 'AUTO') and isset($GLOBALS['meta']['image_process'])) {
1398
-		$process = $GLOBALS['meta']['image_process'];
1399
-	}
1400
-
1401
-	// si le doc n'est pas une image dans un format accetpable, refuser
1402
-	if (!$force and !in_array($format, formats_image_acceptables(in_array($process, ['gd1', 'gd2'])))) {
1403
-		return;
1404
-	}
1405
-	$destination = "$destdir/$destfile";
1406
-
1407
-	// calculer la taille
1408
-	if (($srcWidth = $valeurs['largeur']) && ($srcHeight = $valeurs['hauteur'])) {
1409
-		if (!($destWidth = $valeurs['largeur_dest']) || !($destHeight = $valeurs['hauteur_dest'])) {
1410
-			[$destWidth, $destHeight] = _image_ratio($srcWidth, $srcHeight, $maxWidth, $maxHeight);
1411
-		}
1412
-	} elseif ($process == 'convert' or $process == 'imagick') {
1413
-		$destWidth = $maxWidth;
1414
-		$destHeight = $maxHeight;
1415
-	} else {
1416
-		spip_log("echec $process sur $image");
1417
-
1418
-		return;
1419
-	}
1420
-
1421
-	$vignette = '';
1422
-
1423
-	// Si l'image est de la taille demandee (ou plus petite), simplement la retourner
1424
-	if ($srcWidth and $srcWidth <= $maxWidth and $srcHeight <= $maxHeight) {
1425
-		$vignette = $destination . '.' . $format;
1426
-		@copy($image, $vignette);
1427
-	}
1428
-
1429
-	elseif ($valeurs['format_source'] === 'svg') {
1430
-		if ($svg = svg_redimensionner($valeurs['fichier'], $destWidth, $destHeight)) {
1431
-			$format_sortie = 'svg';
1432
-			$vignette = $destination . '.' . $format_sortie;
1433
-			$valeurs['fichier_dest'] = $vignette;
1434
-			_image_gd_output($svg, $valeurs);
1435
-		}
1436
-	}
1437
-
1438
-	// imagemagick en ligne de commande
1439
-	elseif ($process == 'convert') {
1440
-		if (!defined('_CONVERT_COMMAND')) {
1441
-			define('_CONVERT_COMMAND', 'convert');
1442
-		} // Securite : mes_options.php peut preciser le chemin absolu
1443
-		if (!defined('_RESIZE_COMMAND')) {
1444
-			define('_RESIZE_COMMAND', _CONVERT_COMMAND . ' -quality ' . _IMG_CONVERT_QUALITE . ' -resize %xx%y! %src %dest');
1445
-		}
1446
-		$vignette = $destination . '.' . $format_sortie;
1447
-		$commande = str_replace(
1448
-			['%x', '%y', '%src', '%dest'],
1449
-			[
1450
-				$destWidth,
1451
-				$destHeight,
1452
-				escapeshellcmd($image),
1453
-				escapeshellcmd($vignette)
1454
-			],
1455
-			_RESIZE_COMMAND
1456
-		);
1457
-		spip_log($commande);
1458
-		exec($commande);
1459
-		if (!@file_exists($vignette)) {
1460
-			spip_log("echec convert sur $vignette");
1461
-
1462
-			return;  // echec commande
1463
-		}
1464
-	}
1465
-
1466
-	// php5 imagemagick
1467
-	elseif ($process == 'imagick') {
1468
-		$vignette = "$destination." . $format_sortie;
1469
-
1470
-		if (!class_exists(\Imagick::class)) {
1471
-			spip_log('Classe Imagick absente !', _LOG_ERREUR);
1472
-
1473
-			return;
1474
-		}
1475
-		$imagick = new Imagick();
1476
-		$imagick->readImage($image);
1477
-		$imagick->resizeImage(
1478
-			$destWidth,
1479
-			$destHeight,
1480
-			Imagick::FILTER_LANCZOS,
1481
-			1
1482
-		);//, IMAGICK_FILTER_LANCZOS, _IMG_IMAGICK_QUALITE / 100);
1483
-		$imagick->writeImage($vignette);
1484
-
1485
-		if (!@file_exists($vignette)) {
1486
-			spip_log("echec imagick sur $vignette");
1487
-
1488
-			return;
1489
-		}
1490
-	}
1491
-
1492
-	// netpbm
1493
-	elseif ($process == 'netpbm') {
1494
-		if (!defined('_PNMSCALE_COMMAND')) {
1495
-			define('_PNMSCALE_COMMAND', 'pnmscale');
1496
-		} // Securite : mes_options.php peut preciser le chemin absolu
1497
-		if (_PNMSCALE_COMMAND == '') {
1498
-			return;
1499
-		}
1500
-		$vignette = $destination . '.' . $format_sortie;
1501
-		$pnmtojpeg_command = str_replace('pnmscale', 'pnmtojpeg', _PNMSCALE_COMMAND);
1502
-		if ($format == 'jpg') {
1503
-			$jpegtopnm_command = str_replace('pnmscale', 'jpegtopnm', _PNMSCALE_COMMAND);
1504
-			exec("$jpegtopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette");
1505
-			if (!($s = @filesize($vignette))) {
1506
-				spip_unlink($vignette);
1507
-			}
1508
-			if (!@file_exists($vignette)) {
1509
-				spip_log("echec netpbm-jpg sur $vignette");
1510
-
1511
-				return;
1512
-			}
1513
-		} else {
1514
-			if ($format == 'gif') {
1515
-				$giftopnm_command = str_replace('pnmscale', 'giftopnm', _PNMSCALE_COMMAND);
1516
-				exec("$giftopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette");
1517
-				if (!($s = @filesize($vignette))) {
1518
-					spip_unlink($vignette);
1519
-				}
1520
-				if (!@file_exists($vignette)) {
1521
-					spip_log("echec netpbm-gif sur $vignette");
1522
-
1523
-					return;
1524
-				}
1525
-			} else {
1526
-				if ($format == 'png') {
1527
-					$pngtopnm_command = str_replace('pnmscale', 'pngtopnm', _PNMSCALE_COMMAND);
1528
-					exec("$pngtopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette");
1529
-					if (!($s = @filesize($vignette))) {
1530
-						spip_unlink($vignette);
1531
-					}
1532
-					if (!@file_exists($vignette)) {
1533
-						spip_log("echec netpbm-png sur $vignette");
1534
-
1535
-						return;
1536
-					}
1537
-				}
1538
-			}
1539
-		}
1540
-	}
1541
-
1542
-	// gd ou gd2
1543
-	elseif ($process == 'gd1' or $process == 'gd2') {
1544
-		if (!function_exists('gd_info')) {
1545
-			spip_log('Librairie GD absente !', _LOG_ERREUR);
1546
-
1547
-			return;
1548
-		}
1549
-		if (_IMG_GD_MAX_PIXELS && $srcWidth * $srcHeight > _IMG_GD_MAX_PIXELS) {
1550
-			spip_log('vignette gd1/gd2 impossible : ' . $srcWidth * $srcHeight . 'pixels');
1551
-
1552
-			return;
1553
-		}
1554
-		$destFormat = $format_sortie;
1555
-		if (!$destFormat) {
1556
-			spip_log("pas de format pour $image");
1557
-
1558
-			return;
1559
-		}
1560
-
1561
-		$fonction_imagecreatefrom = $valeurs['fonction_imagecreatefrom'];
1562
-		if (!function_exists($fonction_imagecreatefrom)) {
1563
-			return '';
1564
-		}
1565
-		$srcImage = @$fonction_imagecreatefrom($image);
1566
-		if (!$srcImage) {
1567
-			spip_log('echec gd1/gd2');
1568
-
1569
-			return;
1570
-		}
1571
-
1572
-		// Initialisation de l'image destination
1573
-		$destImage = null;
1574
-		if ($process == 'gd2' and $destFormat != 'gif') {
1575
-			$destImage = ImageCreateTrueColor($destWidth, $destHeight);
1576
-		}
1577
-		if (!$destImage) {
1578
-			$destImage = ImageCreate($destWidth, $destHeight);
1579
-		}
1580
-
1581
-		// Recopie de l'image d'origine avec adaptation de la taille
1582
-		$ok = false;
1583
-		if (($process == 'gd2') and function_exists('ImageCopyResampled')) {
1584
-			if ($format == 'gif') {
1585
-				// Si un GIF est transparent,
1586
-				// fabriquer un PNG transparent
1587
-				$transp = imagecolortransparent($srcImage);
1588
-				if ($transp > 0) {
1589
-					$destFormat = 'png';
1590
-				}
1591
-			}
1592
-			if (in_array($destFormat, _image_extensions_conservent_transparence())) {
1593
-				// Conserver la transparence
1594
-				if (function_exists('imageAntiAlias')) {
1595
-					imageAntiAlias($destImage, true);
1596
-				}
1597
-				@imagealphablending($destImage, false);
1598
-				@imagesavealpha($destImage, true);
1599
-			}
1600
-			$ok = @ImageCopyResampled($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
1601
-		}
1602
-		if (!$ok) {
1603
-			$ok = ImageCopyResized($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
1604
-		}
1605
-
1606
-		// Sauvegarde de l'image destination
1607
-		$valeurs['fichier_dest'] = $vignette = "$destination.$destFormat";
1608
-		$valeurs['format_dest'] = $format = $destFormat;
1609
-		_image_gd_output($destImage, $valeurs);
1610
-
1611
-		if ($srcImage) {
1612
-			ImageDestroy($srcImage);
1613
-		}
1614
-		ImageDestroy($destImage);
1615
-	}
1616
-
1617
-	if (!$vignette or !$size = @spip_getimagesize($vignette)) {
1618
-		$size = [$destWidth, $destHeight];
1619
-	}
1620
-
1621
-	// Gaffe: en safe mode, pas d'acces a la vignette,
1622
-	// donc risque de balancer "width='0'", ce qui masque l'image sous MSIE
1623
-	if ($size[0] < 1) {
1624
-		$size[0] = $destWidth;
1625
-	}
1626
-	if ($size[1] < 1) {
1627
-		$size[1] = $destHeight;
1628
-	}
1629
-
1630
-	$retour['width'] = $largeur = $size[0];
1631
-	$retour['height'] = $hauteur = $size[1];
1632
-
1633
-	$retour['fichier'] = $vignette;
1634
-	$retour['format'] = $format;
1635
-	$retour['date'] = @filemtime($vignette);
1636
-
1637
-	// renvoyer l'image
1638
-	return $retour;
1386
+    $srcHeight = null;
1387
+    $retour = [];
1388
+    // ordre de preference des formats graphiques pour creer les vignettes
1389
+    // le premier format disponible, selon la methode demandee, est utilise
1390
+    $image = $valeurs['fichier'];
1391
+    $format = $valeurs['format_source'];
1392
+    $destdir = dirname($valeurs['fichier_dest']);
1393
+    $destfile = basename($valeurs['fichier_dest'], '.' . $valeurs['format_dest']);
1394
+
1395
+    $format_sortie = $valeurs['format_dest'];
1396
+
1397
+    if (($process == 'AUTO') and isset($GLOBALS['meta']['image_process'])) {
1398
+        $process = $GLOBALS['meta']['image_process'];
1399
+    }
1400
+
1401
+    // si le doc n'est pas une image dans un format accetpable, refuser
1402
+    if (!$force and !in_array($format, formats_image_acceptables(in_array($process, ['gd1', 'gd2'])))) {
1403
+        return;
1404
+    }
1405
+    $destination = "$destdir/$destfile";
1406
+
1407
+    // calculer la taille
1408
+    if (($srcWidth = $valeurs['largeur']) && ($srcHeight = $valeurs['hauteur'])) {
1409
+        if (!($destWidth = $valeurs['largeur_dest']) || !($destHeight = $valeurs['hauteur_dest'])) {
1410
+            [$destWidth, $destHeight] = _image_ratio($srcWidth, $srcHeight, $maxWidth, $maxHeight);
1411
+        }
1412
+    } elseif ($process == 'convert' or $process == 'imagick') {
1413
+        $destWidth = $maxWidth;
1414
+        $destHeight = $maxHeight;
1415
+    } else {
1416
+        spip_log("echec $process sur $image");
1417
+
1418
+        return;
1419
+    }
1420
+
1421
+    $vignette = '';
1422
+
1423
+    // Si l'image est de la taille demandee (ou plus petite), simplement la retourner
1424
+    if ($srcWidth and $srcWidth <= $maxWidth and $srcHeight <= $maxHeight) {
1425
+        $vignette = $destination . '.' . $format;
1426
+        @copy($image, $vignette);
1427
+    }
1428
+
1429
+    elseif ($valeurs['format_source'] === 'svg') {
1430
+        if ($svg = svg_redimensionner($valeurs['fichier'], $destWidth, $destHeight)) {
1431
+            $format_sortie = 'svg';
1432
+            $vignette = $destination . '.' . $format_sortie;
1433
+            $valeurs['fichier_dest'] = $vignette;
1434
+            _image_gd_output($svg, $valeurs);
1435
+        }
1436
+    }
1437
+
1438
+    // imagemagick en ligne de commande
1439
+    elseif ($process == 'convert') {
1440
+        if (!defined('_CONVERT_COMMAND')) {
1441
+            define('_CONVERT_COMMAND', 'convert');
1442
+        } // Securite : mes_options.php peut preciser le chemin absolu
1443
+        if (!defined('_RESIZE_COMMAND')) {
1444
+            define('_RESIZE_COMMAND', _CONVERT_COMMAND . ' -quality ' . _IMG_CONVERT_QUALITE . ' -resize %xx%y! %src %dest');
1445
+        }
1446
+        $vignette = $destination . '.' . $format_sortie;
1447
+        $commande = str_replace(
1448
+            ['%x', '%y', '%src', '%dest'],
1449
+            [
1450
+                $destWidth,
1451
+                $destHeight,
1452
+                escapeshellcmd($image),
1453
+                escapeshellcmd($vignette)
1454
+            ],
1455
+            _RESIZE_COMMAND
1456
+        );
1457
+        spip_log($commande);
1458
+        exec($commande);
1459
+        if (!@file_exists($vignette)) {
1460
+            spip_log("echec convert sur $vignette");
1461
+
1462
+            return;  // echec commande
1463
+        }
1464
+    }
1465
+
1466
+    // php5 imagemagick
1467
+    elseif ($process == 'imagick') {
1468
+        $vignette = "$destination." . $format_sortie;
1469
+
1470
+        if (!class_exists(\Imagick::class)) {
1471
+            spip_log('Classe Imagick absente !', _LOG_ERREUR);
1472
+
1473
+            return;
1474
+        }
1475
+        $imagick = new Imagick();
1476
+        $imagick->readImage($image);
1477
+        $imagick->resizeImage(
1478
+            $destWidth,
1479
+            $destHeight,
1480
+            Imagick::FILTER_LANCZOS,
1481
+            1
1482
+        );//, IMAGICK_FILTER_LANCZOS, _IMG_IMAGICK_QUALITE / 100);
1483
+        $imagick->writeImage($vignette);
1484
+
1485
+        if (!@file_exists($vignette)) {
1486
+            spip_log("echec imagick sur $vignette");
1487
+
1488
+            return;
1489
+        }
1490
+    }
1491
+
1492
+    // netpbm
1493
+    elseif ($process == 'netpbm') {
1494
+        if (!defined('_PNMSCALE_COMMAND')) {
1495
+            define('_PNMSCALE_COMMAND', 'pnmscale');
1496
+        } // Securite : mes_options.php peut preciser le chemin absolu
1497
+        if (_PNMSCALE_COMMAND == '') {
1498
+            return;
1499
+        }
1500
+        $vignette = $destination . '.' . $format_sortie;
1501
+        $pnmtojpeg_command = str_replace('pnmscale', 'pnmtojpeg', _PNMSCALE_COMMAND);
1502
+        if ($format == 'jpg') {
1503
+            $jpegtopnm_command = str_replace('pnmscale', 'jpegtopnm', _PNMSCALE_COMMAND);
1504
+            exec("$jpegtopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette");
1505
+            if (!($s = @filesize($vignette))) {
1506
+                spip_unlink($vignette);
1507
+            }
1508
+            if (!@file_exists($vignette)) {
1509
+                spip_log("echec netpbm-jpg sur $vignette");
1510
+
1511
+                return;
1512
+            }
1513
+        } else {
1514
+            if ($format == 'gif') {
1515
+                $giftopnm_command = str_replace('pnmscale', 'giftopnm', _PNMSCALE_COMMAND);
1516
+                exec("$giftopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette");
1517
+                if (!($s = @filesize($vignette))) {
1518
+                    spip_unlink($vignette);
1519
+                }
1520
+                if (!@file_exists($vignette)) {
1521
+                    spip_log("echec netpbm-gif sur $vignette");
1522
+
1523
+                    return;
1524
+                }
1525
+            } else {
1526
+                if ($format == 'png') {
1527
+                    $pngtopnm_command = str_replace('pnmscale', 'pngtopnm', _PNMSCALE_COMMAND);
1528
+                    exec("$pngtopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette");
1529
+                    if (!($s = @filesize($vignette))) {
1530
+                        spip_unlink($vignette);
1531
+                    }
1532
+                    if (!@file_exists($vignette)) {
1533
+                        spip_log("echec netpbm-png sur $vignette");
1534
+
1535
+                        return;
1536
+                    }
1537
+                }
1538
+            }
1539
+        }
1540
+    }
1541
+
1542
+    // gd ou gd2
1543
+    elseif ($process == 'gd1' or $process == 'gd2') {
1544
+        if (!function_exists('gd_info')) {
1545
+            spip_log('Librairie GD absente !', _LOG_ERREUR);
1546
+
1547
+            return;
1548
+        }
1549
+        if (_IMG_GD_MAX_PIXELS && $srcWidth * $srcHeight > _IMG_GD_MAX_PIXELS) {
1550
+            spip_log('vignette gd1/gd2 impossible : ' . $srcWidth * $srcHeight . 'pixels');
1551
+
1552
+            return;
1553
+        }
1554
+        $destFormat = $format_sortie;
1555
+        if (!$destFormat) {
1556
+            spip_log("pas de format pour $image");
1557
+
1558
+            return;
1559
+        }
1560
+
1561
+        $fonction_imagecreatefrom = $valeurs['fonction_imagecreatefrom'];
1562
+        if (!function_exists($fonction_imagecreatefrom)) {
1563
+            return '';
1564
+        }
1565
+        $srcImage = @$fonction_imagecreatefrom($image);
1566
+        if (!$srcImage) {
1567
+            spip_log('echec gd1/gd2');
1568
+
1569
+            return;
1570
+        }
1571
+
1572
+        // Initialisation de l'image destination
1573
+        $destImage = null;
1574
+        if ($process == 'gd2' and $destFormat != 'gif') {
1575
+            $destImage = ImageCreateTrueColor($destWidth, $destHeight);
1576
+        }
1577
+        if (!$destImage) {
1578
+            $destImage = ImageCreate($destWidth, $destHeight);
1579
+        }
1580
+
1581
+        // Recopie de l'image d'origine avec adaptation de la taille
1582
+        $ok = false;
1583
+        if (($process == 'gd2') and function_exists('ImageCopyResampled')) {
1584
+            if ($format == 'gif') {
1585
+                // Si un GIF est transparent,
1586
+                // fabriquer un PNG transparent
1587
+                $transp = imagecolortransparent($srcImage);
1588
+                if ($transp > 0) {
1589
+                    $destFormat = 'png';
1590
+                }
1591
+            }
1592
+            if (in_array($destFormat, _image_extensions_conservent_transparence())) {
1593
+                // Conserver la transparence
1594
+                if (function_exists('imageAntiAlias')) {
1595
+                    imageAntiAlias($destImage, true);
1596
+                }
1597
+                @imagealphablending($destImage, false);
1598
+                @imagesavealpha($destImage, true);
1599
+            }
1600
+            $ok = @ImageCopyResampled($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
1601
+        }
1602
+        if (!$ok) {
1603
+            $ok = ImageCopyResized($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
1604
+        }
1605
+
1606
+        // Sauvegarde de l'image destination
1607
+        $valeurs['fichier_dest'] = $vignette = "$destination.$destFormat";
1608
+        $valeurs['format_dest'] = $format = $destFormat;
1609
+        _image_gd_output($destImage, $valeurs);
1610
+
1611
+        if ($srcImage) {
1612
+            ImageDestroy($srcImage);
1613
+        }
1614
+        ImageDestroy($destImage);
1615
+    }
1616
+
1617
+    if (!$vignette or !$size = @spip_getimagesize($vignette)) {
1618
+        $size = [$destWidth, $destHeight];
1619
+    }
1620
+
1621
+    // Gaffe: en safe mode, pas d'acces a la vignette,
1622
+    // donc risque de balancer "width='0'", ce qui masque l'image sous MSIE
1623
+    if ($size[0] < 1) {
1624
+        $size[0] = $destWidth;
1625
+    }
1626
+    if ($size[1] < 1) {
1627
+        $size[1] = $destHeight;
1628
+    }
1629
+
1630
+    $retour['width'] = $largeur = $size[0];
1631
+    $retour['height'] = $hauteur = $size[1];
1632
+
1633
+    $retour['fichier'] = $vignette;
1634
+    $retour['format'] = $format;
1635
+    $retour['date'] = @filemtime($vignette);
1636
+
1637
+    // renvoyer l'image
1638
+    return $retour;
1639 1639
 }
1640 1640
 
1641 1641
 /**
@@ -1655,25 +1655,25 @@  discard block
 block discarded – undo
1655 1655
  * @return array Liste [ largeur, hauteur, ratio de réduction ]
1656 1656
  **/
1657 1657
 function _image_ratio(int $srcWidth, int $srcHeight, int $maxWidth, int $maxHeight): array {
1658
-	$ratioWidth = $srcWidth / $maxWidth;
1659
-	$ratioHeight = $srcHeight / $maxHeight;
1660
-
1661
-	if ($srcWidth <= $maxWidth and $srcHeight <= $maxHeight) {
1662
-		$destWidth = $srcWidth;
1663
-		$destHeight = $srcHeight;
1664
-	} elseif ($ratioWidth < $ratioHeight) {
1665
-		$destWidth = $srcWidth / $ratioHeight;
1666
-		$destHeight = $maxHeight;
1667
-	} else {
1668
-		$destWidth = $maxWidth;
1669
-		$destHeight = $srcHeight / $ratioWidth;
1670
-	}
1671
-
1672
-	return [
1673
-		intval(round($destWidth)),
1674
-		intval(round($destHeight)),
1675
-		max($ratioWidth, $ratioHeight)
1676
-	];
1658
+    $ratioWidth = $srcWidth / $maxWidth;
1659
+    $ratioHeight = $srcHeight / $maxHeight;
1660
+
1661
+    if ($srcWidth <= $maxWidth and $srcHeight <= $maxHeight) {
1662
+        $destWidth = $srcWidth;
1663
+        $destHeight = $srcHeight;
1664
+    } elseif ($ratioWidth < $ratioHeight) {
1665
+        $destWidth = $srcWidth / $ratioHeight;
1666
+        $destHeight = $maxHeight;
1667
+    } else {
1668
+        $destWidth = $maxWidth;
1669
+        $destHeight = $srcHeight / $ratioWidth;
1670
+    }
1671
+
1672
+    return [
1673
+        intval(round($destWidth)),
1674
+        intval(round($destHeight)),
1675
+        max($ratioWidth, $ratioHeight)
1676
+    ];
1677 1677
 }
1678 1678
 
1679 1679
 /**
@@ -1693,25 +1693,25 @@  discard block
 block discarded – undo
1693 1693
  * @return array Liste [ largeur, hauteur, ratio de réduction ]
1694 1694
  **/
1695 1695
 function ratio_passe_partout(int $srcWidth, int $srcHeight, int $maxWidth, int $maxHeight): array {
1696
-	$ratioWidth = $srcWidth / $maxWidth;
1697
-	$ratioHeight = $srcHeight / $maxHeight;
1698
-
1699
-	if ($srcWidth <= $maxWidth and $srcHeight <= $maxHeight) {
1700
-		$destWidth = $srcWidth;
1701
-		$destHeight = $srcHeight;
1702
-	} elseif ($ratioWidth > $ratioHeight) {
1703
-		$destWidth = $srcWidth / $ratioHeight;
1704
-		$destHeight = $maxHeight;
1705
-	} else {
1706
-		$destWidth = $maxWidth;
1707
-		$destHeight = $srcHeight / $ratioWidth;
1708
-	}
1709
-
1710
-	return [
1711
-		intval(round($destWidth)),
1712
-		intval(round($destHeight)),
1713
-		min($ratioWidth, $ratioHeight)
1714
-	];
1696
+    $ratioWidth = $srcWidth / $maxWidth;
1697
+    $ratioHeight = $srcHeight / $maxHeight;
1698
+
1699
+    if ($srcWidth <= $maxWidth and $srcHeight <= $maxHeight) {
1700
+        $destWidth = $srcWidth;
1701
+        $destHeight = $srcHeight;
1702
+    } elseif ($ratioWidth > $ratioHeight) {
1703
+        $destWidth = $srcWidth / $ratioHeight;
1704
+        $destHeight = $maxHeight;
1705
+    } else {
1706
+        $destWidth = $maxWidth;
1707
+        $destHeight = $srcHeight / $ratioWidth;
1708
+    }
1709
+
1710
+    return [
1711
+        intval(round($destWidth)),
1712
+        intval(round($destHeight)),
1713
+        min($ratioWidth, $ratioHeight)
1714
+    ];
1715 1715
 }
1716 1716
 
1717 1717
 
@@ -1724,12 +1724,12 @@  discard block
 block discarded – undo
1724 1724
  * @return string
1725 1725
  */
1726 1726
 function process_image_svg_identite($image) {
1727
-	if ($image['creer']) {
1728
-		$source = $image['fichier'];
1729
-		_image_gd_output($source, $image);
1730
-	}
1727
+    if ($image['creer']) {
1728
+        $source = $image['fichier'];
1729
+        _image_gd_output($source, $image);
1730
+    }
1731 1731
 
1732
-	return _image_ecrire_tag($image, ['src' => $image['fichier_dest']]);
1732
+    return _image_ecrire_tag($image, ['src' => $image['fichier_dest']]);
1733 1733
 }
1734 1734
 
1735 1735
 
@@ -1762,111 +1762,111 @@  discard block
 block discarded – undo
1762 1762
  *     Code HTML de la balise img produite
1763 1763
  **/
1764 1764
 function process_image_reduire($fonction, $img, $taille, $taille_y, $force, $process = 'AUTO') {
1765
-	$image = false;
1766
-	if (($process == 'AUTO') and isset($GLOBALS['meta']['image_process'])) {
1767
-		$process = $GLOBALS['meta']['image_process'];
1768
-	}
1769
-	# determiner le format de sortie
1770
-	$format_sortie = false; // le choix par defaut sera bon
1771
-	if ($process == 'netpbm') {
1772
-		$format_sortie = 'jpg';
1773
-	} elseif ($process == 'gd1' or $process == 'gd2') {
1774
-		$image = _image_valeurs_trans($img, "reduire-{$taille}-{$taille_y}", $format_sortie, $fonction, false, _SVG_SUPPORTED);
1775
-		// on verifie que l'extension choisie est bonne (en principe oui)
1776
-		$gd_formats = formats_image_acceptables(true);
1777
-		if (
1778
-			is_array($image)
1779
-			and (!in_array($image['format_dest'], $gd_formats)
1780
-				or (!in_array($image['format_dest'], _image_extensions_acceptees_en_sortie()))
1781
-			)
1782
-		) {
1783
-			if ($image['format_source'] == 'jpg') {
1784
-				$formats_sortie = ['jpg', 'png', 'gif'];
1785
-			} else // les gif sont passes en png preferentiellement pour etre homogene aux autres filtres images
1786
-			{
1787
-				$formats_sortie = ['png', 'jpg', 'gif'];
1788
-			}
1789
-			// Choisir le format destination
1790
-			// - on sauve de preference en JPEG (meilleure compression)
1791
-			// - pour le GIF : les GD recentes peuvent le lire mais pas l'ecrire
1792
-			# bug : gd_formats contient la liste des fichiers qu'on sait *lire*,
1793
-			# pas *ecrire*
1794
-			$format_sortie = '';
1795
-			foreach ($formats_sortie as $fmt) {
1796
-				if (in_array($fmt, $gd_formats) and in_array($fmt, _image_extensions_acceptees_en_sortie())) {
1797
-					$format_sortie = $fmt;
1798
-					break;
1799
-				}
1800
-			}
1801
-			$image = false;
1802
-		}
1803
-	}
1804
-
1805
-	if (!is_array($image)) {
1806
-		$image = _image_valeurs_trans($img, "reduire-{$taille}-{$taille_y}", $format_sortie, $fonction, false, _SVG_SUPPORTED);
1807
-	}
1808
-
1809
-	if (!is_array($image) or !$image['largeur'] or !$image['hauteur']) {
1810
-		spip_log("image_reduire_src:pas de version locale de $img");
1811
-		// on peut resizer en mode html si on dispose des elements
1812
-		if (
1813
-			$srcw = extraire_attribut($img, 'width')
1814
-			and $srch = extraire_attribut($img, 'height')
1815
-		) {
1816
-			[$w, $h] = _image_ratio($srcw, $srch, $taille, $taille_y);
1817
-
1818
-			return _image_tag_changer_taille($img, $w, $h);
1819
-		}
1820
-		// la on n'a pas d'infos sur l'image source... on refile le truc a css
1821
-		// sous la forme style='max-width: NNpx;'
1822
-		return inserer_attribut(
1823
-			$img,
1824
-			'style',
1825
-			"max-width: ${taille}px; max-height: ${taille_y}px"
1826
-		);
1827
-	}
1828
-
1829
-	// si l'image est plus petite que la cible retourner une copie cachee de l'image
1830
-	if (($image['largeur'] <= $taille) && ($image['hauteur'] <= $taille_y)) {
1831
-		if ($image['creer']) {
1832
-			@copy($image['fichier'], $image['fichier_dest']);
1833
-		}
1834
-
1835
-		return _image_ecrire_tag($image, ['src' => $image['fichier_dest']]);
1836
-	}
1837
-
1838
-	if ($image['creer'] == false && !$force) {
1839
-		return _image_ecrire_tag(
1840
-			$image,
1841
-			['src' => $image['fichier_dest'], 'width' => $image['largeur_dest'], 'height' => $image['hauteur_dest']]
1842
-		);
1843
-	}
1844
-
1845
-	if (in_array($image['format_source'], _image_extensions_acceptees_en_entree())) {
1846
-		$destWidth = $image['largeur_dest'];
1847
-		$destHeight = $image['hauteur_dest'];
1848
-		$logo = $image['fichier'];
1849
-		$date = $image['date_src'];
1850
-		$preview = _image_creer_vignette($image, $taille, $taille_y, $process, $force);
1851
-
1852
-		if ($preview && $preview['fichier']) {
1853
-			$logo = $preview['fichier'];
1854
-			$destWidth = $preview['width'];
1855
-			$destHeight = $preview['height'];
1856
-			$date = $preview['date'];
1857
-		}
1858
-		// dans l'espace prive mettre un timestamp sur l'adresse
1859
-		// de l'image, de facon a tromper le cache du navigateur
1860
-		// quand on fait supprimer/reuploader un logo
1861
-		// (pas de filemtime si SAFE MODE)
1862
-		$date = test_espace_prive() ? ('?' . $date) : '';
1863
-
1864
-		return _image_ecrire_tag($image, ['src' => "$logo$date", 'width' => $destWidth, 'height' => $destHeight]);
1865
-	}
1866
-	else {
1867
-		# BMP, tiff ... les redacteurs osent tout!
1868
-		return $img;
1869
-	}
1765
+    $image = false;
1766
+    if (($process == 'AUTO') and isset($GLOBALS['meta']['image_process'])) {
1767
+        $process = $GLOBALS['meta']['image_process'];
1768
+    }
1769
+    # determiner le format de sortie
1770
+    $format_sortie = false; // le choix par defaut sera bon
1771
+    if ($process == 'netpbm') {
1772
+        $format_sortie = 'jpg';
1773
+    } elseif ($process == 'gd1' or $process == 'gd2') {
1774
+        $image = _image_valeurs_trans($img, "reduire-{$taille}-{$taille_y}", $format_sortie, $fonction, false, _SVG_SUPPORTED);
1775
+        // on verifie que l'extension choisie est bonne (en principe oui)
1776
+        $gd_formats = formats_image_acceptables(true);
1777
+        if (
1778
+            is_array($image)
1779
+            and (!in_array($image['format_dest'], $gd_formats)
1780
+                or (!in_array($image['format_dest'], _image_extensions_acceptees_en_sortie()))
1781
+            )
1782
+        ) {
1783
+            if ($image['format_source'] == 'jpg') {
1784
+                $formats_sortie = ['jpg', 'png', 'gif'];
1785
+            } else // les gif sont passes en png preferentiellement pour etre homogene aux autres filtres images
1786
+            {
1787
+                $formats_sortie = ['png', 'jpg', 'gif'];
1788
+            }
1789
+            // Choisir le format destination
1790
+            // - on sauve de preference en JPEG (meilleure compression)
1791
+            // - pour le GIF : les GD recentes peuvent le lire mais pas l'ecrire
1792
+            # bug : gd_formats contient la liste des fichiers qu'on sait *lire*,
1793
+            # pas *ecrire*
1794
+            $format_sortie = '';
1795
+            foreach ($formats_sortie as $fmt) {
1796
+                if (in_array($fmt, $gd_formats) and in_array($fmt, _image_extensions_acceptees_en_sortie())) {
1797
+                    $format_sortie = $fmt;
1798
+                    break;
1799
+                }
1800
+            }
1801
+            $image = false;
1802
+        }
1803
+    }
1804
+
1805
+    if (!is_array($image)) {
1806
+        $image = _image_valeurs_trans($img, "reduire-{$taille}-{$taille_y}", $format_sortie, $fonction, false, _SVG_SUPPORTED);
1807
+    }
1808
+
1809
+    if (!is_array($image) or !$image['largeur'] or !$image['hauteur']) {
1810
+        spip_log("image_reduire_src:pas de version locale de $img");
1811
+        // on peut resizer en mode html si on dispose des elements
1812
+        if (
1813
+            $srcw = extraire_attribut($img, 'width')
1814
+            and $srch = extraire_attribut($img, 'height')
1815
+        ) {
1816
+            [$w, $h] = _image_ratio($srcw, $srch, $taille, $taille_y);
1817
+
1818
+            return _image_tag_changer_taille($img, $w, $h);
1819
+        }
1820
+        // la on n'a pas d'infos sur l'image source... on refile le truc a css
1821
+        // sous la forme style='max-width: NNpx;'
1822
+        return inserer_attribut(
1823
+            $img,
1824
+            'style',
1825
+            "max-width: ${taille}px; max-height: ${taille_y}px"
1826
+        );
1827
+    }
1828
+
1829
+    // si l'image est plus petite que la cible retourner une copie cachee de l'image
1830
+    if (($image['largeur'] <= $taille) && ($image['hauteur'] <= $taille_y)) {
1831
+        if ($image['creer']) {
1832
+            @copy($image['fichier'], $image['fichier_dest']);
1833
+        }
1834
+
1835
+        return _image_ecrire_tag($image, ['src' => $image['fichier_dest']]);
1836
+    }
1837
+
1838
+    if ($image['creer'] == false && !$force) {
1839
+        return _image_ecrire_tag(
1840
+            $image,
1841
+            ['src' => $image['fichier_dest'], 'width' => $image['largeur_dest'], 'height' => $image['hauteur_dest']]
1842
+        );
1843
+    }
1844
+
1845
+    if (in_array($image['format_source'], _image_extensions_acceptees_en_entree())) {
1846
+        $destWidth = $image['largeur_dest'];
1847
+        $destHeight = $image['hauteur_dest'];
1848
+        $logo = $image['fichier'];
1849
+        $date = $image['date_src'];
1850
+        $preview = _image_creer_vignette($image, $taille, $taille_y, $process, $force);
1851
+
1852
+        if ($preview && $preview['fichier']) {
1853
+            $logo = $preview['fichier'];
1854
+            $destWidth = $preview['width'];
1855
+            $destHeight = $preview['height'];
1856
+            $date = $preview['date'];
1857
+        }
1858
+        // dans l'espace prive mettre un timestamp sur l'adresse
1859
+        // de l'image, de facon a tromper le cache du navigateur
1860
+        // quand on fait supprimer/reuploader un logo
1861
+        // (pas de filemtime si SAFE MODE)
1862
+        $date = test_espace_prive() ? ('?' . $date) : '';
1863
+
1864
+        return _image_ecrire_tag($image, ['src' => "$logo$date", 'width' => $destWidth, 'height' => $destHeight]);
1865
+    }
1866
+    else {
1867
+        # BMP, tiff ... les redacteurs osent tout!
1868
+        return $img;
1869
+    }
1870 1870
 }
1871 1871
 
1872 1872
 /**
@@ -1881,145 +1881,145 @@  discard block
 block discarded – undo
1881 1881
  */
1882 1882
 class phpthumb_functions {
1883 1883
 
1884
-	/**
1885
-	 * Retourne la couleur d'un pixel dans une image
1886
-	 *
1887
-	 * @param resource|GdImage $img
1888
-	 * @param int $x
1889
-	 * @param int $y
1890
-	 * @return array|bool
1891
-	 */
1892
-	public static function GetPixelColor(&$img, $x, $y) {
1893
-		if (is_resource($img) || (is_object($img) && $img instanceof \GdImage)) {
1894
-			return @ImageColorsForIndex($img, @ImageColorAt($img, $x, $y));
1895
-		}
1896
-		return false;
1897
-	}
1898
-
1899
-	/**
1900
-	 * Retourne un nombre dans une représentation en Little Endian
1901
-	 *
1902
-	 * @param int $number
1903
-	 * @param int $minbytes
1904
-	 * @return string
1905
-	 */
1906
-	public static function LittleEndian2String($number, $minbytes = 1) {
1907
-		$intstring = '';
1908
-		while ($number > 0) {
1909
-			$intstring = $intstring . chr($number & 255);
1910
-			$number >>= 8;
1911
-		}
1912
-
1913
-		return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
1914
-	}
1915
-
1916
-	/**
1917
-	 * Transforme une ressource GD en image au format ICO
1918
-	 *
1919
-	 * @param array $gd_image_array
1920
-	 *     Tableau de ressources d'images GD
1921
-	 * @return string
1922
-	 *     Image au format ICO
1923
-	 */
1924
-	public static function GD2ICOstring(&$gd_image_array) {
1925
-		foreach ($gd_image_array as $key => $gd_image) {
1926
-			$ImageWidths[$key] = ImageSX($gd_image);
1927
-			$ImageHeights[$key] = ImageSY($gd_image);
1928
-			$bpp[$key] = ImageIsTrueColor($gd_image) ? 32 : 24;
1929
-			$totalcolors[$key] = ImageColorsTotal($gd_image);
1930
-
1931
-			$icXOR[$key] = '';
1932
-			for ($y = $ImageHeights[$key] - 1; $y >= 0; $y--) {
1933
-				for ($x = 0; $x < $ImageWidths[$key]; $x++) {
1934
-					$argb = phpthumb_functions::GetPixelColor($gd_image, $x, $y);
1935
-					$a = round(255 * ((127 - $argb['alpha']) / 127));
1936
-					$r = $argb['red'];
1937
-					$g = $argb['green'];
1938
-					$b = $argb['blue'];
1939
-
1940
-					if ($bpp[$key] == 32) {
1941
-						$icXOR[$key] .= chr($b) . chr($g) . chr($r) . chr($a);
1942
-					} elseif ($bpp[$key] == 24) {
1943
-						$icXOR[$key] .= chr($b) . chr($g) . chr($r);
1944
-					}
1945
-
1946
-					if ($a < 128) {
1947
-						@$icANDmask[$key][$y] .= '1';
1948
-					} else {
1949
-						@$icANDmask[$key][$y] .= '0';
1950
-					}
1951
-				}
1952
-				// mask bits are 32-bit aligned per scanline
1953
-				while (strlen($icANDmask[$key][$y]) % 32) {
1954
-					$icANDmask[$key][$y] .= '0';
1955
-				}
1956
-			}
1957
-			$icAND[$key] = '';
1958
-			foreach ($icANDmask[$key] as $y => $scanlinemaskbits) {
1959
-				for ($i = 0; $i < strlen($scanlinemaskbits); $i += 8) {
1960
-					$icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, '0', STR_PAD_LEFT)));
1961
-				}
1962
-			}
1963
-		}
1964
-
1965
-		foreach ($gd_image_array as $key => $gd_image) {
1966
-			$biSizeImage = $ImageWidths[$key] * $ImageHeights[$key] * ($bpp[$key] / 8);
1967
-
1968
-			// BITMAPINFOHEADER - 40 bytes
1969
-			$BitmapInfoHeader[$key] = '';
1970
-			$BitmapInfoHeader[$key] .= "\x28\x00\x00\x00";                // DWORD  biSize;
1971
-			$BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageWidths[$key], 4);    // LONG   biWidth;
1972
-			// The biHeight member specifies the combined
1973
-			// height of the XOR and AND masks.
1974
-			$BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageHeights[$key] * 2, 4); // LONG   biHeight;
1975
-			$BitmapInfoHeader[$key] .= "\x01\x00";                    // WORD   biPlanes;
1976
-			$BitmapInfoHeader[$key] .= chr($bpp[$key]) . "\x00";              // wBitCount;
1977
-			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // DWORD  biCompression;
1978
-			$BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($biSizeImage, 4);      // DWORD  biSizeImage;
1979
-			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // LONG   biXPelsPerMeter;
1980
-			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // LONG   biYPelsPerMeter;
1981
-			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // DWORD  biClrUsed;
1982
-			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // DWORD  biClrImportant;
1983
-		}
1984
-
1985
-
1986
-		$icondata = "\x00\x00";                    // idReserved;   // Reserved (must be 0)
1987
-		$icondata .= "\x01\x00";                    // idType;	   // Resource Type (1 for icons)
1988
-		$icondata .= phpthumb_functions::LittleEndian2String(count($gd_image_array), 2);  // idCount;	  // How many images?
1989
-
1990
-		$dwImageOffset = 6 + (count($gd_image_array) * 16);
1991
-		foreach ($gd_image_array as $key => $gd_image) {
1992
-			// ICONDIRENTRY   idEntries[1]; // An entry for each image (idCount of 'em)
1993
-
1994
-			$icondata .= chr($ImageWidths[$key]);           // bWidth;		  // Width, in pixels, of the image
1995
-			$icondata .= chr($ImageHeights[$key]);          // bHeight;		 // Height, in pixels, of the image
1996
-			$icondata .= chr($totalcolors[$key]);           // bColorCount;	 // Number of colors in image (0 if >=8bpp)
1997
-			$icondata .= "\x00";                    // bReserved;	   // Reserved ( must be 0)
1998
-
1999
-			$icondata .= "\x01\x00";                  // wPlanes;		 // Color Planes
2000
-			$icondata .= chr($bpp[$key]) . "\x00";            // wBitCount;	   // Bits per pixel
2001
-
2002
-			$dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]);
2003
-			$icondata .= phpthumb_functions::LittleEndian2String(
2004
-				$dwBytesInRes,
2005
-				4
2006
-			);     // dwBytesInRes;	// How many bytes in this resource?
2007
-
2008
-			$icondata .= phpthumb_functions::LittleEndian2String(
2009
-				$dwImageOffset,
2010
-				4
2011
-			);    // dwImageOffset;   // Where in the file is this image?
2012
-			$dwImageOffset += strlen($BitmapInfoHeader[$key]);
2013
-			$dwImageOffset += strlen($icXOR[$key]);
2014
-			$dwImageOffset += strlen($icAND[$key]);
2015
-		}
2016
-
2017
-		foreach ($gd_image_array as $key => $gd_image) {
2018
-			$icondata .= $BitmapInfoHeader[$key];
2019
-			$icondata .= $icXOR[$key];
2020
-			$icondata .= $icAND[$key];
2021
-		}
2022
-
2023
-		return $icondata;
2024
-	}
1884
+    /**
1885
+     * Retourne la couleur d'un pixel dans une image
1886
+     *
1887
+     * @param resource|GdImage $img
1888
+     * @param int $x
1889
+     * @param int $y
1890
+     * @return array|bool
1891
+     */
1892
+    public static function GetPixelColor(&$img, $x, $y) {
1893
+        if (is_resource($img) || (is_object($img) && $img instanceof \GdImage)) {
1894
+            return @ImageColorsForIndex($img, @ImageColorAt($img, $x, $y));
1895
+        }
1896
+        return false;
1897
+    }
1898
+
1899
+    /**
1900
+     * Retourne un nombre dans une représentation en Little Endian
1901
+     *
1902
+     * @param int $number
1903
+     * @param int $minbytes
1904
+     * @return string
1905
+     */
1906
+    public static function LittleEndian2String($number, $minbytes = 1) {
1907
+        $intstring = '';
1908
+        while ($number > 0) {
1909
+            $intstring = $intstring . chr($number & 255);
1910
+            $number >>= 8;
1911
+        }
1912
+
1913
+        return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
1914
+    }
1915
+
1916
+    /**
1917
+     * Transforme une ressource GD en image au format ICO
1918
+     *
1919
+     * @param array $gd_image_array
1920
+     *     Tableau de ressources d'images GD
1921
+     * @return string
1922
+     *     Image au format ICO
1923
+     */
1924
+    public static function GD2ICOstring(&$gd_image_array) {
1925
+        foreach ($gd_image_array as $key => $gd_image) {
1926
+            $ImageWidths[$key] = ImageSX($gd_image);
1927
+            $ImageHeights[$key] = ImageSY($gd_image);
1928
+            $bpp[$key] = ImageIsTrueColor($gd_image) ? 32 : 24;
1929
+            $totalcolors[$key] = ImageColorsTotal($gd_image);
1930
+
1931
+            $icXOR[$key] = '';
1932
+            for ($y = $ImageHeights[$key] - 1; $y >= 0; $y--) {
1933
+                for ($x = 0; $x < $ImageWidths[$key]; $x++) {
1934
+                    $argb = phpthumb_functions::GetPixelColor($gd_image, $x, $y);
1935
+                    $a = round(255 * ((127 - $argb['alpha']) / 127));
1936
+                    $r = $argb['red'];
1937
+                    $g = $argb['green'];
1938
+                    $b = $argb['blue'];
1939
+
1940
+                    if ($bpp[$key] == 32) {
1941
+                        $icXOR[$key] .= chr($b) . chr($g) . chr($r) . chr($a);
1942
+                    } elseif ($bpp[$key] == 24) {
1943
+                        $icXOR[$key] .= chr($b) . chr($g) . chr($r);
1944
+                    }
1945
+
1946
+                    if ($a < 128) {
1947
+                        @$icANDmask[$key][$y] .= '1';
1948
+                    } else {
1949
+                        @$icANDmask[$key][$y] .= '0';
1950
+                    }
1951
+                }
1952
+                // mask bits are 32-bit aligned per scanline
1953
+                while (strlen($icANDmask[$key][$y]) % 32) {
1954
+                    $icANDmask[$key][$y] .= '0';
1955
+                }
1956
+            }
1957
+            $icAND[$key] = '';
1958
+            foreach ($icANDmask[$key] as $y => $scanlinemaskbits) {
1959
+                for ($i = 0; $i < strlen($scanlinemaskbits); $i += 8) {
1960
+                    $icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, '0', STR_PAD_LEFT)));
1961
+                }
1962
+            }
1963
+        }
1964
+
1965
+        foreach ($gd_image_array as $key => $gd_image) {
1966
+            $biSizeImage = $ImageWidths[$key] * $ImageHeights[$key] * ($bpp[$key] / 8);
1967
+
1968
+            // BITMAPINFOHEADER - 40 bytes
1969
+            $BitmapInfoHeader[$key] = '';
1970
+            $BitmapInfoHeader[$key] .= "\x28\x00\x00\x00";                // DWORD  biSize;
1971
+            $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageWidths[$key], 4);    // LONG   biWidth;
1972
+            // The biHeight member specifies the combined
1973
+            // height of the XOR and AND masks.
1974
+            $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageHeights[$key] * 2, 4); // LONG   biHeight;
1975
+            $BitmapInfoHeader[$key] .= "\x01\x00";                    // WORD   biPlanes;
1976
+            $BitmapInfoHeader[$key] .= chr($bpp[$key]) . "\x00";              // wBitCount;
1977
+            $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // DWORD  biCompression;
1978
+            $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($biSizeImage, 4);      // DWORD  biSizeImage;
1979
+            $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // LONG   biXPelsPerMeter;
1980
+            $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // LONG   biYPelsPerMeter;
1981
+            $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // DWORD  biClrUsed;
1982
+            $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // DWORD  biClrImportant;
1983
+        }
1984
+
1985
+
1986
+        $icondata = "\x00\x00";                    // idReserved;   // Reserved (must be 0)
1987
+        $icondata .= "\x01\x00";                    // idType;	   // Resource Type (1 for icons)
1988
+        $icondata .= phpthumb_functions::LittleEndian2String(count($gd_image_array), 2);  // idCount;	  // How many images?
1989
+
1990
+        $dwImageOffset = 6 + (count($gd_image_array) * 16);
1991
+        foreach ($gd_image_array as $key => $gd_image) {
1992
+            // ICONDIRENTRY   idEntries[1]; // An entry for each image (idCount of 'em)
1993
+
1994
+            $icondata .= chr($ImageWidths[$key]);           // bWidth;		  // Width, in pixels, of the image
1995
+            $icondata .= chr($ImageHeights[$key]);          // bHeight;		 // Height, in pixels, of the image
1996
+            $icondata .= chr($totalcolors[$key]);           // bColorCount;	 // Number of colors in image (0 if >=8bpp)
1997
+            $icondata .= "\x00";                    // bReserved;	   // Reserved ( must be 0)
1998
+
1999
+            $icondata .= "\x01\x00";                  // wPlanes;		 // Color Planes
2000
+            $icondata .= chr($bpp[$key]) . "\x00";            // wBitCount;	   // Bits per pixel
2001
+
2002
+            $dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]);
2003
+            $icondata .= phpthumb_functions::LittleEndian2String(
2004
+                $dwBytesInRes,
2005
+                4
2006
+            );     // dwBytesInRes;	// How many bytes in this resource?
2007
+
2008
+            $icondata .= phpthumb_functions::LittleEndian2String(
2009
+                $dwImageOffset,
2010
+                4
2011
+            );    // dwImageOffset;   // Where in the file is this image?
2012
+            $dwImageOffset += strlen($BitmapInfoHeader[$key]);
2013
+            $dwImageOffset += strlen($icXOR[$key]);
2014
+            $dwImageOffset += strlen($icAND[$key]);
2015
+        }
2016
+
2017
+        foreach ($gd_image_array as $key => $gd_image) {
2018
+            $icondata .= $BitmapInfoHeader[$key];
2019
+            $icondata .= $icXOR[$key];
2020
+            $icondata .= $icAND[$key];
2021
+        }
2022
+
2023
+        return $icondata;
2024
+    }
2025 2025
 }
Please login to merge, or discard this patch.