Subversion Repositories eFlore/Applications.cel

Rev

Rev 2461 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2461 Rev 2836
1
<?php
1
<?php
2
// declare(encoding='UTF-8');
2
// declare(encoding='UTF-8');
3
/**
3
/**
4
 * Service fournissant permettant de visualiser ou télécharger des images du CEL.
4
 * Service fournissant permettant de visualiser ou télécharger des images du CEL.
5
 *
5
 *
6
 * @internal   Mininum PHP version : 5.2
6
 * @internal   Mininum PHP version : 5.2
7
 * @category   CEL
7
 * @category   CEL
8
 * @package    Services
8
 * @package    Services
9
 * @subpackage Images
9
 * @subpackage Images
10
 * @version    0.1
10
 * @version    0.1
11
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
11
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
12
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
12
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
13
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
13
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
14
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
14
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
15
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
15
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
16
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
16
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
17
 */
17
 */
18
class CelImageFormat {
18
class CelImageFormat {
19
 
19
 
20
	private $config;
20
	private $config;
-
 
21
	// @TODO tirer ça de la config
21
	private $formats = array('CRX2S', 'CRXS', 'CXS', 'CS', 'CRS', 'XS', 'S', 'M', 'L', 'XL', 'X2L', 'X3L', 'O');
22
	private $formats = array('CRX2S', 'CRXS', 'CXS', 'CS', 'CRS', 'XS', 'S', 'M', 'L', 'CRL', 'XL', 'X2L', 'X3L', 'O');
22
	const METHODE_TELECHARGEMENT = 'telecharger';
23
	const METHODE_TELECHARGEMENT = 'telecharger';
23
	const METHODE_AFFICHAGE = 'afficher';
24
	const METHODE_AFFICHAGE = 'afficher';
24
 
25
 
25
	// Pas besoin d'étendre Cel ici, surtout que le constructeur
26
	// Pas besoin d'étendre Cel ici, surtout que le constructeur
26
	// de la classe Cel instancie toujours une connexion à la bdd
27
	// de la classe Cel instancie toujours une connexion à la bdd
27
	// dont on a pas besoin ici. Ceci évite de planter le service
28
	// dont on a pas besoin ici. Ceci évite de planter le service
28
	// quand la bdd est surchargée.
29
	// quand la bdd est surchargée.
29
	public function __construct($config) {
30
	public function __construct($config) {
30
		$this->config = $config;
31
		$this->config = $config;
31
	}
32
	}
32
 
33
 
33
	public function getRessource() {
34
	public function getRessource() {
34
		header('Content-Type: application/json');
35
		header('Content-Type: application/json');
35
		echo json_encode($this->obtenirDescriptionService());
36
		echo json_encode($this->obtenirDescriptionService());
36
	}
37
	}
37
 
38
 
38
	/**
39
	/**
39
	* Méthode appelée avec une requête de type GET.
40
	* Méthode appelée avec une requête de type GET.
40
	*/
41
	*/
41
	public function getElement($params) {
42
	public function getElement($params) {
42
		// suppression des 0 non significatifs à gauche
43
		// suppression des 0 non significatifs à gauche
43
		$id = ltrim($params[0], '0');
44
		$id = ltrim($params[0], '0');
44
		$format = isset($_GET['format']) ? $_GET['format'] : 'M';
45
		$format = isset($_GET['format']) ? $_GET['format'] : 'M';
45
		$methode_livraison = isset($_GET['methode']) ? $_GET['methode'] : self::METHODE_AFFICHAGE;
46
		$methode_livraison = isset($_GET['methode']) ? $_GET['methode'] : self::METHODE_AFFICHAGE;
46
 
47
 
47
		if($this->verifierParametres($id, $format, $methode_livraison)) {
48
		if($this->verifierParametres($id, $format, $methode_livraison)) {
48
			$gestion_formats_images = new ImageRecreation($this->config);
49
			$gestion_formats_images = new ImageRecreation($this->config);
49
			$image_binaire = $gestion_formats_images->creerOuRenvoyerImage($params[0], $format);
50
			$image_binaire = $gestion_formats_images->creerOuRenvoyerImage($params[0], $format);
50
 
51
 
51
			if($image_binaire) {
52
			if($image_binaire) {
52
				$this->envoyerImage($id, $image_binaire, $format, $methode_livraison);
53
				$this->envoyerImage($id, $image_binaire, $format, $methode_livraison);
53
			} else {
54
			} else {
54
				header("HTTP/1.0 404 Not Found");
55
				header("HTTP/1.0 404 Not Found");
55
				echo 'Aucune image ne correspond à cet identifiant';
56
				echo 'Aucune image ne correspond à cet identifiant';
56
			}
57
			}
57
		}
58
		}
58
	}
59
	}
59
 
60
 
60
	private function verifierParametres($id, $format, $methode_livraison) {
61
	private function verifierParametres($id, $format, $methode_livraison) {
61
		$ok = true;
62
		$ok = true;
62
		$message = '';
63
		$message = '';
63
		if(!is_numeric($id)) {
64
		if(!is_numeric($id)) {
64
			$message .= "L'identifiant de format doit être un entier. ";
65
			$message .= "L'identifiant de format doit être un entier. ";
65
			$ok = false;
66
			$ok = false;
66
		}
67
		}
67
 
68
 
68
		if(!in_array($format, $this->formats)) {
69
		if(!in_array($format, $this->formats)) {
69
			$message .= "Le format d'image est inconnu, les formats acceptés sont ".implode(',', $this->formats).". ";
70
			$message .= "Le format d'image est inconnu, les formats acceptés sont ".implode(',', $this->formats).". ";
70
			$ok = false;
71
			$ok = false;
71
		}
72
		}
72
 
73
 
73
		$types_methode_livraison = array(self::METHODE_AFFICHAGE, self::METHODE_TELECHARGEMENT);
74
		$types_methode_livraison = array(self::METHODE_AFFICHAGE, self::METHODE_TELECHARGEMENT);
74
		if (!in_array($methode_livraison, $types_methode_livraison)) {
75
		if (!in_array($methode_livraison, $types_methode_livraison)) {
75
			$message .= "Le format de methode de livraison ".$methode_livraison." n'est pas acceptée par le service. ".
76
			$message .= "Le format de methode de livraison ".$methode_livraison." n'est pas acceptée par le service. ".
76
					" Seuls les methodes suivantes sont gérés : ".implode(',', $types_methode_livraison);
77
					" Seuls les methodes suivantes sont gérés : ".implode(',', $types_methode_livraison);
77
			$ok = false;
78
			$ok = false;
78
		}
79
		}
79
 
80
 
80
		if(!empty($message)) {
81
		if(!empty($message)) {
81
			header("HTTP/1.0 400 Bad Request");
82
			header("HTTP/1.0 400 Bad Request");
82
			echo $message;
83
			echo $message;
83
		}
84
		}
84
		return $ok;
85
		return $ok;
85
	}
86
	}
86
 
87
 
87
	private function envoyerImage($id, $image_binaire, $format, $methode) {
88
	private function envoyerImage($id, $image_binaire, $format, $methode) {
88
		if ($methode == self::METHODE_AFFICHAGE) {
89
		if ($methode == self::METHODE_AFFICHAGE) {
89
			header('Content-Type: image/jpeg');
90
			header('Content-Type: image/jpeg');
90
		} else {
91
		} else {
91
			$this->envoyerHeadersTelechargement($id, $image_binaire, $format);
92
			$this->envoyerHeadersTelechargement($id, $image_binaire, $format);
92
		}
93
		}
93
 
94
 
94
		echo $image_binaire;
95
		echo $image_binaire;
95
		exit();
96
		exit();
96
	}
97
	}
97
 
98
 
98
	private function envoyerHeadersTelechargement($id, $image_binaire, $format) {
99
	private function envoyerHeadersTelechargement($id, $image_binaire, $format) {
99
		if (function_exists('mb_strlen')) {
100
		if (function_exists('mb_strlen')) {
100
			$taille = mb_strlen($image_binaire, '8bit');
101
			$taille = mb_strlen($image_binaire, '8bit');
101
		} else {
102
		} else {
102
			$taille = strlen($image_binaire);
103
			$taille = strlen($image_binaire);
103
		}
104
		}
104
 
105
 
105
		// creation du format original
106
		// creation du format original
106
		$id_avec_zeros = sprintf('%09s', $id) ;
107
		$id_avec_zeros = sprintf('%09s', $id) ;
107
		$id_avec_zeros_underscores = wordwrap($id_avec_zeros, 3 , '_', true) ;
108
		$id_avec_zeros_underscores = wordwrap($id_avec_zeros, 3 , '_', true) ;
108
		$nom_fichier = $id_avec_zeros_underscores.'_'.$format.'.jpg';
109
		$nom_fichier = $id_avec_zeros_underscores.'_'.$format.'.jpg';
109
 
110
 
110
		header('Content-Description: File Transfer');
111
		header('Content-Description: File Transfer');
111
		header('Content-Type: application/octet-stream');
112
		header('Content-Type: application/octet-stream');
112
		header('Content-Disposition: attachment; filename="'.$nom_fichier.'"');
113
		header('Content-Disposition: attachment; filename="'.$nom_fichier.'"');
113
		header('Content-Transfer-Encoding: binary');
114
		header('Content-Transfer-Encoding: binary');
114
		header('Connection: Keep-Alive');
115
		header('Connection: Keep-Alive');
115
		header('Expires: 0');
116
		header('Expires: 0');
116
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
117
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
117
		header('Pragma: public');
118
		header('Pragma: public');
118
		header('Content-Length: '.$taille);
119
		header('Content-Length: '.$taille);
119
	}
120
	}
120
 
121
 
121
	private function obtenirDescriptionService() {
122
	private function obtenirDescriptionService() {
122
		$retour = array('description' => 'Ce service peut être appelé afin de visualiser ou bien télécharger les images du cel',
123
		$retour = array('description' => 'Ce service peut être appelé afin de visualiser ou bien télécharger les images du cel',
123
			'formats' => $this->formats,
124
			'formats' => $this->formats,
124
			'utilisation' => 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']."/{id} où {id} est l'identifiant numérique de l'image désirée",
125
			'utilisation' => 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']."/{id} où {id} est l'identifiant numérique de l'image désirée",
125
			'parametres' => array(
126
			'parametres' => array(
126
				'methode' => "Valeurs : afficher, telecharger. Permet de préciser si l'image doit être affichée ou téléchargée",
127
				'methode' => "Valeurs : afficher, telecharger. Permet de préciser si l'image doit être affichée ou téléchargée",
127
				'format' => "Valeurs : voir la liste ci dessous. Permet de demander un format précis de l'image parmi ceux disponibles ")
128
				'format' => "Valeurs : voir la liste ci dessous. Permet de demander un format précis de l'image parmi ceux disponibles ")
128
		);
129
		);
129
 
130
 
130
		// ^^ c'est marrant non ?
131
		// ^^ c'est marrant non ?
131
		$format_formates = array();
132
		$format_formates = array();
132
		foreach ($this->formats as $format) {
133
		foreach ($this->formats as $format) {
133
			if ($format == "O") {
134
			if ($format == "O") {
134
				$format_formates["O"] = array("hauteur" => "dépend de l'image originale",
135
				$format_formates["O"] = array("hauteur" => "dépend de l'image originale",
135
						"largeur" => "dépend de l'image originale",
136
						"largeur" => "dépend de l'image originale",
136
						"notes"	=> "Image dans son ratio et sa résolution originale (elle peut éventuellement avoir été compressée en qualité)"
137
						"notes"	=> "Image dans son ratio et sa résolution originale (elle peut éventuellement avoir été compressée en qualité)"
137
				);
138
				);
138
			} else {
139
			} else {
139
				$description = array();
140
				$description = array();
140
				if (strpos($format, 'R') !== false) {
141
				if (strpos($format, 'R') !== false) {
141
					$description[] = "Format carré, rogné pour ne garder que le centre de l'image.";
142
					$description[] = "Format carré, rogné pour ne garder que le centre de l'image.";
142
				}
143
				}
143
				if (strpos($format, 'C') !== false) {
144
				if (strpos($format, 'C') !== false) {
144
					$description[] = "Format carré, si le format contient R, il est rogné, sinon des bandes blanches sont ajoutées pour conserver le ratio.";
145
					$description[] = "Format carré, si le format contient R, il est rogné, sinon des bandes blanches sont ajoutées pour conserver le ratio.";
145
				}
146
				}
146
 
147
 
147
				if (empty($description)) {
148
				if (empty($description)) {
148
					$description[] = "Format standard, le ratio original de l'image est conservé";
149
					$description[] = "Format standard, le ratio original de l'image est conservé";
149
				}
150
				}
150
 
151
 
151
				$resolution = $this->config['cel']['format_'.$format];
152
				$resolution = $this->config['cel']['format_'.$format];
152
				$resolution = explode("_", $resolution);
153
				$resolution = explode("_", $resolution);
153
				$format_formates[$format] = array("hauteur" => $resolution[0],
154
				$format_formates[$format] = array("hauteur" => $resolution[0],
154
						"largeur" => $resolution[1],
155
						"largeur" => $resolution[1],
155
						"notes"	=> implode(' ', $description)
156
						"notes"	=> implode(' ', $description)
156
				);
157
				);
157
			}
158
			}
158
		}
159
		}
159
 
160
 
160
		$retour['resolutions'] = $format_formates;
161
		$retour['resolutions'] = $format_formates;
161
		return $retour;
162
		return $retour;
162
	}
163
	}
163
}
164
}