From 0fff59d4977aae8df6c78a1604beeb27982f1f49 Mon Sep 17 00:00:00 2001
From: Matthieu Marcillaud <marcimat@rezo.net>
Date: Tue, 21 Mar 2023 00:08:04 +0100
Subject: [PATCH] remove: Pas de test sur GD1

Refs: #5258
---
 ecrire/action/tester.php                   | 163 ++++++++-------------
 ecrire/inc/filtres_images_lib_mini.php     |  26 ++--
 ecrire/inc/filtres_images_mini.php         |   4 +-
 ecrire/inc/utils.php                       |   8 +-
 prive/formulaires/configurer_reducteur.php |  33 ++---
 5 files changed, 86 insertions(+), 148 deletions(-)

diff --git a/ecrire/action/tester.php b/ecrire/action/tester.php
index 774371737a..436d6418e2 100644
--- a/ecrire/action/tester.php
+++ b/ecrire/action/tester.php
@@ -23,7 +23,7 @@ if (!defined('_ECRIRE_INC_VERSION')) {
  * Tester les capacités du serveur à utiliser une librairie graphique
  *
  * L'argument transmis dans la clé `arg` est le type de librairie parmi
- * gd2, gd1, netpbm, imagick ou convert
+ * gd2, netpbm, imagick ou convert
  *
  * L'action crée une vignette en utilisant la librairie indiquée puis
  * redirige sur l'image ainsi créée (sinon sur une image d'echec).
@@ -31,25 +31,14 @@ if (!defined('_ECRIRE_INC_VERSION')) {
 function action_tester_dist() {
 	$arg = _request('arg');
 
-	$gd_formats = [];
-	$gd_formats_read_gif = '';
 	// verifier les formats acceptes par GD
-	if ($arg == 'gd1') {
-		// Si GD est installe et php >= 4.0.2
+	if ($arg === 'gd2') {
+		$gd_formats = [];
+
 		if (function_exists('imagetypes')) {
 			if (imagetypes() & IMG_GIF) {
 				$gd_formats[] = 'gif';
-			} else {
-				# Attention GD sait lire le gif mais pas forcement l'ecrire
-				if (function_exists('ImageCreateFromGIF')) {
-					$srcImage = @ImageCreateFromGIF(_ROOT_IMG_PACK . 'test.gif');
-					if ($srcImage) {
-						$gd_formats_read_gif = ',gif';
-						ImageDestroy($srcImage);
-					}
-				}
 			}
-
 			if (imagetypes() & IMG_JPG) {
 				$gd_formats[] = 'jpg';
 			}
@@ -59,104 +48,67 @@ function action_tester_dist() {
 			if (imagetypes() & IMG_WEBP) {
 				$gd_formats[] = 'webp';
 			}
-		} else {
-			# ancienne methode de detection des formats, qui en plus
-			# est bugguee car elle teste les formats en lecture
-			# alors que la valeur deduite sert a identifier
-			# les formats disponibles en ecriture... (cf. inc_logos)
-
-			if (function_exists('ImageCreateFromJPEG')) {
-				$srcImage = @ImageCreateFromJPEG(_ROOT_IMG_PACK . 'test.jpg');
-				if ($srcImage) {
-					$gd_formats[] = 'jpg';
-					ImageDestroy($srcImage);
-				}
-			}
-			if (function_exists('ImageCreateFromGIF')) {
-				$srcImage = @ImageCreateFromGIF(_ROOT_IMG_PACK . 'test.gif');
-				if ($srcImage) {
-					$gd_formats[] = 'gif';
-					ImageDestroy($srcImage);
-				}
-			}
-			if (function_exists('ImageCreateFromPNG')) {
-				$srcImage = @ImageCreateFromPNG(_ROOT_IMG_PACK . 'test.png');
-				if ($srcImage) {
-					$gd_formats[] = 'png';
-					ImageDestroy($srcImage);
-				}
-			}
-			if (function_exists('ImageCreateFromWEBP')) {
-				$srcImage = @ImageCreateFromWEBP(_ROOT_IMG_PACK . 'test.webp');
-				if ($srcImage) {
-					$gd_formats[] = 'webp';
-					ImageDestroy($srcImage);
-				}
-			}
 		}
 
-		if (! empty($gd_formats)) {
-			$gd_formats = implode(',', $gd_formats);
-		}
-		ecrire_meta('gd_formats_read', $gd_formats . $gd_formats_read_gif);
+		$gd_formats = implode(',', $gd_formats);
+		ecrire_meta('gd_formats_read', $gd_formats);
 		ecrire_meta('gd_formats', $gd_formats);
-	} // verifier les formats netpbm
-	else {
-		if ($arg == 'netpbm') {
-			if (!defined('_PNMSCALE_COMMAND')) {
-				define('_PNMSCALE_COMMAND', 'pnmscale');
-			} // Securite : mes_options.php peut preciser le chemin absolu
-			if (_PNMSCALE_COMMAND == '') {
-				return;
-			}
-			$netpbm_formats = [];
-
-			$jpegtopnm_command = str_replace(
-				'pnmscale',
-				'jpegtopnm',
-				_PNMSCALE_COMMAND
-			);
-			$pnmtojpeg_command = str_replace(
-				'pnmscale',
-				'pnmtojpeg',
-				_PNMSCALE_COMMAND
-			);
-
-			$vignette = _ROOT_IMG_PACK . 'test.jpg';
-			$dest = _DIR_VAR . 'test-jpg.jpg';
-			$commande = "$jpegtopnm_command $vignette | " . _PNMSCALE_COMMAND . " -width 10 | $pnmtojpeg_command > $dest";
-			spip_log($commande);
-			exec($commande);
-			if (($taille = @getimagesize($dest)) && $taille[1] == 10) {
-				$netpbm_formats[] = 'jpg';
-			}
-			$giftopnm_command = str_replace('pnmscale', 'giftopnm', _PNMSCALE_COMMAND);
-			$pnmtojpeg_command = str_replace('pnmscale', 'pnmtojpeg', _PNMSCALE_COMMAND);
-			$vignette = _ROOT_IMG_PACK . 'test.gif';
-			$dest = _DIR_VAR . 'test-gif.jpg';
-			$commande = "$giftopnm_command $vignette | " . _PNMSCALE_COMMAND . " -width 10 | $pnmtojpeg_command > $dest";
-			spip_log($commande);
-			exec($commande);
-			if (($taille = @getimagesize($dest)) && $taille[1] == 10) {
-				$netpbm_formats[] = 'gif';
-			}
 
-			$pngtopnm_command = str_replace('pnmscale', 'pngtopnm', _PNMSCALE_COMMAND);
-			$vignette = _ROOT_IMG_PACK . 'test.png';
-			$dest = _DIR_VAR . 'test-gif.jpg';
-			$commande = "$pngtopnm_command $vignette | " . _PNMSCALE_COMMAND . " -width 10 | $pnmtojpeg_command > $dest";
-			spip_log($commande);
-			exec($commande);
-			if (($taille = @getimagesize($dest)) && $taille[1] == 10) {
-				$netpbm_formats[] = 'png';
-			}
+	} elseif ($arg == 'netpbm') {
+		// verifier les formats netpbm
+		if (!defined('_PNMSCALE_COMMAND')) {
+			define('_PNMSCALE_COMMAND', 'pnmscale');
+		} // Securite : mes_options.php peut preciser le chemin absolu
+		if (_PNMSCALE_COMMAND == '') {
+			return;
+		}
+		$netpbm_formats = [];
+
+		$jpegtopnm_command = str_replace(
+			'pnmscale',
+			'jpegtopnm',
+			_PNMSCALE_COMMAND
+		);
+		$pnmtojpeg_command = str_replace(
+			'pnmscale',
+			'pnmtojpeg',
+			_PNMSCALE_COMMAND
+		);
+
+		$vignette = _ROOT_IMG_PACK . 'test.jpg';
+		$dest = _DIR_VAR . 'test-jpg.jpg';
+		$commande = "$jpegtopnm_command $vignette | " . _PNMSCALE_COMMAND . " -width 10 | $pnmtojpeg_command > $dest";
+		spip_log($commande);
+		exec($commande);
+		if (($taille = @getimagesize($dest)) && $taille[1] == 10) {
+			$netpbm_formats[] = 'jpg';
+		}
+		$giftopnm_command = str_replace('pnmscale', 'giftopnm', _PNMSCALE_COMMAND);
+		$pnmtojpeg_command = str_replace('pnmscale', 'pnmtojpeg', _PNMSCALE_COMMAND);
+		$vignette = _ROOT_IMG_PACK . 'test.gif';
+		$dest = _DIR_VAR . 'test-gif.jpg';
+		$commande = "$giftopnm_command $vignette | " . _PNMSCALE_COMMAND . " -width 10 | $pnmtojpeg_command > $dest";
+		spip_log($commande);
+		exec($commande);
+		if (($taille = @getimagesize($dest)) && $taille[1] == 10) {
+			$netpbm_formats[] = 'gif';
+		}
 
-			ecrire_meta('netpbm_formats', implode(',', $netpbm_formats ?: []));
+		$pngtopnm_command = str_replace('pnmscale', 'pngtopnm', _PNMSCALE_COMMAND);
+		$vignette = _ROOT_IMG_PACK . 'test.png';
+		$dest = _DIR_VAR . 'test-gif.jpg';
+		$commande = "$pngtopnm_command $vignette | " . _PNMSCALE_COMMAND . " -width 10 | $pnmtojpeg_command > $dest";
+		spip_log($commande);
+		exec($commande);
+		if (($taille = @getimagesize($dest)) && $taille[1] == 10) {
+			$netpbm_formats[] = 'png';
 		}
+
+		ecrire_meta('netpbm_formats', implode(',', $netpbm_formats ?: []));
 	}
 
 	// et maintenant envoyer la vignette de tests
-	if (in_array($arg, ['gd1', 'gd2', 'imagick', 'convert', 'netpbm'])) {
+	if (in_array($arg, ['gd2', 'imagick', 'convert', 'netpbm'])) {
 		include_spip('inc/filtres');
 		include_spip('inc/filtres_images_mini');
 		$taille_preview = 150;
@@ -165,7 +117,8 @@ function action_tester_dist() {
 		$image['fichier_dest'] = _DIR_VAR . "test_$arg";
 
 		if (
-			($preview = _image_creer_vignette($image, $taille_preview, $taille_preview, $arg, true)) && $preview['width'] * $preview['height'] > 0
+			($preview = _image_creer_vignette($image, $taille_preview, $taille_preview, $arg, true))
+			&& $preview['width'] * $preview['height'] > 0
 		) {
 			redirige_par_entete($preview['fichier']);
 		}
diff --git a/ecrire/inc/filtres_images_lib_mini.php b/ecrire/inc/filtres_images_lib_mini.php
index 4064eea8d1..d4da2b2c9c 100644
--- a/ecrire/inc/filtres_images_lib_mini.php
+++ b/ecrire/inc/filtres_images_lib_mini.php
@@ -549,10 +549,9 @@ function _image_valeurs_trans($img, $effet, $forcer_format = false, $fonction_cr
 
 
 /**
- * @param string $quoi
- * @return array
+ * Extensions d’images acceptées en entrée
  */
-function _image_extensions_acceptees_en_entree() {
+function _image_extensions_acceptees_en_entree(): array {
 	static $extensions = null;
 	if (empty($extensions)) {
 		$extensions = ['png', 'gif', 'jpg', 'jpeg'];
@@ -573,9 +572,9 @@ function _image_extensions_acceptees_en_entree() {
 }
 
 /**
- * @return array|string[]|null
+ * Extensions d’images acceptées en sortie
  */
-function _image_extensions_acceptees_en_sortie() {
+function _image_extensions_acceptees_en_sortie(): array {
 	static $extensions = null;
 	if (empty($extensions)) {
 		$extensions = _image_extensions_acceptees_en_entree();
@@ -1331,7 +1330,7 @@ function _image_creer_vignette($valeurs, $maxWidth, $maxHeight, $process = 'AUTO
 	}
 
 	// si le doc n'est pas une image dans un format accetpable, refuser
-	if (!$force && !in_array($format, formats_image_acceptables(in_array($process, ['gd1', 'gd2'])))) {
+	if (!$force && !in_array($format, formats_image_acceptables($process === 'gd2'))) {
 		return;
 	}
 	$destination = "$destdir/$destfile";
@@ -1481,14 +1480,14 @@ function _image_creer_vignette($valeurs, $maxWidth, $maxHeight, $process = 'AUTO
 	}
 
 	// gd ou gd2
-	elseif ($process == 'gd1' || $process == 'gd2') {
+	elseif ($process === 'gd2') {
 		if (!function_exists('gd_info')) {
 			spip_log('Librairie GD absente !', _LOG_ERREUR);
 
 			return;
 		}
 		if (_IMG_GD_MAX_PIXELS && $srcWidth * $srcHeight > _IMG_GD_MAX_PIXELS) {
-			spip_log('vignette gd1/gd2 impossible : ' . $srcWidth * $srcHeight . 'pixels');
+			spip_log('vignette gd2 impossible : ' . $srcWidth * $srcHeight . 'pixels');
 
 			return;
 		}
@@ -1505,7 +1504,7 @@ function _image_creer_vignette($valeurs, $maxWidth, $maxHeight, $process = 'AUTO
 		}
 		$srcImage = @$fonction_imagecreatefrom($image);
 		if (!$srcImage) {
-			spip_log('echec gd1/gd2');
+			spip_log('echec gd2');
 
 			return;
 		}
@@ -1697,7 +1696,7 @@ function process_image_svg_identite($image) {
  *     Hauteur désirée
  * @param bool $force
  * @param string $process
- *     Librairie graphique à utiliser (gd1, gd2, netpbm, convert, imagick).
+ *     Librairie graphique à utiliser (gd2, netpbm, convert, imagick).
  *     AUTO utilise la librairie sélectionnée dans la configuration.
  * @return string
  *     Code HTML de la balise img produite
@@ -1709,14 +1708,15 @@ function process_image_reduire($fonction, $img, $taille, $taille_y, $force, $pro
 	}
 	# determiner le format de sortie
 	$format_sortie = false; // le choix par defaut sera bon
-	if ($process == 'netpbm') {
+	if ($process === 'netpbm') {
 		$format_sortie = 'jpg';
-	} elseif ($process == 'gd1' || $process == 'gd2') {
+	} elseif ($process === 'gd2') {
 		$image = _image_valeurs_trans($img, "reduire-{$taille}-{$taille_y}", $format_sortie, $fonction, false, _SVG_SUPPORTED);
 		// on verifie que l'extension choisie est bonne (en principe oui)
 		$gd_formats = formats_image_acceptables(true);
 		if (
-			is_array($image) && (!in_array($image['format_dest'], $gd_formats) || !in_array($image['format_dest'], _image_extensions_acceptees_en_sortie()))
+			is_array($image)
+			&& (!in_array($image['format_dest'], $gd_formats) || !in_array($image['format_dest'], _image_extensions_acceptees_en_sortie()))
 		) {
 			$formats_sortie = $image['format_source'] == 'jpg' ? ['jpg', 'png', 'gif'] : ['png', 'jpg', 'gif'];
 			// Choisir le format destination
diff --git a/ecrire/inc/filtres_images_mini.php b/ecrire/inc/filtres_images_mini.php
index afef155567..1f595bf2e5 100644
--- a/ecrire/inc/filtres_images_mini.php
+++ b/ecrire/inc/filtres_images_mini.php
@@ -365,7 +365,7 @@ function image_select($img, $width_min = 0, $height_min = 0, $width_max = 10000,
  * @param bool $cherche_image
  *     Inutilisé
  * @param string $process
- *     Librairie graphique à utiliser (gd1, gd2, netpbm, convert, imagick).
+ *     Librairie graphique à utiliser (gd2, netpbm, convert, imagick).
  *     AUTO utilise la librairie sélectionnée dans la configuration.
  * @return string
  *     Code HTML de l'image ou du texte.
@@ -446,7 +446,7 @@ function image_passe_partout(
  * @param bool $cherche_image
  *     Inutilisé
  * @param string $process
- *     Librairie graphique à utiliser (gd1, gd2, netpbm, convert, imagick).
+ *     Librairie graphique à utiliser (gd2, netpbm, convert, imagick).
  *     AUTO utilise la librairie sélectionnée dans la configuration.
  * @return string
  *     Code HTML de l'image ou du texte.
diff --git a/ecrire/inc/utils.php b/ecrire/inc/utils.php
index 7c3775cd89..778aff80d3 100644
--- a/ecrire/inc/utils.php
+++ b/ecrire/inc/utils.php
@@ -3597,11 +3597,8 @@ function html5_permis() {
 
 /**
  * Lister les formats image acceptes par les lib et fonctions images
- * @param bool $gd
- * @param bool $svg_allowed
- * @return array
  */
-function formats_image_acceptables($gd = null, $svg_allowed = true) {
+function formats_image_acceptables(?bool $gd = null, bool $svg_allowed = true): array {
 	$formats = null;
 	if (!is_null($gd)) {
 		$config = ($gd ? 'gd_formats' : 'formats_graphiques');
@@ -3621,8 +3618,7 @@ function formats_image_acceptables($gd = null, $svg_allowed = true) {
 		if (!in_array('svg', $formats)) {
 			$formats[] = 'svg';
 		}
-	}
-	else {
+	} else {
 		$formats = array_diff($formats, ['svg']);
 	}
 	return $formats;
diff --git a/prive/formulaires/configurer_reducteur.php b/prive/formulaires/configurer_reducteur.php
index a2963322c9..47f6ef3eb5 100644
--- a/prive/formulaires/configurer_reducteur.php
+++ b/prive/formulaires/configurer_reducteur.php
@@ -112,28 +112,17 @@ function formulaires_configurer_reducteur_traiter_dist() {
  * @return string
  *     URL d'action pour tester la librairie graphique en créant une vignette
  **/
-function url_vignette_choix($process) {
-	switch ($process) {
-		case 'gd2':
-			if (!function_exists('ImageCreateTrueColor')) {
-				return '';
-			}
-			break;
-		case 'netpbm':
-			if (defined('_PNMSCALE_COMMAND') && _PNMSCALE_COMMAND == '') {
-				return '';
-			}
-			break;
-		case 'imagick':
-			if (!method_exists(\Imagick::class, 'readImage')) {
-				return '';
-			}
-			break;
-		case 'convert':
-			if (defined('_CONVERT_COMMAND') && _CONVERT_COMMAND == '') {
-				return '';
-			}
-			break;
+function url_vignette_choix(string $process): string {
+	$ok = match ($process) {
+		'gd2' => function_exists('ImageCreateTrueColor'),
+		'netpbm' => !(defined('_PNMSCALE_COMMAND') && _PNMSCALE_COMMAND == ''),
+		'imagick' => method_exists(\Imagick::class, 'readImage'),
+		'convert' => !(defined('_CONVERT_COMMAND') && _CONVERT_COMMAND == ''),
+		default => false,
+	};
+
+	if (!$ok) {
+		return '';
 	}
 
 	return generer_url_action('tester', "arg=$process&time=" . time());
-- 
GitLab