Completed
Push — master ( dec8bc...81ee9f )
by cam
01:38
created
ecrire/inc/filtres_images_lib_mini.php 3 patches
Indentation   +1339 added lines, -1339 removed lines patch added patch discarded remove patch
@@ -17,7 +17,7 @@  discard block
 block discarded – undo
17 17
  */
18 18
 
19 19
 if (!defined('_ECRIRE_INC_VERSION')) {
20
-	return;
20
+    return;
21 21
 }
22 22
 include_spip('inc/filtres'); // par precaution
23 23
 include_spip('inc/filtres_images_mini'); // par precaution
@@ -37,21 +37,21 @@  discard block
 block discarded – undo
37 37
  *     Le code de la couleur en hexadécimal.
38 38
  */
39 39
 function _couleur_dec_to_hex($red, $green, $blue) {
40
-	$red = dechex($red);
41
-	$green = dechex($green);
42
-	$blue = dechex($blue);
43
-
44
-	if (strlen($red) == 1) {
45
-		$red = '0' . $red;
46
-	}
47
-	if (strlen($green) == 1) {
48
-		$green = '0' . $green;
49
-	}
50
-	if (strlen($blue) == 1) {
51
-		$blue = '0' . $blue;
52
-	}
53
-
54
-	return "$red$green$blue";
40
+    $red = dechex($red);
41
+    $green = dechex($green);
42
+    $blue = dechex($blue);
43
+
44
+    if (strlen($red) == 1) {
45
+        $red = '0' . $red;
46
+    }
47
+    if (strlen($green) == 1) {
48
+        $green = '0' . $green;
49
+    }
50
+    if (strlen($blue) == 1) {
51
+        $blue = '0' . $blue;
52
+    }
53
+
54
+    return "$red$green$blue";
55 55
 }
56 56
 
57 57
 /**
@@ -63,18 +63,18 @@  discard block
 block discarded – undo
63 63
  *     Un tableau des 3 éléments : rouge, vert, bleu.
64 64
  */
65 65
 function _couleur_hex_to_dec($couleur) {
66
-	$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;
66
+    $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;
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
 /**
@@ -251,11 +251,11 @@  discard block
 block discarded – undo
251 251
  *     true si il faut supprimer le fichier temporaire ; false sinon.
252 252
  */
253 253
 function statut_effacer_images_temporaires($stat) {
254
-	static $statut = false; // par defaut on grave toute les images
255
-	if ($stat === 'get') {
256
-		return $statut;
257
-	}
258
-	$statut = $stat ? true : false;
254
+    static $statut = false; // par defaut on grave toute les images
255
+    if ($stat === 'get') {
256
+        return $statut;
257
+    }
258
+    $statut = $stat ? true : false;
259 259
 }
260 260
 
261 261
 
@@ -308,253 +308,253 @@  discard block
 block discarded – undo
308 308
  *     - array : tableau décrivant de l'image
309 309
  */
310 310
 function _image_valeurs_trans($img, $effet, $forcer_format = false, $fonction_creation = null, $find_in_path = false, $support_svg = false) {
311
-	$ret = [];
312
-	$f = null;
313
-	static $images_recalcul = [];
314
-	if (strlen($img) == 0) {
315
-		return false;
316
-	}
317
-
318
-	$source = trim(extraire_attribut($img, 'src') ?? '');
319
-	if (strlen($source) < 1) {
320
-		if (strpos($img, '<img ') !== 0) {
321
-			$source = $img;
322
-			$img = "<img src='$source' />";
323
-		}
324
-		else {
325
-			// pas d'attribut src sur cette balise <img../>
326
-			return false;
327
-		}
328
-	} elseif (
329
-		preg_match('@^data:image/([^;]*);base64,(.*)$@isS', $source, $regs)
330
-		and $extension = _image_trouver_extension_depuis_mime('image/' . $regs[1])
331
-		and in_array($extension, _image_extensions_acceptees_en_entree())
332
-	) {
333
-		# gerer img src="data:....base64"
334
-		$local = sous_repertoire(_DIR_VAR, 'image-data') . md5($regs[2]) . '.' . _image_extension_normalisee($extension);
335
-		if (!file_exists($local)) {
336
-			ecrire_fichier($local, base64_decode($regs[2]));
337
-		}
338
-		if ($sanitizer = charger_fonction($extension, 'sanitizer', true)) {
339
-			$sanitizer($local);
340
-		}
341
-		$source = $local;
342
-		$img = inserer_attribut($img, 'src', $source);
343
-		# eviter les mauvaises surprises lors de conversions de format
344
-		$img = inserer_attribut($img, 'width', '');
345
-		$img = inserer_attribut($img, 'height', '');
346
-	}
347
-
348
-	// les protocoles web prennent au moins 3 lettres
349
-	if (tester_url_absolue($source)) {
350
-		include_spip('inc/distant');
351
-		$fichier = _DIR_RACINE . copie_locale($source);
352
-		if (!$fichier) {
353
-			return '';
354
-		}
355
-		if (
356
-			$extension = _image_trouver_extension($fichier)
357
-			and $sanitizer = charger_fonction($extension, 'sanitizer', true)
358
-		) {
359
-			$sanitizer($fichier);
360
-		}
361
-	} else {
362
-		// enlever le timestamp eventuel
363
-		if (strpos($source, '?') !== false) {
364
-			$source = preg_replace(',[?][0-9]+$,', '', $source);
365
-		}
366
-		if (
367
-			strpos($source, '?') !== false
368
-			and strncmp($source, _DIR_IMG, strlen(_DIR_IMG)) == 0
369
-			and file_exists($f = preg_replace(',[?].*$,', '', $source))
370
-		) {
371
-			$source = $f;
372
-		}
373
-		$fichier = $source;
374
-	}
375
-
376
-	$terminaison_dest = '';
377
-	if ($terminaison = _image_trouver_extension($fichier)) {
378
-		$terminaison_dest = ($terminaison == 'gif') ? 'png' : $terminaison;
379
-	}
380
-
381
-	if (
382
-		$forcer_format !== false
383
-		// 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
384
-		and ($terminaison_dest !== 'svg' or $support_svg or !in_array($forcer_format, _image_extensions_acceptees_en_sortie()))
385
-	) {
386
-		$terminaison_dest = $forcer_format;
387
-	}
388
-
389
-	if (!$terminaison_dest) {
390
-		return false;
391
-	}
392
-
393
-	$nom_fichier = substr($fichier, 0, strlen($fichier) - (strlen($terminaison) + 1));
394
-	$fichier_dest = $nom_fichier;
395
-	if (
396
-		($find_in_path and $f = find_in_path($fichier) and $fichier = $f)
397
-		or @file_exists($f = $fichier)
398
-	) {
399
-		// on ne passe jamais la balise img qui est peut-être en x2 et à laquelle on ne peut pas faire confiance
400
-		// on lit directement les infos du fichier
401
-		[$ret['hauteur'], $ret['largeur']] = taille_image($f);
402
-		$date_src = @filemtime($f);
403
-	} elseif (
404
-		@file_exists($f = "$fichier.src")
405
-		and lire_fichier($f, $valeurs)
406
-		and $valeurs = unserialize($valeurs)
407
-		and isset($valeurs['hauteur_dest'])
408
-		and isset($valeurs['largeur_dest'])
409
-	) {
410
-		$ret['hauteur'] = $valeurs['hauteur_dest'];
411
-		$ret['largeur'] = $valeurs['largeur_dest'];
412
-		$date_src = $valeurs['date'];
413
-	} // pas de fichier source par la
414
-	else {
415
-		return false;
416
-	}
417
-
418
-	// pas de taille mesurable ?
419
-	if (!$ret['hauteur']
420
-		or !($ret['hauteur'] = intval(round($ret['hauteur'])))
421
-		or !$ret['largeur']
422
-		or !($ret['largeur'] = intval(round($ret['largeur'])))
423
-	) {
424
-		return false;
425
-	}
426
-
427
-	// les images calculees dependent du chemin du fichier source
428
-	// 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
429
-	// ce n'est pas totalement optimal en terme de stockage, mais chaque image est associee a un fichier .src
430
-	// qui contient la methode de reconstrucion (le filtre + les arguments d'appel) et les arguments different entre prive et public
431
-	// la mise en commun du fichier image cree donc un bug et des problemes qui necessiteraient beaucoup de complexite de code
432
-	// alors que ca concerne peu de site au final
433
-	// la release de r23632+r23633+r23634 a provoque peu de remontee de bug attestant du peu de sites impactes
434
-	$identifiant = $fichier;
435
-
436
-	// cas general :
437
-	// on a un dossier cache commun et un nom de fichier qui varie avec l'effet
438
-	// cas particulier de reduire :
439
-	// un cache par dimension, et le nom de fichier est conserve, suffixe par la dimension aussi
440
-	$cache = 'cache-gd2';
441
-	if (substr($effet, 0, 7) == 'reduire') {
442
-		[, $maxWidth, $maxHeight] = explode('-', $effet);
443
-		[$destWidth, $destHeight] = _image_ratio($ret['largeur'], $ret['hauteur'], $maxWidth, $maxHeight);
444
-		$ret['largeur_dest'] = $destWidth;
445
-		$ret['hauteur_dest'] = $destHeight;
446
-		$effet = "L{$destWidth}xH$destHeight";
447
-		$cache = 'cache-vignettes';
448
-		$fichier_dest = basename($fichier_dest);
449
-		if (($ret['largeur'] <= $maxWidth) && ($ret['hauteur'] <= $maxHeight)) {
450
-			// on garde la terminaison initiale car image simplement copiee
451
-			// et on postfixe son nom avec un md5 du path
452
-			$terminaison_dest = $terminaison;
453
-			$fichier_dest .= '-' . substr(md5("$identifiant"), 0, 5);
454
-		} else {
455
-			$fichier_dest .= '-' . substr(md5("$identifiant-$effet"), 0, 5);
456
-		}
457
-		$cache = sous_repertoire(_DIR_VAR, $cache);
458
-		$cache = sous_repertoire($cache, $effet);
459
-	} else {
460
-		$fichier_dest = md5("$identifiant-$effet");
461
-		$cache = sous_repertoire(_DIR_VAR, $cache);
462
-		$cache = sous_repertoire($cache, substr($fichier_dest, 0, 2));
463
-		$fichier_dest = substr($fichier_dest, 2);
464
-	}
465
-
466
-	$fichier_dest = $cache . $fichier_dest . '.' . $terminaison_dest;
467
-
468
-	$GLOBALS['images_calculees'][] = $fichier_dest;
469
-
470
-	$creer = true;
471
-	// si recalcul des images demande, recalculer chaque image une fois
472
-	if (defined('_VAR_IMAGES') and _VAR_IMAGES and !isset($images_recalcul[$fichier_dest])) {
473
-		$images_recalcul[$fichier_dest] = true;
474
-	} else {
475
-		if (@file_exists($f = $fichier_dest)) {
476
-			if (filemtime($f) >= $date_src) {
477
-				$creer = false;
478
-			}
479
-		} else {
480
-			if (
481
-				@file_exists($f = "$fichier_dest.src")
482
-				and lire_fichier($f, $valeurs)
483
-				and $valeurs = unserialize($valeurs)
484
-				and $valeurs['date'] >= $date_src
485
-			) {
486
-				$creer = false;
487
-			}
488
-		}
489
-	}
490
-	if ($creer) {
491
-		if (!@file_exists($fichier)) {
492
-			if (!@file_exists("$fichier.src")) {
493
-				spip_log("Image absente : $fichier", 'images' . _LOG_ERREUR);
494
-
495
-				return false;
496
-			}
497
-			# on reconstruit l'image source absente a partir de la chaine des .src
498
-			reconstruire_image_intermediaire($fichier);
499
-		}
500
-	}
501
-
502
-	if ($creer) {
503
-		spip_log(
504
-			'filtre image ' . ($fonction_creation ? reset($fonction_creation) : '') . "[$effet] sur $fichier",
505
-			'images' . _LOG_DEBUG
506
-		);
507
-	}
508
-
509
-	$term_fonction = _image_trouver_extension_pertinente($fichier);
510
-	$ret['fonction_imagecreatefrom'] = '_imagecreatefrom' . $term_fonction;
511
-	$ret['fichier'] = $fichier;
512
-	$ret['fonction_image'] = '_image_image' . $terminaison_dest;
513
-	$ret['fichier_dest'] = $fichier_dest;
514
-	$ret['format_source'] = _image_extension_normalisee($terminaison);
515
-	$ret['format_dest'] = $terminaison_dest;
516
-	$ret['date_src'] = $date_src;
517
-	$ret['creer'] = $creer;
518
-	$ret['class'] = extraire_attribut($img, 'class');
519
-	$ret['alt'] = extraire_attribut($img, 'alt');
520
-	$ret['style'] = extraire_attribut($img, 'style');
521
-	$ret['tag'] = $img;
522
-	if ($fonction_creation) {
523
-		$ret['reconstruction'] = $fonction_creation;
524
-		# ecrire ici comment creer le fichier, car il est pas sur qu'on l'ecrira reelement
525
-		# cas de image_reduire qui finalement ne reduit pas l'image source
526
-		# ca evite d'essayer de le creer au prochain hit si il n'est pas la
527
-		#ecrire_fichier($ret['fichier_dest'].'.src',serialize($ret),true);
528
-	}
529
-
530
-	$ret = pipeline('image_preparer_filtre', [
531
-			'args' => [
532
-				'img' => $img,
533
-				'effet' => $effet,
534
-				'forcer_format' => $forcer_format,
535
-				'fonction_creation' => $fonction_creation,
536
-				'find_in_path' => $find_in_path,
537
-			],
538
-			'data' => $ret
539
-		]);
540
-
541
-	// une globale pour le debug en cas de crash memoire
542
-	$GLOBALS['derniere_image_calculee'] = $ret;
543
-
544
-	// traiter le cas particulier des SVG : si le filtre n'a pas annonce explicitement qu'il savait faire, on delegue
545
-	if ($term_fonction === 'svg') {
546
-		if ($creer and !$support_svg) {
547
-			process_image_svg_identite($ret);
548
-			$ret['creer'] = false;
549
-		}
550
-	}
551
-	else {
552
-		if (!function_exists($ret['fonction_imagecreatefrom'])) {
553
-			return false;
554
-		}
555
-	}
556
-
557
-	return $ret;
311
+    $ret = [];
312
+    $f = null;
313
+    static $images_recalcul = [];
314
+    if (strlen($img) == 0) {
315
+        return false;
316
+    }
317
+
318
+    $source = trim(extraire_attribut($img, 'src') ?? '');
319
+    if (strlen($source) < 1) {
320
+        if (strpos($img, '<img ') !== 0) {
321
+            $source = $img;
322
+            $img = "<img src='$source' />";
323
+        }
324
+        else {
325
+            // pas d'attribut src sur cette balise <img../>
326
+            return false;
327
+        }
328
+    } elseif (
329
+        preg_match('@^data:image/([^;]*);base64,(.*)$@isS', $source, $regs)
330
+        and $extension = _image_trouver_extension_depuis_mime('image/' . $regs[1])
331
+        and in_array($extension, _image_extensions_acceptees_en_entree())
332
+    ) {
333
+        # gerer img src="data:....base64"
334
+        $local = sous_repertoire(_DIR_VAR, 'image-data') . md5($regs[2]) . '.' . _image_extension_normalisee($extension);
335
+        if (!file_exists($local)) {
336
+            ecrire_fichier($local, base64_decode($regs[2]));
337
+        }
338
+        if ($sanitizer = charger_fonction($extension, 'sanitizer', true)) {
339
+            $sanitizer($local);
340
+        }
341
+        $source = $local;
342
+        $img = inserer_attribut($img, 'src', $source);
343
+        # eviter les mauvaises surprises lors de conversions de format
344
+        $img = inserer_attribut($img, 'width', '');
345
+        $img = inserer_attribut($img, 'height', '');
346
+    }
347
+
348
+    // les protocoles web prennent au moins 3 lettres
349
+    if (tester_url_absolue($source)) {
350
+        include_spip('inc/distant');
351
+        $fichier = _DIR_RACINE . copie_locale($source);
352
+        if (!$fichier) {
353
+            return '';
354
+        }
355
+        if (
356
+            $extension = _image_trouver_extension($fichier)
357
+            and $sanitizer = charger_fonction($extension, 'sanitizer', true)
358
+        ) {
359
+            $sanitizer($fichier);
360
+        }
361
+    } else {
362
+        // enlever le timestamp eventuel
363
+        if (strpos($source, '?') !== false) {
364
+            $source = preg_replace(',[?][0-9]+$,', '', $source);
365
+        }
366
+        if (
367
+            strpos($source, '?') !== false
368
+            and strncmp($source, _DIR_IMG, strlen(_DIR_IMG)) == 0
369
+            and file_exists($f = preg_replace(',[?].*$,', '', $source))
370
+        ) {
371
+            $source = $f;
372
+        }
373
+        $fichier = $source;
374
+    }
375
+
376
+    $terminaison_dest = '';
377
+    if ($terminaison = _image_trouver_extension($fichier)) {
378
+        $terminaison_dest = ($terminaison == 'gif') ? 'png' : $terminaison;
379
+    }
380
+
381
+    if (
382
+        $forcer_format !== false
383
+        // 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
384
+        and ($terminaison_dest !== 'svg' or $support_svg or !in_array($forcer_format, _image_extensions_acceptees_en_sortie()))
385
+    ) {
386
+        $terminaison_dest = $forcer_format;
387
+    }
388
+
389
+    if (!$terminaison_dest) {
390
+        return false;
391
+    }
392
+
393
+    $nom_fichier = substr($fichier, 0, strlen($fichier) - (strlen($terminaison) + 1));
394
+    $fichier_dest = $nom_fichier;
395
+    if (
396
+        ($find_in_path and $f = find_in_path($fichier) and $fichier = $f)
397
+        or @file_exists($f = $fichier)
398
+    ) {
399
+        // on ne passe jamais la balise img qui est peut-être en x2 et à laquelle on ne peut pas faire confiance
400
+        // on lit directement les infos du fichier
401
+        [$ret['hauteur'], $ret['largeur']] = taille_image($f);
402
+        $date_src = @filemtime($f);
403
+    } elseif (
404
+        @file_exists($f = "$fichier.src")
405
+        and lire_fichier($f, $valeurs)
406
+        and $valeurs = unserialize($valeurs)
407
+        and isset($valeurs['hauteur_dest'])
408
+        and isset($valeurs['largeur_dest'])
409
+    ) {
410
+        $ret['hauteur'] = $valeurs['hauteur_dest'];
411
+        $ret['largeur'] = $valeurs['largeur_dest'];
412
+        $date_src = $valeurs['date'];
413
+    } // pas de fichier source par la
414
+    else {
415
+        return false;
416
+    }
417
+
418
+    // pas de taille mesurable ?
419
+    if (!$ret['hauteur']
420
+        or !($ret['hauteur'] = intval(round($ret['hauteur'])))
421
+        or !$ret['largeur']
422
+        or !($ret['largeur'] = intval(round($ret['largeur'])))
423
+    ) {
424
+        return false;
425
+    }
426
+
427
+    // les images calculees dependent du chemin du fichier source
428
+    // 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
429
+    // ce n'est pas totalement optimal en terme de stockage, mais chaque image est associee a un fichier .src
430
+    // qui contient la methode de reconstrucion (le filtre + les arguments d'appel) et les arguments different entre prive et public
431
+    // la mise en commun du fichier image cree donc un bug et des problemes qui necessiteraient beaucoup de complexite de code
432
+    // alors que ca concerne peu de site au final
433
+    // la release de r23632+r23633+r23634 a provoque peu de remontee de bug attestant du peu de sites impactes
434
+    $identifiant = $fichier;
435
+
436
+    // cas general :
437
+    // on a un dossier cache commun et un nom de fichier qui varie avec l'effet
438
+    // cas particulier de reduire :
439
+    // un cache par dimension, et le nom de fichier est conserve, suffixe par la dimension aussi
440
+    $cache = 'cache-gd2';
441
+    if (substr($effet, 0, 7) == 'reduire') {
442
+        [, $maxWidth, $maxHeight] = explode('-', $effet);
443
+        [$destWidth, $destHeight] = _image_ratio($ret['largeur'], $ret['hauteur'], $maxWidth, $maxHeight);
444
+        $ret['largeur_dest'] = $destWidth;
445
+        $ret['hauteur_dest'] = $destHeight;
446
+        $effet = "L{$destWidth}xH$destHeight";
447
+        $cache = 'cache-vignettes';
448
+        $fichier_dest = basename($fichier_dest);
449
+        if (($ret['largeur'] <= $maxWidth) && ($ret['hauteur'] <= $maxHeight)) {
450
+            // on garde la terminaison initiale car image simplement copiee
451
+            // et on postfixe son nom avec un md5 du path
452
+            $terminaison_dest = $terminaison;
453
+            $fichier_dest .= '-' . substr(md5("$identifiant"), 0, 5);
454
+        } else {
455
+            $fichier_dest .= '-' . substr(md5("$identifiant-$effet"), 0, 5);
456
+        }
457
+        $cache = sous_repertoire(_DIR_VAR, $cache);
458
+        $cache = sous_repertoire($cache, $effet);
459
+    } else {
460
+        $fichier_dest = md5("$identifiant-$effet");
461
+        $cache = sous_repertoire(_DIR_VAR, $cache);
462
+        $cache = sous_repertoire($cache, substr($fichier_dest, 0, 2));
463
+        $fichier_dest = substr($fichier_dest, 2);
464
+    }
465
+
466
+    $fichier_dest = $cache . $fichier_dest . '.' . $terminaison_dest;
467
+
468
+    $GLOBALS['images_calculees'][] = $fichier_dest;
469
+
470
+    $creer = true;
471
+    // si recalcul des images demande, recalculer chaque image une fois
472
+    if (defined('_VAR_IMAGES') and _VAR_IMAGES and !isset($images_recalcul[$fichier_dest])) {
473
+        $images_recalcul[$fichier_dest] = true;
474
+    } else {
475
+        if (@file_exists($f = $fichier_dest)) {
476
+            if (filemtime($f) >= $date_src) {
477
+                $creer = false;
478
+            }
479
+        } else {
480
+            if (
481
+                @file_exists($f = "$fichier_dest.src")
482
+                and lire_fichier($f, $valeurs)
483
+                and $valeurs = unserialize($valeurs)
484
+                and $valeurs['date'] >= $date_src
485
+            ) {
486
+                $creer = false;
487
+            }
488
+        }
489
+    }
490
+    if ($creer) {
491
+        if (!@file_exists($fichier)) {
492
+            if (!@file_exists("$fichier.src")) {
493
+                spip_log("Image absente : $fichier", 'images' . _LOG_ERREUR);
494
+
495
+                return false;
496
+            }
497
+            # on reconstruit l'image source absente a partir de la chaine des .src
498
+            reconstruire_image_intermediaire($fichier);
499
+        }
500
+    }
501
+
502
+    if ($creer) {
503
+        spip_log(
504
+            'filtre image ' . ($fonction_creation ? reset($fonction_creation) : '') . "[$effet] sur $fichier",
505
+            'images' . _LOG_DEBUG
506
+        );
507
+    }
508
+
509
+    $term_fonction = _image_trouver_extension_pertinente($fichier);
510
+    $ret['fonction_imagecreatefrom'] = '_imagecreatefrom' . $term_fonction;
511
+    $ret['fichier'] = $fichier;
512
+    $ret['fonction_image'] = '_image_image' . $terminaison_dest;
513
+    $ret['fichier_dest'] = $fichier_dest;
514
+    $ret['format_source'] = _image_extension_normalisee($terminaison);
515
+    $ret['format_dest'] = $terminaison_dest;
516
+    $ret['date_src'] = $date_src;
517
+    $ret['creer'] = $creer;
518
+    $ret['class'] = extraire_attribut($img, 'class');
519
+    $ret['alt'] = extraire_attribut($img, 'alt');
520
+    $ret['style'] = extraire_attribut($img, 'style');
521
+    $ret['tag'] = $img;
522
+    if ($fonction_creation) {
523
+        $ret['reconstruction'] = $fonction_creation;
524
+        # ecrire ici comment creer le fichier, car il est pas sur qu'on l'ecrira reelement
525
+        # cas de image_reduire qui finalement ne reduit pas l'image source
526
+        # ca evite d'essayer de le creer au prochain hit si il n'est pas la
527
+        #ecrire_fichier($ret['fichier_dest'].'.src',serialize($ret),true);
528
+    }
529
+
530
+    $ret = pipeline('image_preparer_filtre', [
531
+            'args' => [
532
+                'img' => $img,
533
+                'effet' => $effet,
534
+                'forcer_format' => $forcer_format,
535
+                'fonction_creation' => $fonction_creation,
536
+                'find_in_path' => $find_in_path,
537
+            ],
538
+            'data' => $ret
539
+        ]);
540
+
541
+    // une globale pour le debug en cas de crash memoire
542
+    $GLOBALS['derniere_image_calculee'] = $ret;
543
+
544
+    // traiter le cas particulier des SVG : si le filtre n'a pas annonce explicitement qu'il savait faire, on delegue
545
+    if ($term_fonction === 'svg') {
546
+        if ($creer and !$support_svg) {
547
+            process_image_svg_identite($ret);
548
+            $ret['creer'] = false;
549
+        }
550
+    }
551
+    else {
552
+        if (!function_exists($ret['fonction_imagecreatefrom'])) {
553
+            return false;
554
+        }
555
+    }
556
+
557
+    return $ret;
558 558
 }
559 559
 
560 560
 
@@ -563,54 +563,54 @@  discard block
 block discarded – undo
563 563
  * @return array
564 564
  */
565 565
 function _image_extensions_acceptees_en_entree() {
566
-	static $extensions = null;
567
-	if (empty($extensions)) {
568
-		$extensions = ['png', 'gif', 'jpg', 'jpeg'];
569
-		if (!empty($GLOBALS['meta']['gd_formats'])) {
570
-			// action=tester renseigne gd_formats et detecte le support de webp
571
-			$extensions = array_merge(explode(',', $GLOBALS['meta']['gd_formats']));
572
-			$extensions = array_map('trim', $extensions);
573
-			$extensions = array_filter($extensions);
574
-			if (in_array('jpg', $extensions)) {
575
-				$extensions[] = 'jpeg';
576
-			}
577
-			$extensions = array_unique($extensions);
578
-		}
579
-		$extensions[] = 'svg'; // on le supporte toujours avec des fonctions specifiques
580
-	}
581
-
582
-	return $extensions;
566
+    static $extensions = null;
567
+    if (empty($extensions)) {
568
+        $extensions = ['png', 'gif', 'jpg', 'jpeg'];
569
+        if (!empty($GLOBALS['meta']['gd_formats'])) {
570
+            // action=tester renseigne gd_formats et detecte le support de webp
571
+            $extensions = array_merge(explode(',', $GLOBALS['meta']['gd_formats']));
572
+            $extensions = array_map('trim', $extensions);
573
+            $extensions = array_filter($extensions);
574
+            if (in_array('jpg', $extensions)) {
575
+                $extensions[] = 'jpeg';
576
+            }
577
+            $extensions = array_unique($extensions);
578
+        }
579
+        $extensions[] = 'svg'; // on le supporte toujours avec des fonctions specifiques
580
+    }
581
+
582
+    return $extensions;
583 583
 }
584 584
 
585 585
 /**
586 586
  * @return array|string[]|null
587 587
  */
588 588
 function _image_extensions_acceptees_en_sortie() {
589
-	static $extensions = null;
590
-	if (empty($extensions)) {
591
-		$extensions = _image_extensions_acceptees_en_entree();
592
-		$extensions = array_diff($extensions, ['jpeg']);
593
-		if (in_array('gif', $extensions) and !function_exists('imagegif')) {
594
-			$extensions = array_diff($extensions, ['gif']);
595
-		}
596
-		if (in_array('webp', $extensions) and !function_exists('imagewebp')) {
597
-			$extensions = array_diff($extensions, ['webp']);
598
-		}
599
-	}
600
-
601
-	return $extensions;
589
+    static $extensions = null;
590
+    if (empty($extensions)) {
591
+        $extensions = _image_extensions_acceptees_en_entree();
592
+        $extensions = array_diff($extensions, ['jpeg']);
593
+        if (in_array('gif', $extensions) and !function_exists('imagegif')) {
594
+            $extensions = array_diff($extensions, ['gif']);
595
+        }
596
+        if (in_array('webp', $extensions) and !function_exists('imagewebp')) {
597
+            $extensions = array_diff($extensions, ['webp']);
598
+        }
599
+    }
600
+
601
+    return $extensions;
602 602
 }
603 603
 
604 604
 function _image_extension_normalisee($extension) {
605
-	$extension = strtolower($extension);
606
-	if ($extension === 'jpeg') {
607
-		$extension = 'jpg';
608
-	}
609
-	return $extension;
605
+    $extension = strtolower($extension);
606
+    if ($extension === 'jpeg') {
607
+        $extension = 'jpg';
608
+    }
609
+    return $extension;
610 610
 }
611 611
 
612 612
 function _image_extensions_conservent_transparence() {
613
-	return ['png', 'webp'];
613
+    return ['png', 'webp'];
614 614
 }
615 615
 
616 616
 
@@ -620,12 +620,12 @@  discard block
 block discarded – undo
620 620
  * @return string
621 621
  */
622 622
 function _image_trouver_extension($path) {
623
-	$preg_extensions = implode('|', _image_extensions_acceptees_en_entree());
624
-	if (preg_match(",\.($preg_extensions)($|[?]),i", $path, $regs)) {
625
-		$terminaison = strtolower($regs[1]);
626
-		return $terminaison;
627
-	}
628
-	return '';
623
+    $preg_extensions = implode('|', _image_extensions_acceptees_en_entree());
624
+    if (preg_match(",\.($preg_extensions)($|[?]),i", $path, $regs)) {
625
+        $terminaison = strtolower($regs[1]);
626
+        return $terminaison;
627
+    }
628
+    return '';
629 629
 }
630 630
 
631 631
 /**
@@ -636,33 +636,33 @@  discard block
 block discarded – undo
636 636
  * @return string Extension, dans le format attendu par les fonctions 'gd' ('jpeg' pour les .jpg par exemple)
637 637
  */
638 638
 function _image_trouver_extension_pertinente($path) {
639
-	$path = supprimer_timestamp($path);
640
-	$terminaison = _image_trouver_extension($path);
641
-	if ($terminaison == 'jpg') {
642
-		$terminaison = 'jpeg';
643
-	}
644
-
645
-	if (!file_exists($path)) {
646
-		return $terminaison;
647
-	}
648
-
649
-	if (!$info = @spip_getimagesize($path)) {
650
-		return $terminaison;
651
-	}
652
-
653
-	if (isset($info['mime'])) {
654
-		$mime = $info['mime'];
655
-	}
656
-	else {
657
-		$mime = image_type_to_mime_type($info[2]);
658
-	}
659
-
660
-	$_terminaison = _image_trouver_extension_depuis_mime($mime);
661
-	if ($_terminaison and $_terminaison !== $terminaison) {
662
-		spip_log("Mauvaise extension du fichier : $path . Son type mime est : $mime", 'images.' . _LOG_INFO_IMPORTANTE);
663
-		$terminaison = $_terminaison;
664
-	}
665
-	return $terminaison;
639
+    $path = supprimer_timestamp($path);
640
+    $terminaison = _image_trouver_extension($path);
641
+    if ($terminaison == 'jpg') {
642
+        $terminaison = 'jpeg';
643
+    }
644
+
645
+    if (!file_exists($path)) {
646
+        return $terminaison;
647
+    }
648
+
649
+    if (!$info = @spip_getimagesize($path)) {
650
+        return $terminaison;
651
+    }
652
+
653
+    if (isset($info['mime'])) {
654
+        $mime = $info['mime'];
655
+    }
656
+    else {
657
+        $mime = image_type_to_mime_type($info[2]);
658
+    }
659
+
660
+    $_terminaison = _image_trouver_extension_depuis_mime($mime);
661
+    if ($_terminaison and $_terminaison !== $terminaison) {
662
+        spip_log("Mauvaise extension du fichier : $path . Son type mime est : $mime", 'images.' . _LOG_INFO_IMPORTANTE);
663
+        $terminaison = $_terminaison;
664
+    }
665
+    return $terminaison;
666 666
 }
667 667
 
668 668
 /**
@@ -670,36 +670,36 @@  discard block
 block discarded – undo
670 670
  * @return string
671 671
  */
672 672
 function _image_trouver_extension_depuis_mime($mime) {
673
-	switch (strtolower($mime)) {
674
-		case 'image/png':
675
-		case 'image/x-png':
676
-			$terminaison = 'png';
677
-			break;
678
-
679
-		case 'image/jpg':
680
-		case 'image/jpeg':
681
-		case 'image/pjpeg':
682
-			$terminaison = 'jpeg';
683
-			break;
684
-
685
-		case 'image/gif':
686
-			$terminaison = 'gif';
687
-			break;
688
-
689
-		case 'image/webp':
690
-		case 'image/x-webp':
691
-			$terminaison = 'webp';
692
-			break;
693
-
694
-		case 'image/svg+xml':
695
-			$terminaison = 'svg';
696
-			break;
697
-
698
-		default:
699
-			$terminaison = '';
700
-	}
701
-
702
-	return $terminaison;
673
+    switch (strtolower($mime)) {
674
+        case 'image/png':
675
+        case 'image/x-png':
676
+            $terminaison = 'png';
677
+            break;
678
+
679
+        case 'image/jpg':
680
+        case 'image/jpeg':
681
+        case 'image/pjpeg':
682
+            $terminaison = 'jpeg';
683
+            break;
684
+
685
+        case 'image/gif':
686
+            $terminaison = 'gif';
687
+            break;
688
+
689
+        case 'image/webp':
690
+        case 'image/x-webp':
691
+            $terminaison = 'webp';
692
+            break;
693
+
694
+        case 'image/svg+xml':
695
+            $terminaison = 'svg';
696
+            break;
697
+
698
+        default:
699
+            $terminaison = '';
700
+    }
701
+
702
+    return $terminaison;
703 703
 }
704 704
 
705 705
 
@@ -719,18 +719,18 @@  discard block
 block discarded – undo
719 719
  *     Une ressource de type Image GD.
720 720
  */
721 721
 function _imagecreatefrom_func(string $func, string $filename) {
722
-	if (!function_exists($func)) {
723
-		spip_log("GD indisponible : $func inexistante. Traitement $filename impossible.", _LOG_CRITIQUE);
724
-		erreur_squelette("GD indisponible : $func inexistante. Traitement $filename impossible.");
725
-		return null;
726
-	}
727
-	$img = @$func($filename);
728
-	if (!$img) {
729
-		spip_log("Erreur lecture imagecreatefromjpeg $filename", _LOG_CRITIQUE);
730
-		erreur_squelette("Erreur lecture imagecreatefromjpeg $filename");
731
-		$img = imagecreate(10, 10);
732
-	}
733
-	return $img;
722
+    if (!function_exists($func)) {
723
+        spip_log("GD indisponible : $func inexistante. Traitement $filename impossible.", _LOG_CRITIQUE);
724
+        erreur_squelette("GD indisponible : $func inexistante. Traitement $filename impossible.");
725
+        return null;
726
+    }
727
+    $img = @$func($filename);
728
+    if (!$img) {
729
+        spip_log("Erreur lecture imagecreatefromjpeg $filename", _LOG_CRITIQUE);
730
+        erreur_squelette("Erreur lecture imagecreatefromjpeg $filename");
731
+        $img = imagecreate(10, 10);
732
+    }
733
+    return $img;
734 734
 }
735 735
 
736 736
 /**
@@ -746,7 +746,7 @@  discard block
 block discarded – undo
746 746
  *     Une ressource de type Image GD.
747 747
  */
748 748
 function _imagecreatefromjpeg($filename) {
749
-	return _imagecreatefrom_func('imagecreatefromjpeg', $filename);
749
+    return _imagecreatefrom_func('imagecreatefromjpeg', $filename);
750 750
 }
751 751
 
752 752
 /**
@@ -762,7 +762,7 @@  discard block
 block discarded – undo
762 762
  *     Une ressource de type Image GD.
763 763
  */
764 764
 function _imagecreatefrompng($filename) {
765
-	return _imagecreatefrom_func('imagecreatefrompng', $filename);
765
+    return _imagecreatefrom_func('imagecreatefrompng', $filename);
766 766
 }
767 767
 
768 768
 /**
@@ -778,7 +778,7 @@  discard block
 block discarded – undo
778 778
  *     Une ressource de type Image GD.
779 779
  */
780 780
 function _imagecreatefromgif($filename) {
781
-	return _imagecreatefrom_func('imagecreatefromgif', $filename);
781
+    return _imagecreatefrom_func('imagecreatefromgif', $filename);
782 782
 }
783 783
 
784 784
 
@@ -795,7 +795,7 @@  discard block
 block discarded – undo
795 795
  *     Une ressource de type Image GD.
796 796
  */
797 797
 function _imagecreatefromwebp($filename) {
798
-	return _imagecreatefrom_func('imagecreatefromwebp', $filename);
798
+    return _imagecreatefrom_func('imagecreatefromwebp', $filename);
799 799
 }
800 800
 
801 801
 /**
@@ -813,24 +813,24 @@  discard block
 block discarded – undo
813 813
  *     - true si une image est bien retournée.
814 814
  */
815 815
 function _image_imagepng($img, $fichier) {
816
-	if (!function_exists('imagepng')) {
817
-		return false;
818
-	}
819
-	$tmp = $fichier . '.tmp';
820
-	$ret = imagepng($img, $tmp);
821
-	if (file_exists($tmp)) {
822
-		$taille_test = @getimagesize($tmp);
823
-		if (empty($taille_test[0])) {
824
-			return false;
825
-		}
826
-
827
-		spip_unlink($fichier); // le fichier peut deja exister
828
-		@rename($tmp, $fichier);
829
-
830
-		return $ret;
831
-	}
832
-
833
-	return false;
816
+    if (!function_exists('imagepng')) {
817
+        return false;
818
+    }
819
+    $tmp = $fichier . '.tmp';
820
+    $ret = imagepng($img, $tmp);
821
+    if (file_exists($tmp)) {
822
+        $taille_test = @getimagesize($tmp);
823
+        if (empty($taille_test[0])) {
824
+            return false;
825
+        }
826
+
827
+        spip_unlink($fichier); // le fichier peut deja exister
828
+        @rename($tmp, $fichier);
829
+
830
+        return $ret;
831
+    }
832
+
833
+    return false;
834 834
 }
835 835
 
836 836
 /**
@@ -848,24 +848,24 @@  discard block
 block discarded – undo
848 848
  *     - true si une image est bien retournée.
849 849
  */
850 850
 function _image_imagegif($img, $fichier) {
851
-	if (!function_exists('imagegif')) {
852
-		return false;
853
-	}
854
-	$tmp = $fichier . '.tmp';
855
-	$ret = imagegif($img, $tmp);
856
-	if (file_exists($tmp)) {
857
-		$taille_test = @getimagesize($tmp);
858
-		if (empty($taille_test[0])) {
859
-			return false;
860
-		}
861
-
862
-		spip_unlink($fichier); // le fichier peut deja exister
863
-		@rename($tmp, $fichier);
864
-
865
-		return $ret;
866
-	}
867
-
868
-	return false;
851
+    if (!function_exists('imagegif')) {
852
+        return false;
853
+    }
854
+    $tmp = $fichier . '.tmp';
855
+    $ret = imagegif($img, $tmp);
856
+    if (file_exists($tmp)) {
857
+        $taille_test = @getimagesize($tmp);
858
+        if (empty($taille_test[0])) {
859
+            return false;
860
+        }
861
+
862
+        spip_unlink($fichier); // le fichier peut deja exister
863
+        @rename($tmp, $fichier);
864
+
865
+        return $ret;
866
+    }
867
+
868
+    return false;
869 869
 }
870 870
 
871 871
 /**
@@ -888,29 +888,29 @@  discard block
 block discarded – undo
888 888
  *     - true si une image est bien retournée.
889 889
  */
890 890
 function _image_imagejpg($img, $fichier, $qualite = _IMG_GD_QUALITE) {
891
-	if (!function_exists('imagejpeg')) {
892
-		return false;
893
-	}
894
-	$tmp = $fichier . '.tmp';
891
+    if (!function_exists('imagejpeg')) {
892
+        return false;
893
+    }
894
+    $tmp = $fichier . '.tmp';
895 895
 
896
-	// Enable interlancing
897
-	imageinterlace($img, true);
896
+    // Enable interlancing
897
+    imageinterlace($img, true);
898 898
 
899
-	$ret = imagejpeg($img, $tmp, $qualite);
899
+    $ret = imagejpeg($img, $tmp, $qualite);
900 900
 
901
-	if (file_exists($tmp)) {
902
-		$taille_test = @getimagesize($tmp);
903
-		if (empty($taille_test[0])) {
904
-			return false;
905
-		}
901
+    if (file_exists($tmp)) {
902
+        $taille_test = @getimagesize($tmp);
903
+        if (empty($taille_test[0])) {
904
+            return false;
905
+        }
906 906
 
907
-		spip_unlink($fichier); // le fichier peut deja exister
908
-		@rename($tmp, $fichier);
907
+        spip_unlink($fichier); // le fichier peut deja exister
908
+        @rename($tmp, $fichier);
909 909
 
910
-		return $ret;
911
-	}
910
+        return $ret;
911
+    }
912 912
 
913
-	return false;
913
+    return false;
914 914
 }
915 915
 
916 916
 /**
@@ -928,9 +928,9 @@  discard block
 block discarded – undo
928 928
  *     true si le fichier a bien été créé ; false sinon.
929 929
  */
930 930
 function _image_imageico($img, $fichier) {
931
-	$gd_image_array = [$img];
931
+    $gd_image_array = [$img];
932 932
 
933
-	return ecrire_fichier($fichier, phpthumb_functions::GD2ICOstring($gd_image_array));
933
+    return ecrire_fichier($fichier, phpthumb_functions::GD2ICOstring($gd_image_array));
934 934
 }
935 935
 
936 936
 
@@ -949,24 +949,24 @@  discard block
 block discarded – undo
949 949
  *     - true si une image est bien retournée.
950 950
  */
951 951
 function _image_imagewebp($img, $fichier, $qualite = _IMG_GD_QUALITE) {
952
-	if (!function_exists('imagewebp')) {
953
-		return false;
954
-	}
955
-	$tmp = $fichier . '.tmp';
956
-	$ret = imagewebp($img, $tmp, $qualite);
957
-	if (file_exists($tmp)) {
958
-		$taille_test = @getimagesize($tmp);
959
-		if (empty($taille_test[0])) {
960
-			return false;
961
-		}
962
-
963
-		spip_unlink($fichier); // le fichier peut deja exister
964
-		@rename($tmp, $fichier);
965
-
966
-		return $ret;
967
-	}
968
-
969
-	return false;
952
+    if (!function_exists('imagewebp')) {
953
+        return false;
954
+    }
955
+    $tmp = $fichier . '.tmp';
956
+    $ret = imagewebp($img, $tmp, $qualite);
957
+    if (file_exists($tmp)) {
958
+        $taille_test = @getimagesize($tmp);
959
+        if (empty($taille_test[0])) {
960
+            return false;
961
+        }
962
+
963
+        spip_unlink($fichier); // le fichier peut deja exister
964
+        @rename($tmp, $fichier);
965
+
966
+        return $ret;
967
+    }
968
+
969
+    return false;
970 970
 }
971 971
 
972 972
 /**
@@ -986,35 +986,35 @@  discard block
 block discarded – undo
986 986
  */
987 987
 function _image_imagesvg($img, $fichier) {
988 988
 
989
-	$tmp = $fichier . '.tmp';
990
-	if (strpos($img, '<') === false) {
991
-		$img = supprimer_timestamp($img);
992
-		if (!file_exists($img)) {
993
-			return false;
994
-		}
995
-		@copy($img, $tmp);
996
-		if (filesize($tmp) == filesize($img)) {
997
-			spip_unlink($fichier); // le fichier peut deja exister
998
-			@rename($tmp, $fichier);
999
-			return true;
1000
-		}
1001
-		return false;
1002
-	}
1003
-
1004
-	file_put_contents($tmp, $img);
1005
-	if (file_exists($tmp)) {
1006
-		$taille_test = spip_getimagesize($tmp);
1007
-		if (empty($taille_test[0])) {
1008
-			return false;
1009
-		}
1010
-
1011
-		spip_unlink($fichier); // le fichier peut deja exister
1012
-		@rename($tmp, $fichier);
1013
-
1014
-		return true;
1015
-	}
1016
-
1017
-	return false;
989
+    $tmp = $fichier . '.tmp';
990
+    if (strpos($img, '<') === false) {
991
+        $img = supprimer_timestamp($img);
992
+        if (!file_exists($img)) {
993
+            return false;
994
+        }
995
+        @copy($img, $tmp);
996
+        if (filesize($tmp) == filesize($img)) {
997
+            spip_unlink($fichier); // le fichier peut deja exister
998
+            @rename($tmp, $fichier);
999
+            return true;
1000
+        }
1001
+        return false;
1002
+    }
1003
+
1004
+    file_put_contents($tmp, $img);
1005
+    if (file_exists($tmp)) {
1006
+        $taille_test = spip_getimagesize($tmp);
1007
+        if (empty($taille_test[0])) {
1008
+            return false;
1009
+        }
1010
+
1011
+        spip_unlink($fichier); // le fichier peut deja exister
1012
+        @rename($tmp, $fichier);
1013
+
1014
+        return true;
1015
+    }
1016
+
1017
+    return false;
1018 1018
 }
1019 1019
 
1020 1020
 
@@ -1042,30 +1042,30 @@  discard block
 block discarded – undo
1042 1042
  *     - false sinon.
1043 1043
  */
1044 1044
 function _image_gd_output($img, $valeurs, $qualite = _IMG_GD_QUALITE, $fonction = null) {
1045
-	if (is_null($fonction)) {
1046
-		$fonction = '_image_image' . $valeurs['format_dest'];
1047
-	}
1048
-	$ret = false;
1049
-	#un flag pour reperer les images gravees
1050
-	$lock = (
1051
-		!statut_effacer_images_temporaires('get') // si la fonction n'a pas ete activee, on grave tout
1052
-		or (@file_exists($valeurs['fichier_dest']) and !@file_exists($valeurs['fichier_dest'] . '.src'))
1053
-	);
1054
-	if (
1055
-		function_exists($fonction)
1056
-		&& ($ret = $fonction($img, $valeurs['fichier_dest'], $qualite)) # on a reussi a creer l'image
1057
-		&& isset($valeurs['reconstruction']) # et on sait comment la resonctruire le cas echeant
1058
-		&& !$lock
1059
-	) {
1060
-		if (@file_exists($valeurs['fichier_dest'])) {
1061
-			// dans tous les cas mettre a jour la taille de l'image finale
1062
-			[$valeurs['hauteur_dest'], $valeurs['largeur_dest']] = taille_image($valeurs['fichier_dest']);
1063
-			$valeurs['date'] = @filemtime($valeurs['fichier_dest']); // pour la retrouver apres disparition
1064
-			ecrire_fichier($valeurs['fichier_dest'] . '.src', serialize($valeurs), true);
1065
-		}
1066
-	}
1067
-
1068
-	return $ret;
1045
+    if (is_null($fonction)) {
1046
+        $fonction = '_image_image' . $valeurs['format_dest'];
1047
+    }
1048
+    $ret = false;
1049
+    #un flag pour reperer les images gravees
1050
+    $lock = (
1051
+        !statut_effacer_images_temporaires('get') // si la fonction n'a pas ete activee, on grave tout
1052
+        or (@file_exists($valeurs['fichier_dest']) and !@file_exists($valeurs['fichier_dest'] . '.src'))
1053
+    );
1054
+    if (
1055
+        function_exists($fonction)
1056
+        && ($ret = $fonction($img, $valeurs['fichier_dest'], $qualite)) # on a reussi a creer l'image
1057
+        && isset($valeurs['reconstruction']) # et on sait comment la resonctruire le cas echeant
1058
+        && !$lock
1059
+    ) {
1060
+        if (@file_exists($valeurs['fichier_dest'])) {
1061
+            // dans tous les cas mettre a jour la taille de l'image finale
1062
+            [$valeurs['hauteur_dest'], $valeurs['largeur_dest']] = taille_image($valeurs['fichier_dest']);
1063
+            $valeurs['date'] = @filemtime($valeurs['fichier_dest']); // pour la retrouver apres disparition
1064
+            ecrire_fichier($valeurs['fichier_dest'] . '.src', serialize($valeurs), true);
1065
+        }
1066
+    }
1067
+
1068
+    return $ret;
1069 1069
 }
1070 1070
 
1071 1071
 /**
@@ -1078,27 +1078,27 @@  discard block
 block discarded – undo
1078 1078
  *     Chemin vers le fichier manquant
1079 1079
  **/
1080 1080
 function reconstruire_image_intermediaire($fichier_manquant) {
1081
-	$reconstruire = [];
1082
-	$fichier = $fichier_manquant;
1083
-	while (
1084
-		strpos($fichier, '://') === false
1085
-		and !@file_exists($fichier)
1086
-		and lire_fichier($src = "$fichier.src", $source)
1087
-		and $valeurs = unserialize($source)
1088
-		and ($fichier = $valeurs['fichier']) # l'origine est connue (on ne verifie pas son existence, qu'importe ...)
1089
-	) {
1090
-		spip_unlink($src); // si jamais on a un timeout pendant la reconstruction, elle se fera naturellement au hit suivant
1091
-		$reconstruire[] = $valeurs['reconstruction'];
1092
-	}
1093
-	while (count($reconstruire)) {
1094
-		$r = array_pop($reconstruire);
1095
-		$fonction = $r[0];
1096
-		$args = $r[1];
1097
-		$fonction(...$args);
1098
-	}
1099
-	// cette image intermediaire est commune a plusieurs series de filtre, il faut la conserver
1100
-	// mais l'on peut nettoyer les miettes de sa creation
1101
-	ramasse_miettes($fichier_manquant);
1081
+    $reconstruire = [];
1082
+    $fichier = $fichier_manquant;
1083
+    while (
1084
+        strpos($fichier, '://') === false
1085
+        and !@file_exists($fichier)
1086
+        and lire_fichier($src = "$fichier.src", $source)
1087
+        and $valeurs = unserialize($source)
1088
+        and ($fichier = $valeurs['fichier']) # l'origine est connue (on ne verifie pas son existence, qu'importe ...)
1089
+    ) {
1090
+        spip_unlink($src); // si jamais on a un timeout pendant la reconstruction, elle se fera naturellement au hit suivant
1091
+        $reconstruire[] = $valeurs['reconstruction'];
1092
+    }
1093
+    while (count($reconstruire)) {
1094
+        $r = array_pop($reconstruire);
1095
+        $fonction = $r[0];
1096
+        $args = $r[1];
1097
+        $fonction(...$args);
1098
+    }
1099
+    // cette image intermediaire est commune a plusieurs series de filtre, il faut la conserver
1100
+    // mais l'on peut nettoyer les miettes de sa creation
1101
+    ramasse_miettes($fichier_manquant);
1102 1102
 }
1103 1103
 
1104 1104
 /**
@@ -1118,28 +1118,28 @@  discard block
 block discarded – undo
1118 1118
  *     Chemin du fichier d'image calculé
1119 1119
  **/
1120 1120
 function ramasse_miettes($fichier) {
1121
-	if (
1122
-		strpos($fichier, '://') !== false
1123
-		or !lire_fichier($src = "$fichier.src", $source)
1124
-		or !$valeurs = unserialize($source)
1125
-	) {
1126
-		return;
1127
-	}
1128
-	spip_unlink($src); # on supprime la reference a sa source pour marquer cette image comme non intermediaire
1129
-	while (
1130
-		($fichier = $valeurs['fichier']) # l'origine est connue (on ne verifie pas son existence, qu'importe ...)
1131
-		and (substr($fichier, 0, strlen(_DIR_VAR)) == _DIR_VAR) # et est dans local
1132
-		and (lire_fichier(
1133
-			$src = "$fichier.src",
1134
-			$source
1135
-		)) # le fichier a une source connue (c'est donc une image calculee intermediaire)
1136
-		and ($valeurs = unserialize($source))  # et valide
1137
-	) {
1138
-		# on efface le fichier
1139
-		spip_unlink($fichier);
1140
-		# mais laisse le .src qui permet de savoir comment reconstruire l'image si besoin
1141
-		#spip_unlink($src);
1142
-	}
1121
+    if (
1122
+        strpos($fichier, '://') !== false
1123
+        or !lire_fichier($src = "$fichier.src", $source)
1124
+        or !$valeurs = unserialize($source)
1125
+    ) {
1126
+        return;
1127
+    }
1128
+    spip_unlink($src); # on supprime la reference a sa source pour marquer cette image comme non intermediaire
1129
+    while (
1130
+        ($fichier = $valeurs['fichier']) # l'origine est connue (on ne verifie pas son existence, qu'importe ...)
1131
+        and (substr($fichier, 0, strlen(_DIR_VAR)) == _DIR_VAR) # et est dans local
1132
+        and (lire_fichier(
1133
+            $src = "$fichier.src",
1134
+            $source
1135
+        )) # le fichier a une source connue (c'est donc une image calculee intermediaire)
1136
+        and ($valeurs = unserialize($source))  # et valide
1137
+    ) {
1138
+        # on efface le fichier
1139
+        spip_unlink($fichier);
1140
+        # mais laisse le .src qui permet de savoir comment reconstruire l'image si besoin
1141
+        #spip_unlink($src);
1142
+    }
1143 1143
 }
1144 1144
 
1145 1145
 
@@ -1164,35 +1164,35 @@  discard block
 block discarded – undo
1164 1164
  *     Code HTML de l'image
1165 1165
  **/
1166 1166
 function image_graver($img) {
1167
-	// appeler le filtre post_image_filtrer qui permet de faire
1168
-	// des traitements auto a la fin d'une serie de filtres
1169
-	$img = pipeline('post_image_filtrer', $img);
1170
-
1171
-	$fichier_ori = $fichier = (extraire_attribut($img, 'src') ?? '');
1172
-	if (($p = strpos($fichier, '?')) !== false) {
1173
-		$fichier = substr($fichier, 0, $p);
1174
-	}
1175
-	if (strlen($fichier) < 1) {
1176
-		if (strpos($img, '<img ') !== 0) {
1177
-			$fichier = $img;
1178
-		}
1179
-	}
1180
-	if (strlen($fichier)) {
1181
-		# si jamais le fichier final n'a pas ete calcule car suppose temporaire
1182
-		# et qu'il ne s'agit pas d'une URL
1183
-		if (strpos($fichier, '://') === false and !@file_exists($fichier)) {
1184
-			reconstruire_image_intermediaire($fichier);
1185
-		}
1186
-		ramasse_miettes($fichier);
1187
-
1188
-		// ajouter le timestamp si besoin
1189
-		if (strpos($fichier_ori, '?') === false) {
1190
-			// on utilise str_replace pour attraper le onmouseover des logo si besoin
1191
-			$img = str_replace($fichier_ori, timestamp($fichier_ori), $img);
1192
-		}
1193
-	}
1194
-
1195
-	return $img;
1167
+    // appeler le filtre post_image_filtrer qui permet de faire
1168
+    // des traitements auto a la fin d'une serie de filtres
1169
+    $img = pipeline('post_image_filtrer', $img);
1170
+
1171
+    $fichier_ori = $fichier = (extraire_attribut($img, 'src') ?? '');
1172
+    if (($p = strpos($fichier, '?')) !== false) {
1173
+        $fichier = substr($fichier, 0, $p);
1174
+    }
1175
+    if (strlen($fichier) < 1) {
1176
+        if (strpos($img, '<img ') !== 0) {
1177
+            $fichier = $img;
1178
+        }
1179
+    }
1180
+    if (strlen($fichier)) {
1181
+        # si jamais le fichier final n'a pas ete calcule car suppose temporaire
1182
+        # et qu'il ne s'agit pas d'une URL
1183
+        if (strpos($fichier, '://') === false and !@file_exists($fichier)) {
1184
+            reconstruire_image_intermediaire($fichier);
1185
+        }
1186
+        ramasse_miettes($fichier);
1187
+
1188
+        // ajouter le timestamp si besoin
1189
+        if (strpos($fichier_ori, '?') === false) {
1190
+            // on utilise str_replace pour attraper le onmouseover des logo si besoin
1191
+            $img = str_replace($fichier_ori, timestamp($fichier_ori), $img);
1192
+        }
1193
+    }
1194
+
1195
+    return $img;
1196 1196
 }
1197 1197
 
1198 1198
 /**
@@ -1219,34 +1219,34 @@  discard block
 block discarded – undo
1219 1219
  *     Code html modifié de la balise.
1220 1220
  **/
1221 1221
 function _image_tag_changer_taille($tag, $width, $height, $style = false) {
1222
-	if ($style === false) {
1223
-		$style = extraire_attribut($tag, 'style');
1224
-	}
1225
-
1226
-	// enlever le width et height du style
1227
-	if ($style) {
1228
-		$style = preg_replace(',(^|;)\s*(width|height)\s*:\s*[^;]+,ims', '', $style);
1229
-	}
1230
-	if ($style and $style[0] === ';') {
1231
-		$style = substr($style, 1);
1232
-	}
1233
-
1234
-	// mettre des attributs de width et height sur les images,
1235
-	// ca accelere le rendu du navigateur
1236
-	// ca permet aux navigateurs de reserver la bonne taille
1237
-	// quand on a desactive l'affichage des images.
1238
-	$tag = inserer_attribut($tag, 'width', round($width));
1239
-	$tag = inserer_attribut($tag, 'height', round($height));
1240
-
1241
-	// attributs deprecies. Transformer en CSS
1242
-	if ($espace = extraire_attribut($tag, 'hspace')) {
1243
-		$style = "margin:{$espace}px;" . $style;
1244
-		$tag = inserer_attribut($tag, 'hspace', '');
1245
-	}
1246
-
1247
-	$tag = inserer_attribut($tag, 'style', (string) $style, true, $style ? false : true);
1248
-
1249
-	return $tag;
1222
+    if ($style === false) {
1223
+        $style = extraire_attribut($tag, 'style');
1224
+    }
1225
+
1226
+    // enlever le width et height du style
1227
+    if ($style) {
1228
+        $style = preg_replace(',(^|;)\s*(width|height)\s*:\s*[^;]+,ims', '', $style);
1229
+    }
1230
+    if ($style and $style[0] === ';') {
1231
+        $style = substr($style, 1);
1232
+    }
1233
+
1234
+    // mettre des attributs de width et height sur les images,
1235
+    // ca accelere le rendu du navigateur
1236
+    // ca permet aux navigateurs de reserver la bonne taille
1237
+    // quand on a desactive l'affichage des images.
1238
+    $tag = inserer_attribut($tag, 'width', round($width));
1239
+    $tag = inserer_attribut($tag, 'height', round($height));
1240
+
1241
+    // attributs deprecies. Transformer en CSS
1242
+    if ($espace = extraire_attribut($tag, 'hspace')) {
1243
+        $style = "margin:{$espace}px;" . $style;
1244
+        $tag = inserer_attribut($tag, 'hspace', '');
1245
+    }
1246
+
1247
+    $tag = inserer_attribut($tag, 'style', (string) $style, true, $style ? false : true);
1248
+
1249
+    return $tag;
1250 1250
 }
1251 1251
 
1252 1252
 
@@ -1272,72 +1272,72 @@  discard block
 block discarded – undo
1272 1272
  *     Retourne le code HTML de l'image
1273 1273
  **/
1274 1274
 function _image_ecrire_tag($valeurs, $surcharge = []) {
1275
-	$valeurs = pipeline('image_ecrire_tag_preparer', $valeurs);
1276
-
1277
-	// fermer les tags img pas bien fermes;
1278
-	$tag = str_replace('>', '/>', str_replace('/>', '>', $valeurs['tag']));
1279
-
1280
-	// le style
1281
-	$style = $valeurs['style'];
1282
-	if (isset($surcharge['style'])) {
1283
-		$style = $surcharge['style'];
1284
-		unset($surcharge['style']);
1285
-	}
1286
-
1287
-	// traiter specifiquement la largeur et la hauteur
1288
-	$width = $valeurs['largeur'];
1289
-	if (isset($surcharge['width'])) {
1290
-		$width = $surcharge['width'];
1291
-		unset($surcharge['width']);
1292
-	}
1293
-	$height = $valeurs['hauteur'];
1294
-	if (isset($surcharge['height'])) {
1295
-		$height = $surcharge['height'];
1296
-		unset($surcharge['height']);
1297
-	}
1298
-
1299
-	$tag = _image_tag_changer_taille($tag, $width, $height, $style);
1300
-	// traiter specifiquement le src qui peut etre repris dans un onmouseout
1301
-	// on remplace toute les ref a src dans le tag
1302
-	$src = extraire_attribut($tag, 'src');
1303
-	if (isset($surcharge['src'])) {
1304
-		$tag = str_replace($src, $surcharge['src'], $tag);
1305
-		// si il y a des & dans src, alors ils peuvent provenir d'un &amp
1306
-		// pas garanti comme methode, mais mieux que rien
1307
-		if (strpos($src, '&') !== false) {
1308
-			$tag = str_replace(str_replace('&', '&amp;', $src), $surcharge['src'], $tag);
1309
-		}
1310
-		$src = $surcharge['src'];
1311
-		unset($surcharge['src']);
1312
-	}
1313
-
1314
-	$class = $valeurs['class'];
1315
-	if (isset($surcharge['class'])) {
1316
-		$class = $surcharge['class'];
1317
-		unset($surcharge['class']);
1318
-	}
1319
-	if (is_scalar($class) && strlen($class)) {
1320
-		$tag = inserer_attribut($tag, 'class', $class);
1321
-	}
1322
-
1323
-	if (count($surcharge)) {
1324
-		foreach ($surcharge as $attribut => $valeur) {
1325
-			$tag = inserer_attribut($tag, $attribut, $valeur);
1326
-		}
1327
-	}
1328
-
1329
-	$tag = pipeline(
1330
-		'image_ecrire_tag_finir',
1331
-		[
1332
-			'args' => [
1333
-				'valeurs' => $valeurs,
1334
-				'surcharge' => $surcharge,
1335
-			],
1336
-			'data' => $tag
1337
-		]
1338
-	);
1339
-
1340
-	return $tag;
1275
+    $valeurs = pipeline('image_ecrire_tag_preparer', $valeurs);
1276
+
1277
+    // fermer les tags img pas bien fermes;
1278
+    $tag = str_replace('>', '/>', str_replace('/>', '>', $valeurs['tag']));
1279
+
1280
+    // le style
1281
+    $style = $valeurs['style'];
1282
+    if (isset($surcharge['style'])) {
1283
+        $style = $surcharge['style'];
1284
+        unset($surcharge['style']);
1285
+    }
1286
+
1287
+    // traiter specifiquement la largeur et la hauteur
1288
+    $width = $valeurs['largeur'];
1289
+    if (isset($surcharge['width'])) {
1290
+        $width = $surcharge['width'];
1291
+        unset($surcharge['width']);
1292
+    }
1293
+    $height = $valeurs['hauteur'];
1294
+    if (isset($surcharge['height'])) {
1295
+        $height = $surcharge['height'];
1296
+        unset($surcharge['height']);
1297
+    }
1298
+
1299
+    $tag = _image_tag_changer_taille($tag, $width, $height, $style);
1300
+    // traiter specifiquement le src qui peut etre repris dans un onmouseout
1301
+    // on remplace toute les ref a src dans le tag
1302
+    $src = extraire_attribut($tag, 'src');
1303
+    if (isset($surcharge['src'])) {
1304
+        $tag = str_replace($src, $surcharge['src'], $tag);
1305
+        // si il y a des & dans src, alors ils peuvent provenir d'un &amp
1306
+        // pas garanti comme methode, mais mieux que rien
1307
+        if (strpos($src, '&') !== false) {
1308
+            $tag = str_replace(str_replace('&', '&amp;', $src), $surcharge['src'], $tag);
1309
+        }
1310
+        $src = $surcharge['src'];
1311
+        unset($surcharge['src']);
1312
+    }
1313
+
1314
+    $class = $valeurs['class'];
1315
+    if (isset($surcharge['class'])) {
1316
+        $class = $surcharge['class'];
1317
+        unset($surcharge['class']);
1318
+    }
1319
+    if (is_scalar($class) && strlen($class)) {
1320
+        $tag = inserer_attribut($tag, 'class', $class);
1321
+    }
1322
+
1323
+    if (count($surcharge)) {
1324
+        foreach ($surcharge as $attribut => $valeur) {
1325
+            $tag = inserer_attribut($tag, $attribut, $valeur);
1326
+        }
1327
+    }
1328
+
1329
+    $tag = pipeline(
1330
+        'image_ecrire_tag_finir',
1331
+        [
1332
+            'args' => [
1333
+                'valeurs' => $valeurs,
1334
+                'surcharge' => $surcharge,
1335
+            ],
1336
+            'data' => $tag
1337
+        ]
1338
+    );
1339
+
1340
+    return $tag;
1341 1341
 }
1342 1342
 
1343 1343
 /**
@@ -1360,268 +1360,268 @@  discard block
 block discarded – undo
1360 1360
  *     Description de l'image, sinon null.
1361 1361
  **/
1362 1362
 function _image_creer_vignette($valeurs, $maxWidth, $maxHeight, $process = 'AUTO', $force = false) {
1363
-	$srcHeight = null;
1364
-	$retour = [];
1365
-	// ordre de preference des formats graphiques pour creer les vignettes
1366
-	// le premier format disponible, selon la methode demandee, est utilise
1367
-	$image = $valeurs['fichier'];
1368
-	$format = $valeurs['format_source'];
1369
-	$destdir = dirname($valeurs['fichier_dest']);
1370
-	$destfile = basename($valeurs['fichier_dest'], '.' . $valeurs['format_dest']);
1371
-
1372
-	$format_sortie = $valeurs['format_dest'];
1373
-
1374
-	if (($process == 'AUTO') and isset($GLOBALS['meta']['image_process'])) {
1375
-		$process = $GLOBALS['meta']['image_process'];
1376
-	}
1377
-
1378
-	// si le doc n'est pas une image dans un format accetpable, refuser
1379
-	if (!$force and !in_array($format, formats_image_acceptables(in_array($process, ['gd1', 'gd2'])))) {
1380
-		return;
1381
-	}
1382
-	$destination = "$destdir/$destfile";
1383
-
1384
-	// calculer la taille
1385
-	if (($srcWidth = $valeurs['largeur']) && ($srcHeight = $valeurs['hauteur'])) {
1386
-		if (!($destWidth = $valeurs['largeur_dest']) || !($destHeight = $valeurs['hauteur_dest'])) {
1387
-			[$destWidth, $destHeight] = _image_ratio($srcWidth, $srcHeight, $maxWidth, $maxHeight);
1388
-		}
1389
-	} elseif ($process == 'convert' or $process == 'imagick') {
1390
-		$destWidth = $maxWidth;
1391
-		$destHeight = $maxHeight;
1392
-	} else {
1393
-		spip_log("echec $process sur $image");
1394
-
1395
-		return;
1396
-	}
1397
-
1398
-	$vignette = '';
1399
-
1400
-	// Si l'image est de la taille demandee (ou plus petite), simplement la retourner
1401
-	if ($srcWidth and $srcWidth <= $maxWidth and $srcHeight <= $maxHeight) {
1402
-		$vignette = $destination . '.' . $format;
1403
-		@copy($image, $vignette);
1404
-	}
1405
-
1406
-	elseif ($valeurs['format_source'] === 'svg') {
1407
-		include_spip('inc/svg');
1408
-		if ($svg = svg_redimensionner($valeurs['fichier'], $destWidth, $destHeight)) {
1409
-			$format_sortie = 'svg';
1410
-			$vignette = $destination . '.' . $format_sortie;
1411
-			$valeurs['fichier_dest'] = $vignette;
1412
-			_image_gd_output($svg, $valeurs);
1413
-		}
1414
-	}
1415
-
1416
-	// imagemagick en ligne de commande
1417
-	elseif ($process == 'convert') {
1418
-		if (!defined('_CONVERT_COMMAND')) {
1419
-			define('_CONVERT_COMMAND', 'convert');
1420
-		} // Securite : mes_options.php peut preciser le chemin absolu
1421
-		if (!defined('_RESIZE_COMMAND')) {
1422
-			define('_RESIZE_COMMAND', _CONVERT_COMMAND . ' -quality ' . _IMG_CONVERT_QUALITE . ' -orient Undefined -resize %xx%y! %src %dest');
1423
-		}
1424
-		$vignette = $destination . '.' . $format_sortie;
1425
-		$commande = str_replace(
1426
-			['%x', '%y', '%src', '%dest'],
1427
-			[
1428
-				$destWidth,
1429
-				$destHeight,
1430
-				escapeshellcmd($image),
1431
-				escapeshellcmd($vignette)
1432
-			],
1433
-			_RESIZE_COMMAND
1434
-		);
1435
-		spip_log($commande);
1436
-		exec($commande);
1437
-		if (!@file_exists($vignette)) {
1438
-			spip_log("echec convert sur $vignette");
1439
-
1440
-			return;  // echec commande
1441
-		}
1442
-	}
1443
-
1444
-	// php5 imagemagick
1445
-	elseif ($process == 'imagick') {
1446
-		if (!class_exists(\Imagick::class)) {
1447
-			spip_log('Classe Imagick absente !', _LOG_ERREUR);
1448
-
1449
-			return;
1450
-		}
1451
-
1452
-		// chemin compatible Windows
1453
-		$output = realpath(dirname($destination));
1454
-		if (!$output) {
1455
-			return;
1456
-		}
1457
-		$vignette = $output . DIRECTORY_SEPARATOR . basename($destination) . '.' . $format_sortie;
1458
-
1459
-		$imagick = new Imagick();
1460
-		$imagick->readImage(realpath($image));
1461
-		$imagick->resizeImage(
1462
-			$destWidth,
1463
-			$destHeight,
1464
-			Imagick::FILTER_LANCZOS,
1465
-			1
1466
-		);//, IMAGICK_FILTER_LANCZOS, _IMG_IMAGICK_QUALITE / 100);
1467
-		$imagick->writeImage($vignette);
1468
-
1469
-		if (!@file_exists($vignette)) {
1470
-			spip_log("echec imagick sur $vignette");
1471
-
1472
-			return;
1473
-		}
1474
-		// remettre le chemin relatif car c'est ce qu'attend SPIP pour la suite (en particlier action/tester)
1475
-		$vignette = $destination . '.' . $format_sortie;
1476
-	}
1477
-
1478
-	// netpbm
1479
-	elseif ($process == 'netpbm') {
1480
-		if (!defined('_PNMSCALE_COMMAND')) {
1481
-			define('_PNMSCALE_COMMAND', 'pnmscale');
1482
-		} // Securite : mes_options.php peut preciser le chemin absolu
1483
-		if (_PNMSCALE_COMMAND == '') {
1484
-			return;
1485
-		}
1486
-		$vignette = $destination . '.' . $format_sortie;
1487
-		$pnmtojpeg_command = str_replace('pnmscale', 'pnmtojpeg', _PNMSCALE_COMMAND);
1488
-		if ($format == 'jpg') {
1489
-			$jpegtopnm_command = str_replace('pnmscale', 'jpegtopnm', _PNMSCALE_COMMAND);
1490
-			exec("$jpegtopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette");
1491
-			if (!($s = @filesize($vignette))) {
1492
-				spip_unlink($vignette);
1493
-			}
1494
-			if (!@file_exists($vignette)) {
1495
-				spip_log("echec netpbm-jpg sur $vignette");
1496
-
1497
-				return;
1498
-			}
1499
-		} else {
1500
-			if ($format == 'gif') {
1501
-				$giftopnm_command = str_replace('pnmscale', 'giftopnm', _PNMSCALE_COMMAND);
1502
-				exec("$giftopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette");
1503
-				if (!($s = @filesize($vignette))) {
1504
-					spip_unlink($vignette);
1505
-				}
1506
-				if (!@file_exists($vignette)) {
1507
-					spip_log("echec netpbm-gif sur $vignette");
1508
-
1509
-					return;
1510
-				}
1511
-			} else {
1512
-				if ($format == 'png') {
1513
-					$pngtopnm_command = str_replace('pnmscale', 'pngtopnm', _PNMSCALE_COMMAND);
1514
-					exec("$pngtopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette");
1515
-					if (!($s = @filesize($vignette))) {
1516
-						spip_unlink($vignette);
1517
-					}
1518
-					if (!@file_exists($vignette)) {
1519
-						spip_log("echec netpbm-png sur $vignette");
1520
-
1521
-						return;
1522
-					}
1523
-				}
1524
-			}
1525
-		}
1526
-	}
1527
-
1528
-	// gd ou gd2
1529
-	elseif ($process == 'gd1' or $process == 'gd2') {
1530
-		if (!function_exists('gd_info')) {
1531
-			spip_log('Librairie GD absente !', _LOG_ERREUR);
1532
-
1533
-			return;
1534
-		}
1535
-		if (_IMG_GD_MAX_PIXELS && $srcWidth * $srcHeight > _IMG_GD_MAX_PIXELS) {
1536
-			spip_log('vignette gd1/gd2 impossible : ' . $srcWidth * $srcHeight . 'pixels');
1537
-
1538
-			return;
1539
-		}
1540
-		$destFormat = $format_sortie;
1541
-		if (!$destFormat) {
1542
-			spip_log("pas de format pour $image");
1543
-
1544
-			return;
1545
-		}
1546
-
1547
-		$fonction_imagecreatefrom = $valeurs['fonction_imagecreatefrom'];
1548
-		if (!function_exists($fonction_imagecreatefrom)) {
1549
-			return;
1550
-		}
1551
-		$srcImage = @$fonction_imagecreatefrom($image);
1552
-		if (!$srcImage) {
1553
-			spip_log('echec gd1/gd2');
1554
-
1555
-			return;
1556
-		}
1557
-
1558
-		// Initialisation de l'image destination
1559
-		$destImage = null;
1560
-		if ($process == 'gd2' and $destFormat != 'gif') {
1561
-			$destImage = ImageCreateTrueColor($destWidth, $destHeight);
1562
-		}
1563
-		if (!$destImage) {
1564
-			$destImage = ImageCreate($destWidth, $destHeight);
1565
-		}
1566
-
1567
-		// Recopie de l'image d'origine avec adaptation de la taille
1568
-		$ok = false;
1569
-		if (($process == 'gd2') and function_exists('ImageCopyResampled')) {
1570
-			if ($format == 'gif') {
1571
-				// Si un GIF est transparent,
1572
-				// fabriquer un PNG transparent
1573
-				$transp = imagecolortransparent($srcImage);
1574
-				if ($transp > 0) {
1575
-					$destFormat = 'png';
1576
-				}
1577
-			}
1578
-			if (in_array($destFormat, _image_extensions_conservent_transparence())) {
1579
-				// Conserver la transparence
1580
-				if (function_exists('imageAntiAlias')) {
1581
-					imageAntiAlias($destImage, true);
1582
-				}
1583
-				@imagealphablending($destImage, false);
1584
-				@imagesavealpha($destImage, true);
1585
-			}
1586
-			$ok = @ImageCopyResampled($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
1587
-		}
1588
-		if (!$ok) {
1589
-			$ok = ImageCopyResized($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
1590
-		}
1591
-
1592
-		// Sauvegarde de l'image destination
1593
-		$valeurs['fichier_dest'] = $vignette = "$destination.$destFormat";
1594
-		$valeurs['format_dest'] = $format = $destFormat;
1595
-		_image_gd_output($destImage, $valeurs);
1596
-
1597
-		if ($srcImage) {
1598
-			ImageDestroy($srcImage);
1599
-		}
1600
-		ImageDestroy($destImage);
1601
-	}
1602
-
1603
-	if (!$vignette or !$size = @spip_getimagesize($vignette)) {
1604
-		$size = [$destWidth, $destHeight];
1605
-	}
1606
-
1607
-	// Gaffe: en safe mode, pas d'acces a la vignette,
1608
-	// donc risque de balancer "width='0'", ce qui masque l'image sous MSIE
1609
-	if ($size[0] < 1) {
1610
-		$size[0] = $destWidth;
1611
-	}
1612
-	if ($size[1] < 1) {
1613
-		$size[1] = $destHeight;
1614
-	}
1615
-
1616
-	$retour['width'] = $largeur = $size[0];
1617
-	$retour['height'] = $hauteur = $size[1];
1618
-
1619
-	$retour['fichier'] = $vignette;
1620
-	$retour['format'] = $format;
1621
-	$retour['date'] = @filemtime($vignette);
1622
-
1623
-	// renvoyer l'image
1624
-	return $retour;
1363
+    $srcHeight = null;
1364
+    $retour = [];
1365
+    // ordre de preference des formats graphiques pour creer les vignettes
1366
+    // le premier format disponible, selon la methode demandee, est utilise
1367
+    $image = $valeurs['fichier'];
1368
+    $format = $valeurs['format_source'];
1369
+    $destdir = dirname($valeurs['fichier_dest']);
1370
+    $destfile = basename($valeurs['fichier_dest'], '.' . $valeurs['format_dest']);
1371
+
1372
+    $format_sortie = $valeurs['format_dest'];
1373
+
1374
+    if (($process == 'AUTO') and isset($GLOBALS['meta']['image_process'])) {
1375
+        $process = $GLOBALS['meta']['image_process'];
1376
+    }
1377
+
1378
+    // si le doc n'est pas une image dans un format accetpable, refuser
1379
+    if (!$force and !in_array($format, formats_image_acceptables(in_array($process, ['gd1', 'gd2'])))) {
1380
+        return;
1381
+    }
1382
+    $destination = "$destdir/$destfile";
1383
+
1384
+    // calculer la taille
1385
+    if (($srcWidth = $valeurs['largeur']) && ($srcHeight = $valeurs['hauteur'])) {
1386
+        if (!($destWidth = $valeurs['largeur_dest']) || !($destHeight = $valeurs['hauteur_dest'])) {
1387
+            [$destWidth, $destHeight] = _image_ratio($srcWidth, $srcHeight, $maxWidth, $maxHeight);
1388
+        }
1389
+    } elseif ($process == 'convert' or $process == 'imagick') {
1390
+        $destWidth = $maxWidth;
1391
+        $destHeight = $maxHeight;
1392
+    } else {
1393
+        spip_log("echec $process sur $image");
1394
+
1395
+        return;
1396
+    }
1397
+
1398
+    $vignette = '';
1399
+
1400
+    // Si l'image est de la taille demandee (ou plus petite), simplement la retourner
1401
+    if ($srcWidth and $srcWidth <= $maxWidth and $srcHeight <= $maxHeight) {
1402
+        $vignette = $destination . '.' . $format;
1403
+        @copy($image, $vignette);
1404
+    }
1405
+
1406
+    elseif ($valeurs['format_source'] === 'svg') {
1407
+        include_spip('inc/svg');
1408
+        if ($svg = svg_redimensionner($valeurs['fichier'], $destWidth, $destHeight)) {
1409
+            $format_sortie = 'svg';
1410
+            $vignette = $destination . '.' . $format_sortie;
1411
+            $valeurs['fichier_dest'] = $vignette;
1412
+            _image_gd_output($svg, $valeurs);
1413
+        }
1414
+    }
1415
+
1416
+    // imagemagick en ligne de commande
1417
+    elseif ($process == 'convert') {
1418
+        if (!defined('_CONVERT_COMMAND')) {
1419
+            define('_CONVERT_COMMAND', 'convert');
1420
+        } // Securite : mes_options.php peut preciser le chemin absolu
1421
+        if (!defined('_RESIZE_COMMAND')) {
1422
+            define('_RESIZE_COMMAND', _CONVERT_COMMAND . ' -quality ' . _IMG_CONVERT_QUALITE . ' -orient Undefined -resize %xx%y! %src %dest');
1423
+        }
1424
+        $vignette = $destination . '.' . $format_sortie;
1425
+        $commande = str_replace(
1426
+            ['%x', '%y', '%src', '%dest'],
1427
+            [
1428
+                $destWidth,
1429
+                $destHeight,
1430
+                escapeshellcmd($image),
1431
+                escapeshellcmd($vignette)
1432
+            ],
1433
+            _RESIZE_COMMAND
1434
+        );
1435
+        spip_log($commande);
1436
+        exec($commande);
1437
+        if (!@file_exists($vignette)) {
1438
+            spip_log("echec convert sur $vignette");
1439
+
1440
+            return;  // echec commande
1441
+        }
1442
+    }
1443
+
1444
+    // php5 imagemagick
1445
+    elseif ($process == 'imagick') {
1446
+        if (!class_exists(\Imagick::class)) {
1447
+            spip_log('Classe Imagick absente !', _LOG_ERREUR);
1448
+
1449
+            return;
1450
+        }
1451
+
1452
+        // chemin compatible Windows
1453
+        $output = realpath(dirname($destination));
1454
+        if (!$output) {
1455
+            return;
1456
+        }
1457
+        $vignette = $output . DIRECTORY_SEPARATOR . basename($destination) . '.' . $format_sortie;
1458
+
1459
+        $imagick = new Imagick();
1460
+        $imagick->readImage(realpath($image));
1461
+        $imagick->resizeImage(
1462
+            $destWidth,
1463
+            $destHeight,
1464
+            Imagick::FILTER_LANCZOS,
1465
+            1
1466
+        );//, IMAGICK_FILTER_LANCZOS, _IMG_IMAGICK_QUALITE / 100);
1467
+        $imagick->writeImage($vignette);
1468
+
1469
+        if (!@file_exists($vignette)) {
1470
+            spip_log("echec imagick sur $vignette");
1471
+
1472
+            return;
1473
+        }
1474
+        // remettre le chemin relatif car c'est ce qu'attend SPIP pour la suite (en particlier action/tester)
1475
+        $vignette = $destination . '.' . $format_sortie;
1476
+    }
1477
+
1478
+    // netpbm
1479
+    elseif ($process == 'netpbm') {
1480
+        if (!defined('_PNMSCALE_COMMAND')) {
1481
+            define('_PNMSCALE_COMMAND', 'pnmscale');
1482
+        } // Securite : mes_options.php peut preciser le chemin absolu
1483
+        if (_PNMSCALE_COMMAND == '') {
1484
+            return;
1485
+        }
1486
+        $vignette = $destination . '.' . $format_sortie;
1487
+        $pnmtojpeg_command = str_replace('pnmscale', 'pnmtojpeg', _PNMSCALE_COMMAND);
1488
+        if ($format == 'jpg') {
1489
+            $jpegtopnm_command = str_replace('pnmscale', 'jpegtopnm', _PNMSCALE_COMMAND);
1490
+            exec("$jpegtopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette");
1491
+            if (!($s = @filesize($vignette))) {
1492
+                spip_unlink($vignette);
1493
+            }
1494
+            if (!@file_exists($vignette)) {
1495
+                spip_log("echec netpbm-jpg sur $vignette");
1496
+
1497
+                return;
1498
+            }
1499
+        } else {
1500
+            if ($format == 'gif') {
1501
+                $giftopnm_command = str_replace('pnmscale', 'giftopnm', _PNMSCALE_COMMAND);
1502
+                exec("$giftopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette");
1503
+                if (!($s = @filesize($vignette))) {
1504
+                    spip_unlink($vignette);
1505
+                }
1506
+                if (!@file_exists($vignette)) {
1507
+                    spip_log("echec netpbm-gif sur $vignette");
1508
+
1509
+                    return;
1510
+                }
1511
+            } else {
1512
+                if ($format == 'png') {
1513
+                    $pngtopnm_command = str_replace('pnmscale', 'pngtopnm', _PNMSCALE_COMMAND);
1514
+                    exec("$pngtopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette");
1515
+                    if (!($s = @filesize($vignette))) {
1516
+                        spip_unlink($vignette);
1517
+                    }
1518
+                    if (!@file_exists($vignette)) {
1519
+                        spip_log("echec netpbm-png sur $vignette");
1520
+
1521
+                        return;
1522
+                    }
1523
+                }
1524
+            }
1525
+        }
1526
+    }
1527
+
1528
+    // gd ou gd2
1529
+    elseif ($process == 'gd1' or $process == 'gd2') {
1530
+        if (!function_exists('gd_info')) {
1531
+            spip_log('Librairie GD absente !', _LOG_ERREUR);
1532
+
1533
+            return;
1534
+        }
1535
+        if (_IMG_GD_MAX_PIXELS && $srcWidth * $srcHeight > _IMG_GD_MAX_PIXELS) {
1536
+            spip_log('vignette gd1/gd2 impossible : ' . $srcWidth * $srcHeight . 'pixels');
1537
+
1538
+            return;
1539
+        }
1540
+        $destFormat = $format_sortie;
1541
+        if (!$destFormat) {
1542
+            spip_log("pas de format pour $image");
1543
+
1544
+            return;
1545
+        }
1546
+
1547
+        $fonction_imagecreatefrom = $valeurs['fonction_imagecreatefrom'];
1548
+        if (!function_exists($fonction_imagecreatefrom)) {
1549
+            return;
1550
+        }
1551
+        $srcImage = @$fonction_imagecreatefrom($image);
1552
+        if (!$srcImage) {
1553
+            spip_log('echec gd1/gd2');
1554
+
1555
+            return;
1556
+        }
1557
+
1558
+        // Initialisation de l'image destination
1559
+        $destImage = null;
1560
+        if ($process == 'gd2' and $destFormat != 'gif') {
1561
+            $destImage = ImageCreateTrueColor($destWidth, $destHeight);
1562
+        }
1563
+        if (!$destImage) {
1564
+            $destImage = ImageCreate($destWidth, $destHeight);
1565
+        }
1566
+
1567
+        // Recopie de l'image d'origine avec adaptation de la taille
1568
+        $ok = false;
1569
+        if (($process == 'gd2') and function_exists('ImageCopyResampled')) {
1570
+            if ($format == 'gif') {
1571
+                // Si un GIF est transparent,
1572
+                // fabriquer un PNG transparent
1573
+                $transp = imagecolortransparent($srcImage);
1574
+                if ($transp > 0) {
1575
+                    $destFormat = 'png';
1576
+                }
1577
+            }
1578
+            if (in_array($destFormat, _image_extensions_conservent_transparence())) {
1579
+                // Conserver la transparence
1580
+                if (function_exists('imageAntiAlias')) {
1581
+                    imageAntiAlias($destImage, true);
1582
+                }
1583
+                @imagealphablending($destImage, false);
1584
+                @imagesavealpha($destImage, true);
1585
+            }
1586
+            $ok = @ImageCopyResampled($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
1587
+        }
1588
+        if (!$ok) {
1589
+            $ok = ImageCopyResized($destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight);
1590
+        }
1591
+
1592
+        // Sauvegarde de l'image destination
1593
+        $valeurs['fichier_dest'] = $vignette = "$destination.$destFormat";
1594
+        $valeurs['format_dest'] = $format = $destFormat;
1595
+        _image_gd_output($destImage, $valeurs);
1596
+
1597
+        if ($srcImage) {
1598
+            ImageDestroy($srcImage);
1599
+        }
1600
+        ImageDestroy($destImage);
1601
+    }
1602
+
1603
+    if (!$vignette or !$size = @spip_getimagesize($vignette)) {
1604
+        $size = [$destWidth, $destHeight];
1605
+    }
1606
+
1607
+    // Gaffe: en safe mode, pas d'acces a la vignette,
1608
+    // donc risque de balancer "width='0'", ce qui masque l'image sous MSIE
1609
+    if ($size[0] < 1) {
1610
+        $size[0] = $destWidth;
1611
+    }
1612
+    if ($size[1] < 1) {
1613
+        $size[1] = $destHeight;
1614
+    }
1615
+
1616
+    $retour['width'] = $largeur = $size[0];
1617
+    $retour['height'] = $hauteur = $size[1];
1618
+
1619
+    $retour['fichier'] = $vignette;
1620
+    $retour['format'] = $format;
1621
+    $retour['date'] = @filemtime($vignette);
1622
+
1623
+    // renvoyer l'image
1624
+    return $retour;
1625 1625
 }
1626 1626
 
1627 1627
 /**
@@ -1641,25 +1641,25 @@  discard block
 block discarded – undo
1641 1641
  * @return array Liste [ largeur, hauteur, ratio de réduction ]
1642 1642
  **/
1643 1643
 function _image_ratio(int $srcWidth, int $srcHeight, int $maxWidth, int $maxHeight): array {
1644
-	$ratioWidth = $srcWidth / $maxWidth;
1645
-	$ratioHeight = $srcHeight / $maxHeight;
1646
-
1647
-	if ($srcWidth <= $maxWidth and $srcHeight <= $maxHeight) {
1648
-		$destWidth = $srcWidth;
1649
-		$destHeight = $srcHeight;
1650
-	} elseif ($ratioWidth < $ratioHeight) {
1651
-		$destWidth = $srcWidth / $ratioHeight;
1652
-		$destHeight = $maxHeight;
1653
-	} else {
1654
-		$destWidth = $maxWidth;
1655
-		$destHeight = $srcHeight / $ratioWidth;
1656
-	}
1657
-
1658
-	return [
1659
-		intval(round($destWidth)),
1660
-		intval(round($destHeight)),
1661
-		max($ratioWidth, $ratioHeight)
1662
-	];
1644
+    $ratioWidth = $srcWidth / $maxWidth;
1645
+    $ratioHeight = $srcHeight / $maxHeight;
1646
+
1647
+    if ($srcWidth <= $maxWidth and $srcHeight <= $maxHeight) {
1648
+        $destWidth = $srcWidth;
1649
+        $destHeight = $srcHeight;
1650
+    } elseif ($ratioWidth < $ratioHeight) {
1651
+        $destWidth = $srcWidth / $ratioHeight;
1652
+        $destHeight = $maxHeight;
1653
+    } else {
1654
+        $destWidth = $maxWidth;
1655
+        $destHeight = $srcHeight / $ratioWidth;
1656
+    }
1657
+
1658
+    return [
1659
+        intval(round($destWidth)),
1660
+        intval(round($destHeight)),
1661
+        max($ratioWidth, $ratioHeight)
1662
+    ];
1663 1663
 }
1664 1664
 
1665 1665
 /**
@@ -1679,25 +1679,25 @@  discard block
 block discarded – undo
1679 1679
  * @return array Liste [ largeur, hauteur, ratio de réduction ]
1680 1680
  **/
1681 1681
 function ratio_passe_partout(int $srcWidth, int $srcHeight, int $maxWidth, int $maxHeight): array {
1682
-	$ratioWidth = $srcWidth / $maxWidth;
1683
-	$ratioHeight = $srcHeight / $maxHeight;
1684
-
1685
-	if ($srcWidth <= $maxWidth and $srcHeight <= $maxHeight) {
1686
-		$destWidth = $srcWidth;
1687
-		$destHeight = $srcHeight;
1688
-	} elseif ($ratioWidth > $ratioHeight) {
1689
-		$destWidth = $srcWidth / $ratioHeight;
1690
-		$destHeight = $maxHeight;
1691
-	} else {
1692
-		$destWidth = $maxWidth;
1693
-		$destHeight = $srcHeight / $ratioWidth;
1694
-	}
1695
-
1696
-	return [
1697
-		intval(round($destWidth)),
1698
-		intval(round($destHeight)),
1699
-		min($ratioWidth, $ratioHeight)
1700
-	];
1682
+    $ratioWidth = $srcWidth / $maxWidth;
1683
+    $ratioHeight = $srcHeight / $maxHeight;
1684
+
1685
+    if ($srcWidth <= $maxWidth and $srcHeight <= $maxHeight) {
1686
+        $destWidth = $srcWidth;
1687
+        $destHeight = $srcHeight;
1688
+    } elseif ($ratioWidth > $ratioHeight) {
1689
+        $destWidth = $srcWidth / $ratioHeight;
1690
+        $destHeight = $maxHeight;
1691
+    } else {
1692
+        $destWidth = $maxWidth;
1693
+        $destHeight = $srcHeight / $ratioWidth;
1694
+    }
1695
+
1696
+    return [
1697
+        intval(round($destWidth)),
1698
+        intval(round($destHeight)),
1699
+        min($ratioWidth, $ratioHeight)
1700
+    ];
1701 1701
 }
1702 1702
 
1703 1703
 
@@ -1710,12 +1710,12 @@  discard block
 block discarded – undo
1710 1710
  * @return string
1711 1711
  */
1712 1712
 function process_image_svg_identite($image) {
1713
-	if ($image['creer']) {
1714
-		$source = $image['fichier'];
1715
-		_image_gd_output($source, $image);
1716
-	}
1713
+    if ($image['creer']) {
1714
+        $source = $image['fichier'];
1715
+        _image_gd_output($source, $image);
1716
+    }
1717 1717
 
1718
-	return _image_ecrire_tag($image, ['src' => $image['fichier_dest']]);
1718
+    return _image_ecrire_tag($image, ['src' => $image['fichier_dest']]);
1719 1719
 }
1720 1720
 
1721 1721
 
@@ -1748,109 +1748,109 @@  discard block
 block discarded – undo
1748 1748
  *     Code HTML de la balise img produite
1749 1749
  **/
1750 1750
 function process_image_reduire($fonction, $img, $taille, $taille_y, $force, $process = 'AUTO') {
1751
-	$image = false;
1752
-	if (($process == 'AUTO') and isset($GLOBALS['meta']['image_process'])) {
1753
-		$process = $GLOBALS['meta']['image_process'];
1754
-	}
1755
-	# determiner le format de sortie
1756
-	$format_sortie = false; // le choix par defaut sera bon
1757
-	if ($process == 'netpbm') {
1758
-		$format_sortie = 'jpg';
1759
-	} elseif ($process == 'gd1' or $process == 'gd2') {
1760
-		$image = _image_valeurs_trans($img, "reduire-{$taille}-{$taille_y}", $format_sortie, $fonction, false, _SVG_SUPPORTED);
1761
-		// on verifie que l'extension choisie est bonne (en principe oui)
1762
-		$gd_formats = formats_image_acceptables(true);
1763
-		if (
1764
-			is_array($image)
1765
-			and (!in_array($image['format_dest'], $gd_formats)
1766
-				or (!in_array($image['format_dest'], _image_extensions_acceptees_en_sortie()))
1767
-			)
1768
-		) {
1769
-			if ($image['format_source'] == 'jpg') {
1770
-				$formats_sortie = ['jpg', 'png', 'gif'];
1771
-			} else // les gif sont passes en png preferentiellement pour etre homogene aux autres filtres images
1772
-			{
1773
-				$formats_sortie = ['png', 'jpg', 'gif'];
1774
-			}
1775
-			// Choisir le format destination
1776
-			// - on sauve de preference en JPEG (meilleure compression)
1777
-			// - pour le GIF : les GD recentes peuvent le lire mais pas l'ecrire
1778
-			# bug : gd_formats contient la liste des fichiers qu'on sait *lire*,
1779
-			# pas *ecrire*
1780
-			$format_sortie = '';
1781
-			foreach ($formats_sortie as $fmt) {
1782
-				if (in_array($fmt, $gd_formats) and in_array($fmt, _image_extensions_acceptees_en_sortie())) {
1783
-					$format_sortie = $fmt;
1784
-					break;
1785
-				}
1786
-			}
1787
-			$image = false;
1788
-		}
1789
-	}
1790
-
1791
-	if (!is_array($image)) {
1792
-		$image = _image_valeurs_trans($img, "reduire-{$taille}-{$taille_y}", $format_sortie, $fonction, false, _SVG_SUPPORTED);
1793
-	}
1794
-
1795
-	if (!is_array($image) or !$image['largeur'] or !$image['hauteur']) {
1796
-		spip_log("image_reduire_src:pas de version locale de $img ou extension non prise en charge");
1797
-		// on peut resizer en mode html si on dispose des elements
1798
-		[$srcw, $srch] = taille_image($img);
1799
-		if ($srcw and $srch) {
1800
-			[$w, $h] = _image_ratio($srcw, $srch, $taille, $taille_y);
1801
-
1802
-			return _image_tag_changer_taille($img, $w, $h);
1803
-		}
1804
-		// la on n'a pas d'infos sur l'image source... on refile le truc a css
1805
-		// sous la forme style='max-width: NNpx;'
1806
-		return inserer_attribut(
1807
-			$img,
1808
-			'style',
1809
-			"max-width: {$taille}px;max-width: min(100%,{$taille}px); max-height: {$taille_y}px"
1810
-		);
1811
-	}
1812
-
1813
-	// si l'image est plus petite que la cible retourner une copie cachee de l'image
1814
-	if (($image['largeur'] <= $taille) && ($image['hauteur'] <= $taille_y)) {
1815
-		if ($image['creer']) {
1816
-			@copy($image['fichier'], $image['fichier_dest']);
1817
-		}
1818
-
1819
-		return _image_ecrire_tag($image, ['src' => $image['fichier_dest']]);
1820
-	}
1821
-
1822
-	if ($image['creer'] == false && !$force) {
1823
-		return _image_ecrire_tag(
1824
-			$image,
1825
-			['src' => $image['fichier_dest'], 'width' => $image['largeur_dest'], 'height' => $image['hauteur_dest']]
1826
-		);
1827
-	}
1828
-
1829
-	if (in_array($image['format_source'], _image_extensions_acceptees_en_entree())) {
1830
-		$destWidth = $image['largeur_dest'];
1831
-		$destHeight = $image['hauteur_dest'];
1832
-		$logo = $image['fichier'];
1833
-		$date = $image['date_src'];
1834
-		$preview = _image_creer_vignette($image, $taille, $taille_y, $process, $force);
1835
-
1836
-		if ($preview && $preview['fichier']) {
1837
-			$logo = $preview['fichier'];
1838
-			$destWidth = $preview['width'];
1839
-			$destHeight = $preview['height'];
1840
-			$date = $preview['date'];
1841
-		}
1842
-		// dans l'espace prive mettre un timestamp sur l'adresse
1843
-		// de l'image, de facon a tromper le cache du navigateur
1844
-		// quand on fait supprimer/reuploader un logo
1845
-		// (pas de filemtime si SAFE MODE)
1846
-		$date = test_espace_prive() ? ('?' . $date) : '';
1847
-
1848
-		return _image_ecrire_tag($image, ['src' => "$logo$date", 'width' => $destWidth, 'height' => $destHeight]);
1849
-	}
1850
-	else {
1851
-		# BMP, tiff ... les redacteurs osent tout!
1852
-		return $img;
1853
-	}
1751
+    $image = false;
1752
+    if (($process == 'AUTO') and isset($GLOBALS['meta']['image_process'])) {
1753
+        $process = $GLOBALS['meta']['image_process'];
1754
+    }
1755
+    # determiner le format de sortie
1756
+    $format_sortie = false; // le choix par defaut sera bon
1757
+    if ($process == 'netpbm') {
1758
+        $format_sortie = 'jpg';
1759
+    } elseif ($process == 'gd1' or $process == 'gd2') {
1760
+        $image = _image_valeurs_trans($img, "reduire-{$taille}-{$taille_y}", $format_sortie, $fonction, false, _SVG_SUPPORTED);
1761
+        // on verifie que l'extension choisie est bonne (en principe oui)
1762
+        $gd_formats = formats_image_acceptables(true);
1763
+        if (
1764
+            is_array($image)
1765
+            and (!in_array($image['format_dest'], $gd_formats)
1766
+                or (!in_array($image['format_dest'], _image_extensions_acceptees_en_sortie()))
1767
+            )
1768
+        ) {
1769
+            if ($image['format_source'] == 'jpg') {
1770
+                $formats_sortie = ['jpg', 'png', 'gif'];
1771
+            } else // les gif sont passes en png preferentiellement pour etre homogene aux autres filtres images
1772
+            {
1773
+                $formats_sortie = ['png', 'jpg', 'gif'];
1774
+            }
1775
+            // Choisir le format destination
1776
+            // - on sauve de preference en JPEG (meilleure compression)
1777
+            // - pour le GIF : les GD recentes peuvent le lire mais pas l'ecrire
1778
+            # bug : gd_formats contient la liste des fichiers qu'on sait *lire*,
1779
+            # pas *ecrire*
1780
+            $format_sortie = '';
1781
+            foreach ($formats_sortie as $fmt) {
1782
+                if (in_array($fmt, $gd_formats) and in_array($fmt, _image_extensions_acceptees_en_sortie())) {
1783
+                    $format_sortie = $fmt;
1784
+                    break;
1785
+                }
1786
+            }
1787
+            $image = false;
1788
+        }
1789
+    }
1790
+
1791
+    if (!is_array($image)) {
1792
+        $image = _image_valeurs_trans($img, "reduire-{$taille}-{$taille_y}", $format_sortie, $fonction, false, _SVG_SUPPORTED);
1793
+    }
1794
+
1795
+    if (!is_array($image) or !$image['largeur'] or !$image['hauteur']) {
1796
+        spip_log("image_reduire_src:pas de version locale de $img ou extension non prise en charge");
1797
+        // on peut resizer en mode html si on dispose des elements
1798
+        [$srcw, $srch] = taille_image($img);
1799
+        if ($srcw and $srch) {
1800
+            [$w, $h] = _image_ratio($srcw, $srch, $taille, $taille_y);
1801
+
1802
+            return _image_tag_changer_taille($img, $w, $h);
1803
+        }
1804
+        // la on n'a pas d'infos sur l'image source... on refile le truc a css
1805
+        // sous la forme style='max-width: NNpx;'
1806
+        return inserer_attribut(
1807
+            $img,
1808
+            'style',
1809
+            "max-width: {$taille}px;max-width: min(100%,{$taille}px); max-height: {$taille_y}px"
1810
+        );
1811
+    }
1812
+
1813
+    // si l'image est plus petite que la cible retourner une copie cachee de l'image
1814
+    if (($image['largeur'] <= $taille) && ($image['hauteur'] <= $taille_y)) {
1815
+        if ($image['creer']) {
1816
+            @copy($image['fichier'], $image['fichier_dest']);
1817
+        }
1818
+
1819
+        return _image_ecrire_tag($image, ['src' => $image['fichier_dest']]);
1820
+    }
1821
+
1822
+    if ($image['creer'] == false && !$force) {
1823
+        return _image_ecrire_tag(
1824
+            $image,
1825
+            ['src' => $image['fichier_dest'], 'width' => $image['largeur_dest'], 'height' => $image['hauteur_dest']]
1826
+        );
1827
+    }
1828
+
1829
+    if (in_array($image['format_source'], _image_extensions_acceptees_en_entree())) {
1830
+        $destWidth = $image['largeur_dest'];
1831
+        $destHeight = $image['hauteur_dest'];
1832
+        $logo = $image['fichier'];
1833
+        $date = $image['date_src'];
1834
+        $preview = _image_creer_vignette($image, $taille, $taille_y, $process, $force);
1835
+
1836
+        if ($preview && $preview['fichier']) {
1837
+            $logo = $preview['fichier'];
1838
+            $destWidth = $preview['width'];
1839
+            $destHeight = $preview['height'];
1840
+            $date = $preview['date'];
1841
+        }
1842
+        // dans l'espace prive mettre un timestamp sur l'adresse
1843
+        // de l'image, de facon a tromper le cache du navigateur
1844
+        // quand on fait supprimer/reuploader un logo
1845
+        // (pas de filemtime si SAFE MODE)
1846
+        $date = test_espace_prive() ? ('?' . $date) : '';
1847
+
1848
+        return _image_ecrire_tag($image, ['src' => "$logo$date", 'width' => $destWidth, 'height' => $destHeight]);
1849
+    }
1850
+    else {
1851
+        # BMP, tiff ... les redacteurs osent tout!
1852
+        return $img;
1853
+    }
1854 1854
 }
1855 1855
 
1856 1856
 /**
@@ -1864,145 +1864,145 @@  discard block
 block discarded – undo
1864 1864
  * Class phpthumb_functions
1865 1865
  */
1866 1866
 class phpthumb_functions {
1867
-	/**
1868
-	 * Retourne la couleur d'un pixel dans une image
1869
-	 *
1870
-	 * @param resource|GdImage $img
1871
-	 * @param int $x
1872
-	 * @param int $y
1873
-	 * @return array|bool
1874
-	 */
1875
-	public static function GetPixelColor(&$img, $x, $y) {
1876
-		if (is_resource($img) || (is_object($img) && $img instanceof \GdImage)) {
1877
-			return @ImageColorsForIndex($img, @ImageColorAt($img, $x, $y));
1878
-		}
1879
-		return false;
1880
-	}
1881
-
1882
-	/**
1883
-	 * Retourne un nombre dans une représentation en Little Endian
1884
-	 *
1885
-	 * @param int $number
1886
-	 * @param int $minbytes
1887
-	 * @return string
1888
-	 */
1889
-	public static function LittleEndian2String($number, $minbytes = 1) {
1890
-		$intstring = '';
1891
-		while ($number > 0) {
1892
-			$intstring = $intstring . chr($number & 255);
1893
-			$number >>= 8;
1894
-		}
1895
-
1896
-		return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
1897
-	}
1898
-
1899
-	/**
1900
-	 * Transforme une ressource GD en image au format ICO
1901
-	 *
1902
-	 * @param array $gd_image_array
1903
-	 *     Tableau de ressources d'images GD
1904
-	 * @return string
1905
-	 *     Image au format ICO
1906
-	 */
1907
-	public static function GD2ICOstring(&$gd_image_array) {
1908
-		foreach ($gd_image_array as $key => $gd_image) {
1909
-			$ImageWidths[$key] = ImageSX($gd_image);
1910
-			$ImageHeights[$key] = ImageSY($gd_image);
1911
-			$bpp[$key] = ImageIsTrueColor($gd_image) ? 32 : 24;
1912
-			$totalcolors[$key] = ImageColorsTotal($gd_image);
1913
-
1914
-			$icXOR[$key] = '';
1915
-			for ($y = $ImageHeights[$key] - 1; $y >= 0; $y--) {
1916
-				for ($x = 0; $x < $ImageWidths[$key]; $x++) {
1917
-					$argb = phpthumb_functions::GetPixelColor($gd_image, $x, $y);
1918
-					$a = round(255 * ((127 - $argb['alpha']) / 127));
1919
-					$r = $argb['red'];
1920
-					$g = $argb['green'];
1921
-					$b = $argb['blue'];
1922
-
1923
-					if ($bpp[$key] == 32) {
1924
-						$icXOR[$key] .= chr($b) . chr($g) . chr($r) . chr($a);
1925
-					} elseif ($bpp[$key] == 24) {
1926
-						$icXOR[$key] .= chr($b) . chr($g) . chr($r);
1927
-					}
1928
-
1929
-					if ($a < 128) {
1930
-						@$icANDmask[$key][$y] .= '1';
1931
-					} else {
1932
-						@$icANDmask[$key][$y] .= '0';
1933
-					}
1934
-				}
1935
-				// mask bits are 32-bit aligned per scanline
1936
-				while (strlen($icANDmask[$key][$y]) % 32) {
1937
-					$icANDmask[$key][$y] .= '0';
1938
-				}
1939
-			}
1940
-			$icAND[$key] = '';
1941
-			foreach ($icANDmask[$key] as $y => $scanlinemaskbits) {
1942
-				for ($i = 0; $i < strlen($scanlinemaskbits); $i += 8) {
1943
-					$icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, '0', STR_PAD_LEFT)));
1944
-				}
1945
-			}
1946
-		}
1947
-
1948
-		foreach ($gd_image_array as $key => $gd_image) {
1949
-			$biSizeImage = $ImageWidths[$key] * $ImageHeights[$key] * ($bpp[$key] / 8);
1950
-
1951
-			// BITMAPINFOHEADER - 40 bytes
1952
-			$BitmapInfoHeader[$key] = '';
1953
-			$BitmapInfoHeader[$key] .= "\x28\x00\x00\x00";                // DWORD  biSize;
1954
-			$BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageWidths[$key], 4);    // LONG   biWidth;
1955
-			// The biHeight member specifies the combined
1956
-			// height of the XOR and AND masks.
1957
-			$BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageHeights[$key] * 2, 4); // LONG   biHeight;
1958
-			$BitmapInfoHeader[$key] .= "\x01\x00";                    // WORD   biPlanes;
1959
-			$BitmapInfoHeader[$key] .= chr($bpp[$key]) . "\x00";              // wBitCount;
1960
-			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // DWORD  biCompression;
1961
-			$BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($biSizeImage, 4);      // DWORD  biSizeImage;
1962
-			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // LONG   biXPelsPerMeter;
1963
-			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // LONG   biYPelsPerMeter;
1964
-			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // DWORD  biClrUsed;
1965
-			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // DWORD  biClrImportant;
1966
-		}
1967
-
1968
-
1969
-		$icondata = "\x00\x00";                    // idReserved;   // Reserved (must be 0)
1970
-		$icondata .= "\x01\x00";                    // idType;	   // Resource Type (1 for icons)
1971
-		$icondata .= phpthumb_functions::LittleEndian2String(count($gd_image_array), 2);  // idCount;	  // How many images?
1972
-
1973
-		$dwImageOffset = 6 + (count($gd_image_array) * 16);
1974
-		foreach ($gd_image_array as $key => $gd_image) {
1975
-			// ICONDIRENTRY   idEntries[1]; // An entry for each image (idCount of 'em)
1976
-
1977
-			$icondata .= chr($ImageWidths[$key]);           // bWidth;		  // Width, in pixels, of the image
1978
-			$icondata .= chr($ImageHeights[$key]);          // bHeight;		 // Height, in pixels, of the image
1979
-			$icondata .= chr($totalcolors[$key]);           // bColorCount;	 // Number of colors in image (0 if >=8bpp)
1980
-			$icondata .= "\x00";                    // bReserved;	   // Reserved ( must be 0)
1981
-
1982
-			$icondata .= "\x01\x00";                  // wPlanes;		 // Color Planes
1983
-			$icondata .= chr($bpp[$key]) . "\x00";            // wBitCount;	   // Bits per pixel
1984
-
1985
-			$dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]);
1986
-			$icondata .= phpthumb_functions::LittleEndian2String(
1987
-				$dwBytesInRes,
1988
-				4
1989
-			);     // dwBytesInRes;	// How many bytes in this resource?
1990
-
1991
-			$icondata .= phpthumb_functions::LittleEndian2String(
1992
-				$dwImageOffset,
1993
-				4
1994
-			);    // dwImageOffset;   // Where in the file is this image?
1995
-			$dwImageOffset += strlen($BitmapInfoHeader[$key]);
1996
-			$dwImageOffset += strlen($icXOR[$key]);
1997
-			$dwImageOffset += strlen($icAND[$key]);
1998
-		}
1999
-
2000
-		foreach ($gd_image_array as $key => $gd_image) {
2001
-			$icondata .= $BitmapInfoHeader[$key];
2002
-			$icondata .= $icXOR[$key];
2003
-			$icondata .= $icAND[$key];
2004
-		}
2005
-
2006
-		return $icondata;
2007
-	}
1867
+    /**
1868
+     * Retourne la couleur d'un pixel dans une image
1869
+     *
1870
+     * @param resource|GdImage $img
1871
+     * @param int $x
1872
+     * @param int $y
1873
+     * @return array|bool
1874
+     */
1875
+    public static function GetPixelColor(&$img, $x, $y) {
1876
+        if (is_resource($img) || (is_object($img) && $img instanceof \GdImage)) {
1877
+            return @ImageColorsForIndex($img, @ImageColorAt($img, $x, $y));
1878
+        }
1879
+        return false;
1880
+    }
1881
+
1882
+    /**
1883
+     * Retourne un nombre dans une représentation en Little Endian
1884
+     *
1885
+     * @param int $number
1886
+     * @param int $minbytes
1887
+     * @return string
1888
+     */
1889
+    public static function LittleEndian2String($number, $minbytes = 1) {
1890
+        $intstring = '';
1891
+        while ($number > 0) {
1892
+            $intstring = $intstring . chr($number & 255);
1893
+            $number >>= 8;
1894
+        }
1895
+
1896
+        return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
1897
+    }
1898
+
1899
+    /**
1900
+     * Transforme une ressource GD en image au format ICO
1901
+     *
1902
+     * @param array $gd_image_array
1903
+     *     Tableau de ressources d'images GD
1904
+     * @return string
1905
+     *     Image au format ICO
1906
+     */
1907
+    public static function GD2ICOstring(&$gd_image_array) {
1908
+        foreach ($gd_image_array as $key => $gd_image) {
1909
+            $ImageWidths[$key] = ImageSX($gd_image);
1910
+            $ImageHeights[$key] = ImageSY($gd_image);
1911
+            $bpp[$key] = ImageIsTrueColor($gd_image) ? 32 : 24;
1912
+            $totalcolors[$key] = ImageColorsTotal($gd_image);
1913
+
1914
+            $icXOR[$key] = '';
1915
+            for ($y = $ImageHeights[$key] - 1; $y >= 0; $y--) {
1916
+                for ($x = 0; $x < $ImageWidths[$key]; $x++) {
1917
+                    $argb = phpthumb_functions::GetPixelColor($gd_image, $x, $y);
1918
+                    $a = round(255 * ((127 - $argb['alpha']) / 127));
1919
+                    $r = $argb['red'];
1920
+                    $g = $argb['green'];
1921
+                    $b = $argb['blue'];
1922
+
1923
+                    if ($bpp[$key] == 32) {
1924
+                        $icXOR[$key] .= chr($b) . chr($g) . chr($r) . chr($a);
1925
+                    } elseif ($bpp[$key] == 24) {
1926
+                        $icXOR[$key] .= chr($b) . chr($g) . chr($r);
1927
+                    }
1928
+
1929
+                    if ($a < 128) {
1930
+                        @$icANDmask[$key][$y] .= '1';
1931
+                    } else {
1932
+                        @$icANDmask[$key][$y] .= '0';
1933
+                    }
1934
+                }
1935
+                // mask bits are 32-bit aligned per scanline
1936
+                while (strlen($icANDmask[$key][$y]) % 32) {
1937
+                    $icANDmask[$key][$y] .= '0';
1938
+                }
1939
+            }
1940
+            $icAND[$key] = '';
1941
+            foreach ($icANDmask[$key] as $y => $scanlinemaskbits) {
1942
+                for ($i = 0; $i < strlen($scanlinemaskbits); $i += 8) {
1943
+                    $icAND[$key] .= chr(bindec(str_pad(substr($scanlinemaskbits, $i, 8), 8, '0', STR_PAD_LEFT)));
1944
+                }
1945
+            }
1946
+        }
1947
+
1948
+        foreach ($gd_image_array as $key => $gd_image) {
1949
+            $biSizeImage = $ImageWidths[$key] * $ImageHeights[$key] * ($bpp[$key] / 8);
1950
+
1951
+            // BITMAPINFOHEADER - 40 bytes
1952
+            $BitmapInfoHeader[$key] = '';
1953
+            $BitmapInfoHeader[$key] .= "\x28\x00\x00\x00";                // DWORD  biSize;
1954
+            $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageWidths[$key], 4);    // LONG   biWidth;
1955
+            // The biHeight member specifies the combined
1956
+            // height of the XOR and AND masks.
1957
+            $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageHeights[$key] * 2, 4); // LONG   biHeight;
1958
+            $BitmapInfoHeader[$key] .= "\x01\x00";                    // WORD   biPlanes;
1959
+            $BitmapInfoHeader[$key] .= chr($bpp[$key]) . "\x00";              // wBitCount;
1960
+            $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // DWORD  biCompression;
1961
+            $BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($biSizeImage, 4);      // DWORD  biSizeImage;
1962
+            $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // LONG   biXPelsPerMeter;
1963
+            $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // LONG   biYPelsPerMeter;
1964
+            $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // DWORD  biClrUsed;
1965
+            $BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // DWORD  biClrImportant;
1966
+        }
1967
+
1968
+
1969
+        $icondata = "\x00\x00";                    // idReserved;   // Reserved (must be 0)
1970
+        $icondata .= "\x01\x00";                    // idType;	   // Resource Type (1 for icons)
1971
+        $icondata .= phpthumb_functions::LittleEndian2String(count($gd_image_array), 2);  // idCount;	  // How many images?
1972
+
1973
+        $dwImageOffset = 6 + (count($gd_image_array) * 16);
1974
+        foreach ($gd_image_array as $key => $gd_image) {
1975
+            // ICONDIRENTRY   idEntries[1]; // An entry for each image (idCount of 'em)
1976
+
1977
+            $icondata .= chr($ImageWidths[$key]);           // bWidth;		  // Width, in pixels, of the image
1978
+            $icondata .= chr($ImageHeights[$key]);          // bHeight;		 // Height, in pixels, of the image
1979
+            $icondata .= chr($totalcolors[$key]);           // bColorCount;	 // Number of colors in image (0 if >=8bpp)
1980
+            $icondata .= "\x00";                    // bReserved;	   // Reserved ( must be 0)
1981
+
1982
+            $icondata .= "\x01\x00";                  // wPlanes;		 // Color Planes
1983
+            $icondata .= chr($bpp[$key]) . "\x00";            // wBitCount;	   // Bits per pixel
1984
+
1985
+            $dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]);
1986
+            $icondata .= phpthumb_functions::LittleEndian2String(
1987
+                $dwBytesInRes,
1988
+                4
1989
+            );     // dwBytesInRes;	// How many bytes in this resource?
1990
+
1991
+            $icondata .= phpthumb_functions::LittleEndian2String(
1992
+                $dwImageOffset,
1993
+                4
1994
+            );    // dwImageOffset;   // Where in the file is this image?
1995
+            $dwImageOffset += strlen($BitmapInfoHeader[$key]);
1996
+            $dwImageOffset += strlen($icXOR[$key]);
1997
+            $dwImageOffset += strlen($icAND[$key]);
1998
+        }
1999
+
2000
+        foreach ($gd_image_array as $key => $gd_image) {
2001
+            $icondata .= $BitmapInfoHeader[$key];
2002
+            $icondata .= $icXOR[$key];
2003
+            $icondata .= $icAND[$key];
2004
+        }
2005
+
2006
+        return $icondata;
2007
+    }
2008 2008
 }
Please login to merge, or discard this patch.
Spacing   +68 added lines, -68 removed lines patch added patch discarded remove patch
@@ -42,13 +42,13 @@  discard block
 block discarded – undo
42 42
 	$blue = dechex($blue);
43 43
 
44 44
 	if (strlen($red) == 1) {
45
-		$red = '0' . $red;
45
+		$red = '0'.$red;
46 46
 	}
47 47
 	if (strlen($green) == 1) {
48
-		$green = '0' . $green;
48
+		$green = '0'.$green;
49 49
 	}
50 50
 	if (strlen($blue) == 1) {
51
-		$blue = '0' . $blue;
51
+		$blue = '0'.$blue;
52 52
 	}
53 53
 
54 54
 	return "$red$green$blue";
@@ -67,7 +67,7 @@  discard block
 block discarded – undo
67 67
 	$couleur = couleur_html_to_hex($couleur);
68 68
 	$couleur = ltrim($couleur, '#');
69 69
 	if (strlen($couleur) === 3) {
70
-		$couleur = $couleur[0] . $couleur[0] . $couleur[1] . $couleur[1] . $couleur[2] . $couleur[2];
70
+		$couleur = $couleur[0].$couleur[0].$couleur[1].$couleur[1].$couleur[2].$couleur[2];
71 71
 	}
72 72
 	$retour = [];
73 73
 	$retour['red'] = hexdec(substr($couleur, 0, 2));
@@ -125,9 +125,9 @@  discard block
 block discarded – undo
125 125
 	$var_G = ($G / 255);
126 126
 	$var_B = ($B / 255);
127 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
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 131
 
132 132
 	$L = ($var_Max + $var_Min) / 2;
133 133
 
@@ -189,7 +189,7 @@  discard block
 block discarded – undo
189 189
  */
190 190
 function _couleur_hsl_to_rgb($H, $S, $L) {
191 191
 	// helper
192
-	$hue_2_rgb = function ($v1, $v2, $vH) {
192
+	$hue_2_rgb = function($v1, $v2, $vH) {
193 193
 		if ($vH < 0) {
194 194
 			$vH += 1;
195 195
 		}
@@ -327,11 +327,11 @@  discard block
 block discarded – undo
327 327
 		}
328 328
 	} elseif (
329 329
 		preg_match('@^data:image/([^;]*);base64,(.*)$@isS', $source, $regs)
330
-		and $extension = _image_trouver_extension_depuis_mime('image/' . $regs[1])
330
+		and $extension = _image_trouver_extension_depuis_mime('image/'.$regs[1])
331 331
 		and in_array($extension, _image_extensions_acceptees_en_entree())
332 332
 	) {
333 333
 		# gerer img src="data:....base64"
334
-		$local = sous_repertoire(_DIR_VAR, 'image-data') . md5($regs[2]) . '.' . _image_extension_normalisee($extension);
334
+		$local = sous_repertoire(_DIR_VAR, 'image-data').md5($regs[2]).'.'._image_extension_normalisee($extension);
335 335
 		if (!file_exists($local)) {
336 336
 			ecrire_fichier($local, base64_decode($regs[2]));
337 337
 		}
@@ -348,7 +348,7 @@  discard block
 block discarded – undo
348 348
 	// les protocoles web prennent au moins 3 lettres
349 349
 	if (tester_url_absolue($source)) {
350 350
 		include_spip('inc/distant');
351
-		$fichier = _DIR_RACINE . copie_locale($source);
351
+		$fichier = _DIR_RACINE.copie_locale($source);
352 352
 		if (!$fichier) {
353 353
 			return '';
354 354
 		}
@@ -450,9 +450,9 @@  discard block
 block discarded – undo
450 450
 			// on garde la terminaison initiale car image simplement copiee
451 451
 			// et on postfixe son nom avec un md5 du path
452 452
 			$terminaison_dest = $terminaison;
453
-			$fichier_dest .= '-' . substr(md5("$identifiant"), 0, 5);
453
+			$fichier_dest .= '-'.substr(md5("$identifiant"), 0, 5);
454 454
 		} else {
455
-			$fichier_dest .= '-' . substr(md5("$identifiant-$effet"), 0, 5);
455
+			$fichier_dest .= '-'.substr(md5("$identifiant-$effet"), 0, 5);
456 456
 		}
457 457
 		$cache = sous_repertoire(_DIR_VAR, $cache);
458 458
 		$cache = sous_repertoire($cache, $effet);
@@ -463,7 +463,7 @@  discard block
 block discarded – undo
463 463
 		$fichier_dest = substr($fichier_dest, 2);
464 464
 	}
465 465
 
466
-	$fichier_dest = $cache . $fichier_dest . '.' . $terminaison_dest;
466
+	$fichier_dest = $cache.$fichier_dest.'.'.$terminaison_dest;
467 467
 
468 468
 	$GLOBALS['images_calculees'][] = $fichier_dest;
469 469
 
@@ -490,7 +490,7 @@  discard block
 block discarded – undo
490 490
 	if ($creer) {
491 491
 		if (!@file_exists($fichier)) {
492 492
 			if (!@file_exists("$fichier.src")) {
493
-				spip_log("Image absente : $fichier", 'images' . _LOG_ERREUR);
493
+				spip_log("Image absente : $fichier", 'images'._LOG_ERREUR);
494 494
 
495 495
 				return false;
496 496
 			}
@@ -501,15 +501,15 @@  discard block
 block discarded – undo
501 501
 
502 502
 	if ($creer) {
503 503
 		spip_log(
504
-			'filtre image ' . ($fonction_creation ? reset($fonction_creation) : '') . "[$effet] sur $fichier",
505
-			'images' . _LOG_DEBUG
504
+			'filtre image '.($fonction_creation ? reset($fonction_creation) : '')."[$effet] sur $fichier",
505
+			'images'._LOG_DEBUG
506 506
 		);
507 507
 	}
508 508
 
509 509
 	$term_fonction = _image_trouver_extension_pertinente($fichier);
510
-	$ret['fonction_imagecreatefrom'] = '_imagecreatefrom' . $term_fonction;
510
+	$ret['fonction_imagecreatefrom'] = '_imagecreatefrom'.$term_fonction;
511 511
 	$ret['fichier'] = $fichier;
512
-	$ret['fonction_image'] = '_image_image' . $terminaison_dest;
512
+	$ret['fonction_image'] = '_image_image'.$terminaison_dest;
513 513
 	$ret['fichier_dest'] = $fichier_dest;
514 514
 	$ret['format_source'] = _image_extension_normalisee($terminaison);
515 515
 	$ret['format_dest'] = $terminaison_dest;
@@ -659,7 +659,7 @@  discard block
 block discarded – undo
659 659
 
660 660
 	$_terminaison = _image_trouver_extension_depuis_mime($mime);
661 661
 	if ($_terminaison and $_terminaison !== $terminaison) {
662
-		spip_log("Mauvaise extension du fichier : $path . Son type mime est : $mime", 'images.' . _LOG_INFO_IMPORTANTE);
662
+		spip_log("Mauvaise extension du fichier : $path . Son type mime est : $mime", 'images.'._LOG_INFO_IMPORTANTE);
663 663
 		$terminaison = $_terminaison;
664 664
 	}
665 665
 	return $terminaison;
@@ -816,7 +816,7 @@  discard block
 block discarded – undo
816 816
 	if (!function_exists('imagepng')) {
817 817
 		return false;
818 818
 	}
819
-	$tmp = $fichier . '.tmp';
819
+	$tmp = $fichier.'.tmp';
820 820
 	$ret = imagepng($img, $tmp);
821 821
 	if (file_exists($tmp)) {
822 822
 		$taille_test = @getimagesize($tmp);
@@ -851,7 +851,7 @@  discard block
 block discarded – undo
851 851
 	if (!function_exists('imagegif')) {
852 852
 		return false;
853 853
 	}
854
-	$tmp = $fichier . '.tmp';
854
+	$tmp = $fichier.'.tmp';
855 855
 	$ret = imagegif($img, $tmp);
856 856
 	if (file_exists($tmp)) {
857 857
 		$taille_test = @getimagesize($tmp);
@@ -891,7 +891,7 @@  discard block
 block discarded – undo
891 891
 	if (!function_exists('imagejpeg')) {
892 892
 		return false;
893 893
 	}
894
-	$tmp = $fichier . '.tmp';
894
+	$tmp = $fichier.'.tmp';
895 895
 
896 896
 	// Enable interlancing
897 897
 	imageinterlace($img, true);
@@ -952,7 +952,7 @@  discard block
 block discarded – undo
952 952
 	if (!function_exists('imagewebp')) {
953 953
 		return false;
954 954
 	}
955
-	$tmp = $fichier . '.tmp';
955
+	$tmp = $fichier.'.tmp';
956 956
 	$ret = imagewebp($img, $tmp, $qualite);
957 957
 	if (file_exists($tmp)) {
958 958
 		$taille_test = @getimagesize($tmp);
@@ -986,7 +986,7 @@  discard block
 block discarded – undo
986 986
  */
987 987
 function _image_imagesvg($img, $fichier) {
988 988
 
989
-	$tmp = $fichier . '.tmp';
989
+	$tmp = $fichier.'.tmp';
990 990
 	if (strpos($img, '<') === false) {
991 991
 		$img = supprimer_timestamp($img);
992 992
 		if (!file_exists($img)) {
@@ -1043,13 +1043,13 @@  discard block
 block discarded – undo
1043 1043
  */
1044 1044
 function _image_gd_output($img, $valeurs, $qualite = _IMG_GD_QUALITE, $fonction = null) {
1045 1045
 	if (is_null($fonction)) {
1046
-		$fonction = '_image_image' . $valeurs['format_dest'];
1046
+		$fonction = '_image_image'.$valeurs['format_dest'];
1047 1047
 	}
1048 1048
 	$ret = false;
1049 1049
 	#un flag pour reperer les images gravees
1050 1050
 	$lock = (
1051 1051
 		!statut_effacer_images_temporaires('get') // si la fonction n'a pas ete activee, on grave tout
1052
-		or (@file_exists($valeurs['fichier_dest']) and !@file_exists($valeurs['fichier_dest'] . '.src'))
1052
+		or (@file_exists($valeurs['fichier_dest']) and !@file_exists($valeurs['fichier_dest'].'.src'))
1053 1053
 	);
1054 1054
 	if (
1055 1055
 		function_exists($fonction)
@@ -1061,7 +1061,7 @@  discard block
 block discarded – undo
1061 1061
 			// dans tous les cas mettre a jour la taille de l'image finale
1062 1062
 			[$valeurs['hauteur_dest'], $valeurs['largeur_dest']] = taille_image($valeurs['fichier_dest']);
1063 1063
 			$valeurs['date'] = @filemtime($valeurs['fichier_dest']); // pour la retrouver apres disparition
1064
-			ecrire_fichier($valeurs['fichier_dest'] . '.src', serialize($valeurs), true);
1064
+			ecrire_fichier($valeurs['fichier_dest'].'.src', serialize($valeurs), true);
1065 1065
 		}
1066 1066
 	}
1067 1067
 
@@ -1240,7 +1240,7 @@  discard block
 block discarded – undo
1240 1240
 
1241 1241
 	// attributs deprecies. Transformer en CSS
1242 1242
 	if ($espace = extraire_attribut($tag, 'hspace')) {
1243
-		$style = "margin:{$espace}px;" . $style;
1243
+		$style = "margin:{$espace}px;".$style;
1244 1244
 		$tag = inserer_attribut($tag, 'hspace', '');
1245 1245
 	}
1246 1246
 
@@ -1367,7 +1367,7 @@  discard block
 block discarded – undo
1367 1367
 	$image = $valeurs['fichier'];
1368 1368
 	$format = $valeurs['format_source'];
1369 1369
 	$destdir = dirname($valeurs['fichier_dest']);
1370
-	$destfile = basename($valeurs['fichier_dest'], '.' . $valeurs['format_dest']);
1370
+	$destfile = basename($valeurs['fichier_dest'], '.'.$valeurs['format_dest']);
1371 1371
 
1372 1372
 	$format_sortie = $valeurs['format_dest'];
1373 1373
 
@@ -1399,7 +1399,7 @@  discard block
 block discarded – undo
1399 1399
 
1400 1400
 	// Si l'image est de la taille demandee (ou plus petite), simplement la retourner
1401 1401
 	if ($srcWidth and $srcWidth <= $maxWidth and $srcHeight <= $maxHeight) {
1402
-		$vignette = $destination . '.' . $format;
1402
+		$vignette = $destination.'.'.$format;
1403 1403
 		@copy($image, $vignette);
1404 1404
 	}
1405 1405
 
@@ -1407,7 +1407,7 @@  discard block
 block discarded – undo
1407 1407
 		include_spip('inc/svg');
1408 1408
 		if ($svg = svg_redimensionner($valeurs['fichier'], $destWidth, $destHeight)) {
1409 1409
 			$format_sortie = 'svg';
1410
-			$vignette = $destination . '.' . $format_sortie;
1410
+			$vignette = $destination.'.'.$format_sortie;
1411 1411
 			$valeurs['fichier_dest'] = $vignette;
1412 1412
 			_image_gd_output($svg, $valeurs);
1413 1413
 		}
@@ -1419,9 +1419,9 @@  discard block
 block discarded – undo
1419 1419
 			define('_CONVERT_COMMAND', 'convert');
1420 1420
 		} // Securite : mes_options.php peut preciser le chemin absolu
1421 1421
 		if (!defined('_RESIZE_COMMAND')) {
1422
-			define('_RESIZE_COMMAND', _CONVERT_COMMAND . ' -quality ' . _IMG_CONVERT_QUALITE . ' -orient Undefined -resize %xx%y! %src %dest');
1422
+			define('_RESIZE_COMMAND', _CONVERT_COMMAND.' -quality '._IMG_CONVERT_QUALITE.' -orient Undefined -resize %xx%y! %src %dest');
1423 1423
 		}
1424
-		$vignette = $destination . '.' . $format_sortie;
1424
+		$vignette = $destination.'.'.$format_sortie;
1425 1425
 		$commande = str_replace(
1426 1426
 			['%x', '%y', '%src', '%dest'],
1427 1427
 			[
@@ -1437,7 +1437,7 @@  discard block
 block discarded – undo
1437 1437
 		if (!@file_exists($vignette)) {
1438 1438
 			spip_log("echec convert sur $vignette");
1439 1439
 
1440
-			return;  // echec commande
1440
+			return; // echec commande
1441 1441
 		}
1442 1442
 	}
1443 1443
 
@@ -1454,7 +1454,7 @@  discard block
 block discarded – undo
1454 1454
 		if (!$output) {
1455 1455
 			return;
1456 1456
 		}
1457
-		$vignette = $output . DIRECTORY_SEPARATOR . basename($destination) . '.' . $format_sortie;
1457
+		$vignette = $output.DIRECTORY_SEPARATOR.basename($destination).'.'.$format_sortie;
1458 1458
 
1459 1459
 		$imagick = new Imagick();
1460 1460
 		$imagick->readImage(realpath($image));
@@ -1463,7 +1463,7 @@  discard block
 block discarded – undo
1463 1463
 			$destHeight,
1464 1464
 			Imagick::FILTER_LANCZOS,
1465 1465
 			1
1466
-		);//, IMAGICK_FILTER_LANCZOS, _IMG_IMAGICK_QUALITE / 100);
1466
+		); //, IMAGICK_FILTER_LANCZOS, _IMG_IMAGICK_QUALITE / 100);
1467 1467
 		$imagick->writeImage($vignette);
1468 1468
 
1469 1469
 		if (!@file_exists($vignette)) {
@@ -1472,7 +1472,7 @@  discard block
 block discarded – undo
1472 1472
 			return;
1473 1473
 		}
1474 1474
 		// remettre le chemin relatif car c'est ce qu'attend SPIP pour la suite (en particlier action/tester)
1475
-		$vignette = $destination . '.' . $format_sortie;
1475
+		$vignette = $destination.'.'.$format_sortie;
1476 1476
 	}
1477 1477
 
1478 1478
 	// netpbm
@@ -1483,11 +1483,11 @@  discard block
 block discarded – undo
1483 1483
 		if (_PNMSCALE_COMMAND == '') {
1484 1484
 			return;
1485 1485
 		}
1486
-		$vignette = $destination . '.' . $format_sortie;
1486
+		$vignette = $destination.'.'.$format_sortie;
1487 1487
 		$pnmtojpeg_command = str_replace('pnmscale', 'pnmtojpeg', _PNMSCALE_COMMAND);
1488 1488
 		if ($format == 'jpg') {
1489 1489
 			$jpegtopnm_command = str_replace('pnmscale', 'jpegtopnm', _PNMSCALE_COMMAND);
1490
-			exec("$jpegtopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette");
1490
+			exec("$jpegtopnm_command $image | "._PNMSCALE_COMMAND." -width $destWidth | $pnmtojpeg_command > $vignette");
1491 1491
 			if (!($s = @filesize($vignette))) {
1492 1492
 				spip_unlink($vignette);
1493 1493
 			}
@@ -1499,7 +1499,7 @@  discard block
 block discarded – undo
1499 1499
 		} else {
1500 1500
 			if ($format == 'gif') {
1501 1501
 				$giftopnm_command = str_replace('pnmscale', 'giftopnm', _PNMSCALE_COMMAND);
1502
-				exec("$giftopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette");
1502
+				exec("$giftopnm_command $image | "._PNMSCALE_COMMAND." -width $destWidth | $pnmtojpeg_command > $vignette");
1503 1503
 				if (!($s = @filesize($vignette))) {
1504 1504
 					spip_unlink($vignette);
1505 1505
 				}
@@ -1511,7 +1511,7 @@  discard block
 block discarded – undo
1511 1511
 			} else {
1512 1512
 				if ($format == 'png') {
1513 1513
 					$pngtopnm_command = str_replace('pnmscale', 'pngtopnm', _PNMSCALE_COMMAND);
1514
-					exec("$pngtopnm_command $image | " . _PNMSCALE_COMMAND . " -width $destWidth | $pnmtojpeg_command > $vignette");
1514
+					exec("$pngtopnm_command $image | "._PNMSCALE_COMMAND." -width $destWidth | $pnmtojpeg_command > $vignette");
1515 1515
 					if (!($s = @filesize($vignette))) {
1516 1516
 						spip_unlink($vignette);
1517 1517
 					}
@@ -1533,7 +1533,7 @@  discard block
 block discarded – undo
1533 1533
 			return;
1534 1534
 		}
1535 1535
 		if (_IMG_GD_MAX_PIXELS && $srcWidth * $srcHeight > _IMG_GD_MAX_PIXELS) {
1536
-			spip_log('vignette gd1/gd2 impossible : ' . $srcWidth * $srcHeight . 'pixels');
1536
+			spip_log('vignette gd1/gd2 impossible : '.$srcWidth * $srcHeight.'pixels');
1537 1537
 
1538 1538
 			return;
1539 1539
 		}
@@ -1843,7 +1843,7 @@  discard block
 block discarded – undo
1843 1843
 		// de l'image, de facon a tromper le cache du navigateur
1844 1844
 		// quand on fait supprimer/reuploader un logo
1845 1845
 		// (pas de filemtime si SAFE MODE)
1846
-		$date = test_espace_prive() ? ('?' . $date) : '';
1846
+		$date = test_espace_prive() ? ('?'.$date) : '';
1847 1847
 
1848 1848
 		return _image_ecrire_tag($image, ['src' => "$logo$date", 'width' => $destWidth, 'height' => $destHeight]);
1849 1849
 	}
@@ -1889,7 +1889,7 @@  discard block
 block discarded – undo
1889 1889
 	public static function LittleEndian2String($number, $minbytes = 1) {
1890 1890
 		$intstring = '';
1891 1891
 		while ($number > 0) {
1892
-			$intstring = $intstring . chr($number & 255);
1892
+			$intstring = $intstring.chr($number & 255);
1893 1893
 			$number >>= 8;
1894 1894
 		}
1895 1895
 
@@ -1921,9 +1921,9 @@  discard block
 block discarded – undo
1921 1921
 					$b = $argb['blue'];
1922 1922
 
1923 1923
 					if ($bpp[$key] == 32) {
1924
-						$icXOR[$key] .= chr($b) . chr($g) . chr($r) . chr($a);
1924
+						$icXOR[$key] .= chr($b).chr($g).chr($r).chr($a);
1925 1925
 					} elseif ($bpp[$key] == 24) {
1926
-						$icXOR[$key] .= chr($b) . chr($g) . chr($r);
1926
+						$icXOR[$key] .= chr($b).chr($g).chr($r);
1927 1927
 					}
1928 1928
 
1929 1929
 					if ($a < 128) {
@@ -1950,48 +1950,48 @@  discard block
 block discarded – undo
1950 1950
 
1951 1951
 			// BITMAPINFOHEADER - 40 bytes
1952 1952
 			$BitmapInfoHeader[$key] = '';
1953
-			$BitmapInfoHeader[$key] .= "\x28\x00\x00\x00";                // DWORD  biSize;
1954
-			$BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageWidths[$key], 4);    // LONG   biWidth;
1953
+			$BitmapInfoHeader[$key] .= "\x28\x00\x00\x00"; // DWORD  biSize;
1954
+			$BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageWidths[$key], 4); // LONG   biWidth;
1955 1955
 			// The biHeight member specifies the combined
1956 1956
 			// height of the XOR and AND masks.
1957 1957
 			$BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($ImageHeights[$key] * 2, 4); // LONG   biHeight;
1958
-			$BitmapInfoHeader[$key] .= "\x01\x00";                    // WORD   biPlanes;
1959
-			$BitmapInfoHeader[$key] .= chr($bpp[$key]) . "\x00";              // wBitCount;
1960
-			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // DWORD  biCompression;
1961
-			$BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($biSizeImage, 4);      // DWORD  biSizeImage;
1962
-			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // LONG   biXPelsPerMeter;
1963
-			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // LONG   biYPelsPerMeter;
1964
-			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // DWORD  biClrUsed;
1965
-			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00";                // DWORD  biClrImportant;
1958
+			$BitmapInfoHeader[$key] .= "\x01\x00"; // WORD   biPlanes;
1959
+			$BitmapInfoHeader[$key] .= chr($bpp[$key])."\x00"; // wBitCount;
1960
+			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD  biCompression;
1961
+			$BitmapInfoHeader[$key] .= phpthumb_functions::LittleEndian2String($biSizeImage, 4); // DWORD  biSizeImage;
1962
+			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // LONG   biXPelsPerMeter;
1963
+			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // LONG   biYPelsPerMeter;
1964
+			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD  biClrUsed;
1965
+			$BitmapInfoHeader[$key] .= "\x00\x00\x00\x00"; // DWORD  biClrImportant;
1966 1966
 		}
1967 1967
 
1968 1968
 
1969
-		$icondata = "\x00\x00";                    // idReserved;   // Reserved (must be 0)
1970
-		$icondata .= "\x01\x00";                    // idType;	   // Resource Type (1 for icons)
1971
-		$icondata .= phpthumb_functions::LittleEndian2String(count($gd_image_array), 2);  // idCount;	  // How many images?
1969
+		$icondata = "\x00\x00"; // idReserved;   // Reserved (must be 0)
1970
+		$icondata .= "\x01\x00"; // idType;	   // Resource Type (1 for icons)
1971
+		$icondata .= phpthumb_functions::LittleEndian2String(count($gd_image_array), 2); // idCount;	  // How many images?
1972 1972
 
1973 1973
 		$dwImageOffset = 6 + (count($gd_image_array) * 16);
1974 1974
 		foreach ($gd_image_array as $key => $gd_image) {
1975 1975
 			// ICONDIRENTRY   idEntries[1]; // An entry for each image (idCount of 'em)
1976 1976
 
1977
-			$icondata .= chr($ImageWidths[$key]);           // bWidth;		  // Width, in pixels, of the image
1978
-			$icondata .= chr($ImageHeights[$key]);          // bHeight;		 // Height, in pixels, of the image
1979
-			$icondata .= chr($totalcolors[$key]);           // bColorCount;	 // Number of colors in image (0 if >=8bpp)
1980
-			$icondata .= "\x00";                    // bReserved;	   // Reserved ( must be 0)
1977
+			$icondata .= chr($ImageWidths[$key]); // bWidth;		  // Width, in pixels, of the image
1978
+			$icondata .= chr($ImageHeights[$key]); // bHeight;		 // Height, in pixels, of the image
1979
+			$icondata .= chr($totalcolors[$key]); // bColorCount;	 // Number of colors in image (0 if >=8bpp)
1980
+			$icondata .= "\x00"; // bReserved;	   // Reserved ( must be 0)
1981 1981
 
1982
-			$icondata .= "\x01\x00";                  // wPlanes;		 // Color Planes
1983
-			$icondata .= chr($bpp[$key]) . "\x00";            // wBitCount;	   // Bits per pixel
1982
+			$icondata .= "\x01\x00"; // wPlanes;		 // Color Planes
1983
+			$icondata .= chr($bpp[$key])."\x00"; // wBitCount;	   // Bits per pixel
1984 1984
 
1985 1985
 			$dwBytesInRes = 40 + strlen($icXOR[$key]) + strlen($icAND[$key]);
1986 1986
 			$icondata .= phpthumb_functions::LittleEndian2String(
1987 1987
 				$dwBytesInRes,
1988 1988
 				4
1989
-			);     // dwBytesInRes;	// How many bytes in this resource?
1989
+			); // dwBytesInRes;	// How many bytes in this resource?
1990 1990
 
1991 1991
 			$icondata .= phpthumb_functions::LittleEndian2String(
1992 1992
 				$dwImageOffset,
1993 1993
 				4
1994
-			);    // dwImageOffset;   // Where in the file is this image?
1994
+			); // dwImageOffset;   // Where in the file is this image?
1995 1995
 			$dwImageOffset += strlen($BitmapInfoHeader[$key]);
1996 1996
 			$dwImageOffset += strlen($icXOR[$key]);
1997 1997
 			$dwImageOffset += strlen($icAND[$key]);
Please login to merge, or discard this patch.
Braces   +5 added lines, -11 removed lines patch added patch discarded remove patch
@@ -320,8 +320,7 @@  discard block
 block discarded – undo
320 320
 		if (strpos($img, '<img ') !== 0) {
321 321
 			$source = $img;
322 322
 			$img = "<img src='$source' />";
323
-		}
324
-		else {
323
+		} else {
325 324
 			// pas d'attribut src sur cette balise <img../>
326 325
 			return false;
327 326
 		}
@@ -547,8 +546,7 @@  discard block
 block discarded – undo
547 546
 			process_image_svg_identite($ret);
548 547
 			$ret['creer'] = false;
549 548
 		}
550
-	}
551
-	else {
549
+	} else {
552 550
 		if (!function_exists($ret['fonction_imagecreatefrom'])) {
553 551
 			return false;
554 552
 		}
@@ -652,8 +650,7 @@  discard block
 block discarded – undo
652 650
 
653 651
 	if (isset($info['mime'])) {
654 652
 		$mime = $info['mime'];
655
-	}
656
-	else {
653
+	} else {
657 654
 		$mime = image_type_to_mime_type($info[2]);
658 655
 	}
659 656
 
@@ -1401,9 +1398,7 @@  discard block
 block discarded – undo
1401 1398
 	if ($srcWidth and $srcWidth <= $maxWidth and $srcHeight <= $maxHeight) {
1402 1399
 		$vignette = $destination . '.' . $format;
1403 1400
 		@copy($image, $vignette);
1404
-	}
1405
-
1406
-	elseif ($valeurs['format_source'] === 'svg') {
1401
+	} elseif ($valeurs['format_source'] === 'svg') {
1407 1402
 		include_spip('inc/svg');
1408 1403
 		if ($svg = svg_redimensionner($valeurs['fichier'], $destWidth, $destHeight)) {
1409 1404
 			$format_sortie = 'svg';
@@ -1846,8 +1841,7 @@  discard block
 block discarded – undo
1846 1841
 		$date = test_espace_prive() ? ('?' . $date) : '';
1847 1842
 
1848 1843
 		return _image_ecrire_tag($image, ['src' => "$logo$date", 'width' => $destWidth, 'height' => $destHeight]);
1849
-	}
1850
-	else {
1844
+	} else {
1851 1845
 		# BMP, tiff ... les redacteurs osent tout!
1852 1846
 		return $img;
1853 1847
 	}
Please login to merge, or discard this patch.