Subversion Repositories Sites.obs-saisons.fr

Rev

Rev 189 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
172 aurelien 1
<?php
2
 
3
class OdsImageEspece extends JrestService {
4
 
5
	private $droits = 0755;
6
	const PREFIXE = 'get';
7
 
8
	public function OdsImageEspece($config) {
9
 
10
		$this->config=$config;
11
	}
12
 
13
	/**
14
     * Méthode appelée avec une requête de type GET.
15
     *
16
     */
17
    function getElement($param = array()) {
18
 
19
    	$type = $param[0];
20
 
21
        if ($type == '*' || is_numeric($type)) {
22
            $info = $this->getElementParDefaut($param);
23
        } else {
24
            $methode = self::PREFIXE.$type;
25
            if (method_exists($this, $methode)) {
26
                array_shift($param);
27
                $info = $this->$methode($param);
28
            } else {
29
                $this->messages[] = "Le type d'information demandé '$type' n'est pas disponible.";
30
            }
31
        }
32
 
33
        // Envoi sur la sortie standard
34
        echo 'OK';
35
    }
36
 
37
	public function getRecreationMiniatures() {
38
 
39
		$dossier = $this->config['appli']['chemin_stockage_images_especes'];
40
 
41
		$formats = array('CXS','XS','S','M');
42
 
43
		$dossiers_a_exclure = array();
44
 
45
		foreach (new DirectoryIterator($dossier) as $fichier_ou_dossier) {
46
 
47
			if($fichier_ou_dossier->isDot() || $fichier_ou_dossier->isDir()) {
48
				continue;
49
			}
50
 
51
			if(in_array($fichier_ou_dossier->getBasename(), $dossiers_a_exclure)) {
52
				continue;
189 aurelien 53
			}
54
 
55
			$extension = pathinfo($fichier_ou_dossier->getPathname(),PATHINFO_EXTENSION);
56
			if($extension == 'txt') {
57
				continue;
58
			}
172 aurelien 59
 
60
		    $nom_fichier = $fichier_ou_dossier->getFilename();
61
 
62
			$infos_image_originale = $this->obtenirImageEtInfosPourChemin($fichier_ou_dossier->getPathname());
63
 
64
		    	// creation de miniatures pour chacuns des formats définis
65
			foreach($formats as $format) {
66
					$this->creerEtStockerMiniatureFichierImageSelonFormat($nom_fichier, $infos_image_originale, $format);
67
			}
68
		}
69
	}
70
 
71
	public function creerEtStockerMiniatureFichierImageSelonFormat($nom_fichier ,$infos_image_originale, $format = 'O') {
72
 
73
		if($format == 'O') {
74
			// format original : rien à faire
75
			$image_redimensionnee = $infos_image_originale['image'];
76
 
77
		} else {
78
			// le format carre et une image redimensionnée en gardant son ratio, insérée dans un carré blanc
79
			if($this->estUnFormatCarre($format)) {
80
				$image_redimensionnee = $this->creerMiniatureCarree($infos_image_originale, $format);
81
 
82
			} else {
83
				// la minature est une image redimensionnée en gardant son ratio
84
				$image_redimensionnee = $this->creerMiniature($infos_image_originale, $format);
85
			}
86
		}
87
 
88
		$taux_compression = 100;
89
		if($format == 'O') {
90
			$taux_compression = $this->renvoyerTauxCompressionPourPoids($infos_image_originale['poids_octets']);
91
		}
92
		$this->ecrireImageSurDisque($image_redimensionnee, $nom_fichier, $format, $taux_compression);
93
 
94
		return true;
95
	}
96
 
97
	public function creerImageRedimensionnee($infos_image_originale, $hauteur_redimension, $largeur_redimension) {
98
 
99
		$image_redimensionnee = imagecreatetruecolor($largeur_redimension, $hauteur_redimension);
100
 
101
		imagecopyresampled($image_redimensionnee,
102
						$infos_image_originale['image'],
103
						0, 0,
104
						$debut_largeur_a_copier,
105
						$debut_hauteur_a_copier,
106
						$largeur_redimension,
107
						$hauteur_redimension,
108
						$infos_image_originale['largeur'],
109
						$infos_image_originale['hauteur']
110
		);
111
 
112
		return $image_redimensionnee;
113
	}
114
 
115
	public function creerMiniature($informations_images, $format) {
116
 
117
		$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
118
 
119
		$taille_image_redimensionnee = $this->calculerTailleImage($informations_images, $taille_reference_pour_format['hauteur']);
120
		$image_redimensionnee = $this->creerImageRedimensionnee($informations_images, $taille_image_redimensionnee['hauteur'], $taille_image_redimensionnee['largeur']);
121
 
122
		return $image_redimensionnee;
123
	}
124
 
125
	public function creerMiniatureCarree($informations_images, $format) {
126
 
127
		$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
128
		$cote_carre = $taille_reference_pour_format['largeur'];
129
 
130
		$image_redimensionnee_avec_rapport = $this->creerMiniature($informations_images, $format);
131
		$taille_redimensionnee_avec_rapport = $this->calculerTailleImage($informations_images, $taille_reference_pour_format['hauteur']);
132
 
133
		if($this->estPaysage($informations_images)) {
134
				$debut_largeur_a_copier = 0 ;
135
				$debut_hauteur_a_copier = ($cote_carre - $taille_redimensionnee_avec_rapport['hauteur'])/2 ;
136
		} else {
137
				$debut_largeur_a_copier = ($cote_carre - $taille_redimensionnee_avec_rapport['largeur'])/2 ;
138
				$debut_hauteur_a_copier = 0 ;
139
		}
140
 
141
		$image_carre_cible = $this->renvoyerEtCreerImageCarreeSelonFormat($cote_carre);
142
 
143
		imagecopy($image_carre_cible, $image_redimensionnee_avec_rapport,
144
				$debut_largeur_a_copier ,$debut_hauteur_a_copier, 0, 0,
145
				$taille_redimensionnee_avec_rapport['largeur'], $taille_redimensionnee_avec_rapport['hauteur']
146
		);
147
 
148
		return $image_carre_cible;
149
	}
150
 
151
	public function obtenirImageEtInfosPourChemin($chemin_fichier) {
152
 
153
		$image_et_infos = array();
154
 
155
		list($image_et_infos['largeur'], $image_et_infos['hauteur']) = getimagesize($chemin_fichier);
156
		$image_et_infos['poids_octets'] = filesize($chemin_fichier);
157
		$image_et_infos['image'] = imagecreatefromjpeg($chemin_fichier);
158
 
159
		return $image_et_infos;
160
	}
161
 
162
	public function obtenirDimensionsPourFormat($format) {
163
 
164
		$dimensions = array('largeur' => 0, 'hauteur' => 0);
165
 
166
		if(isset($this->config['appli']['format_'.$format])) {
167
 
262 gduche 168
			$format_largeur_hauteur = explode('_', $this->config['appli']['format_'.$format]);
172 aurelien 169
 
170
			$dimensions['largeur'] = $format_largeur_hauteur[0];
171
			$dimensions['hauteur'] = $format_largeur_hauteur[1];
172
		}
173
 
174
		return $dimensions;
175
 
176
	}
177
 
178
	public function calculerTailleImage($informations_images, $taille_max) {
179
 
180
	        $HL_redimension = array();
181
 
182
	        if($this->estPaysage($informations_images)) {
183
 
184
		        $rapport = $informations_images['hauteur']/$informations_images['largeur'] ;
185
		        $HL_redimension['largeur'] = round($taille_max) ;
186
		        $HL_redimension['hauteur'] = round($taille_max*$rapport) ;
187
 
188
	        } else {
189
	        	$rapport = $informations_images['largeur']/$informations_images['hauteur'] ;
190
		        $HL_redimension['hauteur'] = round($taille_max) ;
191
		        $HL_redimension['largeur'] = round($taille_max*$rapport) ;
192
	        }
193
 
194
	        return $HL_redimension;
195
	}
196
 
197
	public function estUnFormatCarre($format) {
198
 
199
		return (strpos($format,'C') === 0);
200
	}
201
 
202
	public function estPaysage($informations_images) {
203
		return $informations_images['largeur'] > $informations_images['hauteur'];
204
	}
205
 
206
	public function estPortait($informations_images) {
207
		return $informations_images['largeur'] < $informations_images['hauteur'];
208
	}
209
 
210
	public function renvoyerTauxCompressionPourPoids($poids_octets) {
211
 
212
		$poids_max_octets = $this->config['appli']['taille_max'];
213
 
214
		$ratio_compression = 100 ;
215
 
216
	    if($poids_octets >= $poids_max_octets) {
217
	      $ratio_compression = 85 ;
218
	    }
219
 
220
	    return $ratio_compression;
221
	}
222
 
223
	public function ecrireImageSurDisque($image, $nom_fichier, $format, $compression) {
224
 
225
		umask(0);
226
 
227
		$chemin_sur_serveur_final = $this->config['appli']['chemin_stockage_images_especes'].'/'.$format;
228
 
229
		if(!is_dir($chemin_sur_serveur_final)) {
230
			mkdir($chemin_sur_serveur_final,$this->droits);
231
		}
232
 
233
		if(file_exists($chemin_sur_serveur_final.'/'.$nom_fichier)) {
234
			unlink($chemin_sur_serveur_final.'/'.$nom_fichier);
235
		}
236
 
237
		imagejpeg($image, $chemin_sur_serveur_final.'/'.$nom_fichier);
238
		chmod($chemin_sur_serveur_final.'/'.$nom_fichier,$this->droits);
239
 
240
	}
241
 
242
	public function renvoyerEtCreerImageCarreeSelonFormat($cote) {
243
 
244
		$r = $g = $b = 255;
245
 
246
		if(isset($this->config['appli']['couleur_fond_carre'])) {
247
			$rgb = $this->config['appli']['couleur_fond_carre'];
248
 
249
			$r = base_convert(substr($rgb,0,2), 16, 10);
250
			$g = base_convert(substr($rgb,2,2), 16, 10);
251
			$b = base_convert(substr($rgb,4,2), 16, 10);
252
		}
253
 
254
		$image_couleur = imagecreatetruecolor($cote, $cote);
255
		$couleur = imagecolorallocate($image_couleur, $r, $g, $b);
256
		imagefilledrectangle($image_couleur, 0, 0, $cote, $cote, $couleur);
257
 
258
		return $image_couleur;
259
	}
260
 
261
	public function detruireImageEnMemoire($image) {
262
		imagedestroy($image);
263
	}
264
}
265
?>