Subversion Repositories eFlore/Applications.cel

Rev

Rev 632 | Rev 672 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 632 Rev 668
1
<?php
1
<?php
2
Class ImageRecreation {
2
Class ImageRecreation {
3
	
3
	
4
	private $droits = 0755;
4
	private $droits = 0755;
5
	private $formats = array('CRX2S','CXS','CS','CRS','XS','S','M','L','XL','X2L','X3L');
5
	private $formats = array('CRX2S','CXS','CS','CRS','XS','S','M','L','XL','X2L','X3L');
6
	const MODE_GD = 'gd';
6
	const MODE_GD = 'gd';
7
	const MODE_IMAGEMAGICK = 'imagemagick';
7
	const MODE_IMAGEMAGICK = 'imagemagick';
8
	private $mode;
8
	private $mode;
9
	
9
	
10
	private $verbose = true;
10
	private $verbose = true;
11
 
11
 
12
	public function ImageRecreation($config) {
12
	public function ImageRecreation($config) {
13
 
13
 
14
		$this->config=$config;
14
		$this->config=$config;
15
		$this->mode = self::MODE_GD;
15
		$this->mode = self::MODE_GD;
16
		
16
		
17
		if (extension_loaded('imagick')) {
17
		if (extension_loaded('imagick')) {
18
			putenv("MAGICK_TEMPORARY_PATH=/home/aurelien/web/file_tmp");
18
			putenv("MAGICK_TEMPORARY_PATH=".$this->config['cel_db']['chemin_stockage_temp']);
19
			$this->mode = self::MODE_IMAGEMAGICK;
19
			$this->mode = self::MODE_IMAGEMAGICK;
20
		}
20
		}
21
	}
21
	}
22
	
22
	
23
	public function recreerMiniaturesRecursivement() {
23
	public function recreerMiniaturesRecursivement() {
24
		$this->itererRecursivement($this->config['cel_db']['chemin_images']);
24
		$this->itererRecursivement($this->config['cel_db']['chemin_images']);
25
	}
25
	}
26
	
-
 
27
	public function testOptiCrop() {
-
 
28
		
-
 
29
		$image = $this->obtenirImageEtInfosPourChemin('/home/aurelien/web/file_tmp/test.jpg');
-
 
30
		return $this->opticrop($image,'CRXS','');
-
 
31
		
-
 
32
	}
-
 
33
	
26
	
34
	public function regenererMiniaturesIntervalle($params) {
27
	public function regenererMiniaturesIntervalle($params) {
35
		
28
		
36
		$id_debut = $params[0];
29
		$id_debut = $params[0];
37
		$id_fin = $params[1];
30
		$id_fin = $params[1];
38
 
31
 
39
		if (is_numeric($id_debut) && is_numeric($id_fin)) {
32
		if (is_numeric($id_debut) && is_numeric($id_fin)) {
40
			
-
 
41
			
-
 
42
			//echo("Début du traitement des images de ".$id_debut." à ".$id_fin."\n");
-
 
43
			
33
			
44
			for ($i = $id_debut; $i <= $id_fin; $i++) {
-
 
45
					
-
 
46
				echo($i."\n");
34
			for ($i = $id_debut; $i <= $id_fin; $i++) {;
47
				
35
				
48
			    $tab_param = array($i);
36
			    $tab_param = array($i);
49
			    $this->regenererMiniaturesPourId($tab_param);
37
			    $this->regenererMiniaturesPourId($tab_param);
50
			}
38
			}
51
		}
39
		}
52
	}
40
	}
53
	
41
	
54
	public function regenererMiniaturesPourId($params) {
42
	public function regenererMiniaturesPourId($params) {
55
 
43
 
56
		$id = $params[0];
44
		$id = $params[0];
57
 
45
 
58
		if (!is_numeric($id)) {
46
		if (!is_numeric($id)) {
59
		      return;
47
		      return;
60
		}
48
		}
61
 
49
 
62
		$dossier_fichier = $this->obtenirDossierPourFormat($id, 'O');
50
		$dossier_fichier = $this->obtenirDossierPourFormat($id, 'O');
63
 
-
 
64
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, 'O');
51
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, 'O');
65
		
52
		
66
		$chemin_fichier = $dossier_fichier.'/'.$nom_fichier;
53
		$chemin_fichier = $dossier_fichier.'/'.$nom_fichier;
67
 
54
 
68
		if(file_exists($chemin_fichier)) {
55
		if(file_exists($chemin_fichier)) {
69
			$infos_image_originale = $this->obtenirImageEtInfosPourChemin($chemin_fichier);
56
			$infos_image_originale = $this->obtenirImageEtInfosPourChemin($chemin_fichier);
70
							
57
							
71
			// creation de miniatures pour chacuns des formats définis
58
			// creation de miniatures pour chacuns des formats définis
72
			foreach($this->formats as $format) {
59
			foreach($this->formats as $format) {
73
				$this->creerEtStockerMiniatureFichierImageSelonFormat($id, $infos_image_originale, $format);
60
				$this->creerEtStockerMiniatureFichierImageSelonFormat($id, $infos_image_originale, $format);
74
			}
61
			};
75
					      
62
			
76
	      	//if($this->verbose) {
-
 
77
	      		//echo("traitement de l'image ".$infos_image_originale."\n");
-
 
78
	      	//}
-
 
79
		} else {
-
 
80
			//echo 'le fichier '.$chemin_fichier.'n\'existe pas '."\n";
-
 
81
		}
63
		}
82
	}
64
	}
83
	
65
	
84
	public function itererRecursivement($dossier) {
66
	public function itererRecursivement($dossier) {
85
		
67
		
86
		// on ne parse que le dossier des images originales
68
		// on ne parse que le dossier des images originales
87
		$dossiers_a_exclure = $this->getFormats();
69
		$dossiers_a_exclure = $this->getFormats();
88
				
70
				
89
		foreach (new DirectoryIterator($dossier) as $fichier_ou_dossier) {
71
		foreach (new DirectoryIterator($dossier) as $fichier_ou_dossier) {
90
							
72
							
91
			if ($fichier_ou_dossier->isDot()) {
73
			if ($fichier_ou_dossier->isDot()) {
92
				continue;
74
				continue;
93
			}
75
			}
94
			
76
			
95
			if (in_array($fichier_ou_dossier->getBasename(), $dossiers_a_exclure)) {
77
			if (in_array($fichier_ou_dossier->getBasename(), $dossiers_a_exclure)) {
96
				continue;
78
				continue;
97
			}
79
			}
98
			
80
			
99
		    if ($fichier_ou_dossier->isDir()) {
81
		    if ($fichier_ou_dossier->isDir()) {
100
		    	
-
 
101
		    	$profondeur_dossier_fils = $profondeur + 1;
82
		    	
102
	    		$this->itererRecursivement($fichier_ou_dossier->getPathname(), $profondeur_dossier_fils);
83
	    		$this->itererRecursivement($fichier_ou_dossier->getPathname());
103
	    		
84
	    		
104
		    } else {	
85
		    } else {	
105
		    	    	
86
		    	    	
106
		    	$nom_fichier = $fichier_ou_dossier->getFilename();
87
		    	$nom_fichier = $fichier_ou_dossier->getFilename();
107
		    	
88
		    	
108
				$infos_image_originale = $this->obtenirImageEtInfosPourChemin($fichier_ou_dossier->getPathname());
89
				$infos_image_originale = $this->obtenirImageEtInfosPourChemin($fichier_ou_dossier->getPathname());
109
		    	$id = $this->convertirBaseNomFichierVersIdBdd($nom_fichier, $this->formats);
90
		    	$id = $this->convertirBaseNomFichierVersIdBdd($nom_fichier, $this->formats);
110
				
91
				
111
		    	// creation de miniatures pour chacuns des formats définis
92
		    	// creation de miniatures pour chacuns des formats définis
112
				foreach($this->formats as $format) {
93
				foreach($this->formats as $format) {
113
					
94
					
114
					$this->creerEtStockerMiniatureFichierImageSelonFormat($id, $infos_image_originale, $format);
95
					$this->creerEtStockerMiniatureFichierImageSelonFormat($id, $infos_image_originale, $format);
115
				}
96
				}
116
		    }
97
		    }
117
		}
98
		}
118
	}
99
	}
119
			
100
	
120
	public function creerEtStockerMiniatureFichierImageSelonFormat($id ,$infos_image_originale, $format = 'O') {
101
	public function creerMiniatureImageSelonFormat($infos_image_originale, $format = 'O') {
121
		
102
		
122
		if ($format == 'O') {		
103
		if ($format == 'O') {		
123
			// format original : rien à faire
104
			// format original : rien à faire
124
			$image_redimensionnee = $infos_image_originale['image'];
105
			$image_redimensionnee = $infos_image_originale['image'];
125
			
106
			
126
		} else {
107
		} else {
127
			 if ($this->estUnFormatRogne($format)) {
108
			 if ($this->estUnFormatRogne($format)) {
128
			 	
109
			 	
129
			 	if ($this->mode == self::MODE_IMAGEMAGICK) {
110
			 	if ($this->mode == self::MODE_IMAGEMAGICK) {
130
			 		// si l'on dispose de la librairie imageMagick
111
			 		// si l'on dispose de la librairie imageMagick
131
			 		// on applique l'algorithme d'auto détection de sujets
112
			 		// on applique l'algorithme d'auto détection de sujets
132
			 		// qui centre la miniature sur le sujet de l'image
113
			 		// qui centre la miniature sur le sujet de l'image
133
			 		$image_redimensionnee = $this->opticrop($infos_image_originale, $format);
114
			 		$image_redimensionnee = $this->opticrop($infos_image_originale, $format);
134
			 	} else {
115
			 	} else {
135
			 		// si l'on ne dispose que de gd
116
			 		// si l'on ne dispose que de gd
136
					// la minature est une image redimensionnée rognée au centre
117
					// la minature est une image redimensionnée rognée au centre
137
					$image_redimensionnee = $this->creerMiniatureCarreeRognee($infos_image_originale, $format); 
118
					$image_redimensionnee = $this->creerMiniatureCarreeRognee($infos_image_originale, $format); 
138
			 	}	 	
119
			 	}	 	
139
			} else if ($this->estUnFormatCarre($format)) {
120
			} else if ($this->estUnFormatCarre($format)) {
140
				// le format carre et une image redimensionnée en gardant son ratio, insérée dans un carré blanc
121
				// le format carre et une image redimensionnée en gardant son ratio, insérée dans un carré blanc
141
				$image_redimensionnee = $this->creerMiniatureCarree($infos_image_originale, $format);				
122
				$image_redimensionnee = $this->creerMiniatureCarree($infos_image_originale, $format);				
142
			} else {
123
			} else {
143
				$image_redimensionnee = $this->creerMiniature($infos_image_originale, $format);			
124
				$image_redimensionnee = $this->creerMiniature($infos_image_originale, $format);			
144
			}
125
			}
145
		}
126
		}
-
 
127
		
-
 
128
		return $image_redimensionnee;
-
 
129
	}
-
 
130
			
-
 
131
	public function creerEtStockerMiniatureFichierImageSelonFormat($id ,$infos_image_originale, $format = 'O') {
-
 
132
		
-
 
133
		$image_redimensionnee = $this->creerMiniatureImageSelonFormat($infos_image_originale, $format);
146
				
134
				
147
		$taux_compression = $this->renvoyerTauxCompressionPourPoids($infos_image_originale['poids_octets']);
135
		$taux_compression = $this->renvoyerTauxCompressionPourPoids($infos_image_originale['poids_octets']);
148
		$this->ecrireImageSurDisque($image_redimensionnee, $id, $format, $taux_compression);
136
		$this->ecrireImageSurDisque($image_redimensionnee, $id, $format, $taux_compression);
149
				
137
				
150
		return true;
138
		return true;
151
	}
139
	}
152
	
140
	
153
	public function creerImageRedimensionnee($infos_image_originale, $hauteur_redimension, $largeur_redimension) {
141
	public function creerImageRedimensionnee($infos_image_originale, $hauteur_redimension, $largeur_redimension) {
154
		
142
		
155
		$image_redimensionnee = imagecreatetruecolor($largeur_redimension, $hauteur_redimension);
143
		$image_redimensionnee = imagecreatetruecolor($largeur_redimension, $hauteur_redimension);
156
		
144
		
157
		imagecopyresampled($image_redimensionnee, 
145
		imagecopyresampled($image_redimensionnee, 
158
						$infos_image_originale['image'], 
146
						$infos_image_originale['image'], 
159
						0, 0, 
147
						0, 0, 
160
						0, 0, 
148
						0, 0, 
161
						$largeur_redimension, 
149
						$largeur_redimension, 
162
						$hauteur_redimension, 
150
						$hauteur_redimension, 
163
						$infos_image_originale['largeur'], 
151
						$infos_image_originale['largeur'], 
164
						$infos_image_originale['hauteur']
152
						$infos_image_originale['hauteur']
165
		);
153
		);
166
		
154
		
167
		return $image_redimensionnee;
155
		return $image_redimensionnee;
168
	}
156
	}
169
	
157
	
170
	public function creerMiniature($informations_images, $format) {
158
	public function creerMiniature($informations_images, $format) {
171
				
159
				
172
		$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
160
		$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
173
		
161
		
174
		$taille_image_redimensionnee = $this->calculerTailleImage($informations_images, $taille_reference_pour_format['hauteur']);
162
		$taille_image_redimensionnee = $this->calculerTailleImage($informations_images, $taille_reference_pour_format['hauteur']);
175
		$image_redimensionnee = $this->creerImageRedimensionnee($informations_images, $taille_image_redimensionnee['hauteur'], $taille_image_redimensionnee['largeur']);
163
		$image_redimensionnee = $this->creerImageRedimensionnee($informations_images, $taille_image_redimensionnee['hauteur'], $taille_image_redimensionnee['largeur']);
176
		
164
		
177
		return $image_redimensionnee;
165
		return $image_redimensionnee;
178
	}
166
	}
179
	
167
	
180
	public function creerMiniatureCarree($informations_image, $format) {
168
	public function creerMiniatureCarree($informations_image, $format) {
181
		
169
		
182
		$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
170
		$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
183
		$cote_carre = $taille_reference_pour_format['largeur'];
171
		$cote_carre = $taille_reference_pour_format['largeur'];
184
		
172
		
185
		$image_redimensionnee_avec_rapport = $this->creerMiniature($informations_image, $format);
173
		$image_redimensionnee_avec_rapport = $this->creerMiniature($informations_image, $format);
186
		$taille_redimensionnee_avec_rapport = $this->calculerTailleImage($informations_image, $taille_reference_pour_format['hauteur']);
174
		$taille_redimensionnee_avec_rapport = $this->calculerTailleImage($informations_image, $taille_reference_pour_format['hauteur']);
187
		
175
		
188
		if ($this->estPaysage($informations_image)) {				
176
		if ($this->estPaysage($informations_image)) {				
189
				$debut_largeur_a_copier = 0 ;
177
				$debut_largeur_a_copier = 0 ;
190
				$debut_hauteur_a_copier = ($cote_carre - $taille_redimensionnee_avec_rapport['hauteur'])/2 ;	
178
				$debut_hauteur_a_copier = ($cote_carre - $taille_redimensionnee_avec_rapport['hauteur'])/2 ;	
191
		} else {
179
		} else {
192
				$debut_largeur_a_copier = ($cote_carre - $taille_redimensionnee_avec_rapport['largeur'])/2 ;
180
				$debut_largeur_a_copier = ($cote_carre - $taille_redimensionnee_avec_rapport['largeur'])/2 ;
193
				$debut_hauteur_a_copier = 0 ;	
181
				$debut_hauteur_a_copier = 0 ;	
194
		}
182
		}
195
		
183
		
196
		$image_carre_blanc_cible = $this->renvoyerEtCreerImageCarreeBlancheSelonFormat($cote_carre);
184
		$image_carre_blanc_cible = $this->renvoyerEtCreerImageCarreeBlancheSelonFormat($cote_carre);
197
		
185
		
198
		imagecopy($image_carre_blanc_cible, $image_redimensionnee_avec_rapport,
186
		imagecopy($image_carre_blanc_cible, $image_redimensionnee_avec_rapport,
199
				$debut_largeur_a_copier ,$debut_hauteur_a_copier, 0, 0,
187
				$debut_largeur_a_copier ,$debut_hauteur_a_copier, 0, 0,
200
				$taille_redimensionnee_avec_rapport['largeur'], $taille_redimensionnee_avec_rapport['hauteur']
188
				$taille_redimensionnee_avec_rapport['largeur'], $taille_redimensionnee_avec_rapport['hauteur']
201
		);
189
		);
202
 
190
 
203
		return $image_carre_blanc_cible;
191
		return $image_carre_blanc_cible;
204
	}
192
	}
205
	
193
	
206
	public function creerMiniatureCarreeRognee($informations_image, $format) {
194
	public function creerMiniatureCarreeRognee($informations_image, $format) {
207
		
195
		
208
		$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
196
		$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
209
		$cote_carre = $taille_reference_pour_format['largeur'];
197
		$cote_carre = $taille_reference_pour_format['largeur'];
210
		$cote_carre_non_redimensionne = 0;
198
		$cote_carre_non_redimensionne = 0;
211
		
199
		
212
		if ($this->estPaysage($informations_image)) {	
200
		if ($this->estPaysage($informations_image)) {	
213
				$cote_carre_non_redimensionne = $informations_image['hauteur'];		
201
				$cote_carre_non_redimensionne = $informations_image['hauteur'];		
214
				$debut_largeur_a_copier = ($informations_image['hauteur'] - $informations_image['hauteur'])/2 ;
202
				$debut_largeur_a_copier = ($informations_image['hauteur'] - $cote_carre)/2 ;
215
				$debut_hauteur_a_copier = 0;
203
				$debut_hauteur_a_copier = 0;
-
 
204
				
-
 
205
				if($debut_largeur_a_copier <= 0) {
-
 
206
					$debut_largeur_a_copier = 0;
-
 
207
				}
-
 
208
				
216
				$nb_pixels_largeur_a_copier = $informations_image['hauteur'];
209
				$nb_pixels_largeur_a_copier = $cote_carre_non_redimensionne;
217
				$nb_pixels_hauteur_a_copier = $informations_image['hauteur'];
210
				$nb_pixels_hauteur_a_copier = $cote_carre_non_redimensionne;
218
		} else {
211
		} else {
219
				$cote_carre_non_redimensionne = $informations_image['largeur'];
212
				$cote_carre_non_redimensionne = $informations_image['largeur'];
220
				$debut_largeur_a_copier = 0 ;
213
				$debut_largeur_a_copier = 0 ;
221
				$debut_hauteur_a_copier = ($informations_image['largeur'] - $informations_image['largeur'])/2;
214
				$debut_hauteur_a_copier = ($informations_image['largeur'] - $cote_carre)/2;
-
 
215
				
-
 
216
				if($debut_hauteur_a_copier <= 0) {
-
 
217
					$debut_hauteur_a_copier = 0;
-
 
218
				}
-
 
219
				
222
				$nb_pixels_largeur_a_copier = $informations_image['largeur'];
220
				$nb_pixels_largeur_a_copier = $cote_carre_non_redimensionne;
223
				$nb_pixels_hauteur_a_copier = $informations_image['largeur'];
221
				$nb_pixels_hauteur_a_copier = $cote_carre_non_redimensionne;
224
		}
222
		}
225
		
223
		
226
		$image_carre_temporaire = imagecreatetruecolor($cote_carre_non_redimensionne, $cote_carre_non_redimensionne);
224
		$image_carre_temporaire = imagecreatetruecolor($cote_carre_non_redimensionne, $cote_carre_non_redimensionne);
227
		
225
		
228
		imagecopyresampled($image_carre_temporaire, 
226
		imagecopyresampled($image_carre_temporaire, 
229
						$informations_image['image'], 
227
						$informations_image['image'], 
230
						0, 0, 
228
						0, 0, 
231
						$debut_largeur_a_copier,
229
						$debut_largeur_a_copier,
232
						$debut_hauteur_a_copier, 
230
						$debut_hauteur_a_copier, 
233
						$cote_carre_non_redimensionne, 
231
						$cote_carre_non_redimensionne, 
234
						$cote_carre_non_redimensionne, 
232
						$cote_carre_non_redimensionne, 
235
						$nb_pixels_largeur_a_copier, 
233
						$nb_pixels_largeur_a_copier, 
236
						$nb_pixels_hauteur_a_copier
234
						$nb_pixels_hauteur_a_copier
237
		);
235
		);
238
		
236
		
239
		$image_redimensionnee = imagecreatetruecolor($cote_carre, $cote_carre);
237
		$image_redimensionnee = imagecreatetruecolor($cote_carre, $cote_carre);
240
		
238
		
241
		imagecopyresampled($image_redimensionnee, 
239
		imagecopyresampled($image_redimensionnee, 
242
						$image_carre_temporaire, 
240
						$image_carre_temporaire, 
243
						0, 0, 
241
						0, 0, 
244
						0, 0, 
242
						0, 0, 
245
						$cote_carre, 
243
						$cote_carre, 
246
						$cote_carre, 
244
						$cote_carre, 
247
						$cote_carre_non_redimensionne, 
245
						$cote_carre_non_redimensionne, 
248
						$cote_carre_non_redimensionne
246
						$cote_carre_non_redimensionne
249
		);
247
		);
250
 
248
 
251
		return $image_redimensionnee;
249
		return $image_redimensionnee;
252
	}
250
	}
253
	
251
	
254
	public function stockerFichierEtCreerMiniatures($fichier, $id) {
252
	public function stockerFichierEtCreerMiniatures($fichier, $id) {
255
		
253
		
256
		$chemin_base_fichier = $this->creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, 'O');	
254
		$chemin_base_fichier = $this->creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, 'O');	
257
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, 'O');
255
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, 'O');
258
	
256
	
259
		$chemin_fichier = $chemin_base_fichier.'/'.$nom_fichier;
257
		$chemin_fichier = $chemin_base_fichier.'/'.$nom_fichier;
260
		
258
		
261
		$deplacement_fichier = $this->stockerImageExterne($fichier['tmp_name'], $chemin_fichier);
259
		$deplacement_fichier = $this->stockerImageExterne($fichier['tmp_name'], $chemin_fichier);
262
		
260
		
263
		if ($deplacement_fichier) {
261
		if ($deplacement_fichier) {
264
 
262
 
265
			$infos_image_originale = $this->obtenirImageEtInfosPourChemin($chemin_fichier);
263
			$infos_image_originale = $this->obtenirImageEtInfosPourChemin($chemin_fichier);
266
			$taux_compression = $this->renvoyerTauxCompressionPourPoids($infos_image_originale['poids_octets']);
264
			$taux_compression = $this->renvoyerTauxCompressionPourPoids($infos_image_originale['poids_octets']);
267
			
265
			
268
			if ($taux_compression < 100 && $this->mode == self::MODE_IMAGEMAGICK) {
266
			if ($taux_compression < 100 && $this->mode == self::MODE_IMAGEMAGICK) {
269
				$this->ecrireImageSurDisqueAvecMeta($chemin_fichier, $taux_compression);
267
				$this->ecrireImageSurDisqueAvecMeta($chemin_fichier, $taux_compression);
270
			}
268
			}
271
				
269
				
272
			$infos_image_originale_stockee = $this->obtenirImageEtInfosPourChemin($chemin_fichier);
270
			$infos_image_originale_stockee = $this->obtenirImageEtInfosPourChemin($chemin_fichier);
273
			
271
			
274
			$formats = $this->getFormats();
272
			$formats = $this->getFormats();
275
			
273
			
276
			// creation de miniatures pour chacuns des formats définis
274
			// creation de miniatures pour chacuns des formats définis
277
			foreach($formats as $format) {
275
			foreach($formats as $format) {
278
				$this->creerEtStockerMiniatureFichierImageSelonFormat($id, $infos_image_originale_stockee, $format);
276
				$this->creerEtStockerMiniatureFichierImageSelonFormat($id, $infos_image_originale_stockee, $format);
279
			}
277
			}
280
	
278
	
281
	  		return true ;
279
	  		return true ;
282
	  		
280
	  		
283
		} else {
281
		} else {
284
			$erreur =  'ERROR : probleme durant le déplacement du fichier temporaire \n' ;
282
			$erreur =  'ERROR : probleme durant le déplacement du fichier temporaire \n' ;
285
			$this->logger('CEL_bugs',$erreur);
283
			$this->logger('CEL_bugs',$erreur);
286
  			return false ;
284
  			return false ;
287
		}
285
		}
288
	}
286
	}
289
	
287
	
290
	public function stockerImageExterne($chemin_fichier_temp, $chemin_destination) {
288
	public function stockerImageExterne($chemin_fichier_temp, $chemin_destination) {
291
		
289
		
292
		if(is_uploaded_file($chemin_fichier_temp)) {
290
		if(is_uploaded_file($chemin_fichier_temp)) {
293
			$deplacement = move_uploaded_file($chemin_fichier_temp, $chemin_destination);
291
			$deplacement = move_uploaded_file($chemin_fichier_temp, $chemin_destination);
294
		} else {
292
		} else {
295
			$deplacement = rename($chemin_fichier_temp, $chemin_destination);
293
			$deplacement = rename($chemin_fichier_temp, $chemin_destination);
296
		}
294
		}
297
		
295
		
298
		return $deplacement;
296
		return $deplacement;
299
	}
297
	}
300
	
298
	
301
	public function creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, $format) {
299
	public function creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, $format) {
302
		
300
		
303
		$chemin_sur_serveur_final = $this->obtenirDossierPourFormat($id,$format);
301
		$chemin_sur_serveur_final = $this->obtenirDossierPourFormat($id,$format);
304
		
302
		
305
		if (!file_exists($chemin_sur_serveur_final))
303
		if (!file_exists($chemin_sur_serveur_final))
306
		{
304
		{
-
 
305
			umask(0);
307
			if (mkdir($chemin_sur_serveur_final,$this->droits, true)) {
306
			if (mkdir($chemin_sur_serveur_final,$this->droits, true)) {
308
				chmod($chemin_sur_serveur_final,$this->droits);
307
				chmod($chemin_sur_serveur_final,$this->droits);
309
			}
308
			}
310
			else
309
			else
311
			{
310
			{
312
				$erreur =  'ERROR : probleme durant l\'écriture du dossier '.$format.' \n' ;
311
				$erreur =  'ERROR : probleme durant l\'écriture du dossier '.$format.' \n' ;
313
				echo $erreur;
312
				echo $erreur;
314
				$this->logger('CEL_bugs',$erreur);
313
				$this->logger('CEL_bugs',$erreur);
315
				return false;
314
				return false;
316
			}
315
			}
317
		}
316
		}
318
		
317
		
319
		return $chemin_sur_serveur_final;
318
		return $chemin_sur_serveur_final;
320
	}
319
	}
321
	
320
	
322
	public function obtenirDossierPourFormat($id, $format) {
321
	public function obtenirDossierPourFormat($id, $format) {
323
		
322
		
324
		$chemin_base = $this->config['cel_db']['chemin_images'];
323
		$chemin_base = $this->config['cel_db']['chemin_images'];
325
	
324
	
326
		$chemin_sur_serveur = $chemin_base ;
325
		$chemin_sur_serveur = $chemin_base ;
327
	
326
	
328
		$id = sprintf('%09s', $id) ;
327
		$id = sprintf('%09s', $id) ;
329
		$id = wordwrap($id, 3 , '_', true) ;
328
		$id = wordwrap($id, 3 , '_', true) ;
330
	
329
	
331
		$niveauDossier = split("_", $id) ;
330
		$niveauDossier = split("_", $id) ;
332
	
331
	
333
		$dossierNiveau1 = $niveauDossier[0] ;
332
		$dossierNiveau1 = $niveauDossier[0] ;
334
		$dossierNiveau2 = $niveauDossier[1] ;
333
		$dossierNiveau2 = $niveauDossier[1] ;
335
		
334
		
336
		$chemin_sur_serveur_final = $chemin_sur_serveur.'/'.$dossierNiveau1.'/'.$dossierNiveau2.'/'.$format;
335
		$chemin_sur_serveur_final = $chemin_sur_serveur.'/'.$dossierNiveau1.'/'.$dossierNiveau2.'/'.$format;
337
		
336
		
338
		return $chemin_sur_serveur_final;
337
		return $chemin_sur_serveur_final;
339
	}
338
	}
-
 
339
	
-
 
340
	public function obtenirCheminImageOriginale($id_image) {
-
 
341
		
-
 
342
		$nom = $this->convertirIdBddVersNomFichier($id_image, 'O');
-
 
343
		$dossier = $this->obtenirDossierPourFormat($id_image,'O');
-
 
344
		
-
 
345
		return $dossier.'/'.$nom;
-
 
346
	}
-
 
347
	
-
 
348
	public function obtenirImageEtInfosPourId($id_image) {
-
 
349
		
-
 
350
		$chemin_image_o = $this->obtenirCheminImageOriginale($id_image);
-
 
351
		return $this->obtenirImageEtInfosPourChemin($chemin_image_o);
-
 
352
	}
340
	
353
	
341
	public function obtenirImageEtInfosPourChemin($chemin_fichier) {
354
	public function obtenirImageEtInfosPourChemin($chemin_fichier) {
-
 
355
		
-
 
356
		$image_et_infos = false;
-
 
357
		
342
		
358
		if(file_exists($chemin_fichier)) { 
343
		$image_et_infos = array();
359
			
344
		
360
			$image_et_infos = array();
345
		list($image_et_infos['largeur'], $image_et_infos['hauteur']) = getimagesize($chemin_fichier);		
361
			list($image_et_infos['largeur'], $image_et_infos['hauteur']) = getimagesize($chemin_fichier);		
-
 
362
			$image_et_infos['poids_octets'] = filesize($chemin_fichier);
346
		$image_et_infos['poids_octets'] = filesize($chemin_fichier);
363
			$image_et_infos['image'] = imagecreatefromjpeg($chemin_fichier);
347
		$image_et_infos['image'] = imagecreatefromjpeg($chemin_fichier);
364
			$image_et_infos['chemin'] = $chemin_fichier;
348
		$image_et_infos['chemin'] = $chemin_fichier;
365
		}
349
		
366
		
350
		return $image_et_infos;
367
		return $image_et_infos;
351
	}
368
	}
352
	
369
	
353
	public function obtenirDimensionsPourFormat($format) {
370
	public function obtenirDimensionsPourFormat($format) {
354
		
371
		
355
		$dimensions = array('largeur' => 0, 'hauteur' => 0);
372
		$dimensions = array('largeur' => 0, 'hauteur' => 0);
356
			
373
			
357
		if (isset($this->config['cel_db']['format_'.$format])) {
374
		if (isset($this->config['cel_db']['format_'.$format])) {
358
			
375
			
359
			$format_largeur_hauteur = split('_', $this->config['cel_db']['format_'.$format]);
376
			$format_largeur_hauteur = split('_', $this->config['cel_db']['format_'.$format]);
360
			
377
			
361
			$dimensions['largeur'] = $format_largeur_hauteur[0];
378
			$dimensions['largeur'] = $format_largeur_hauteur[0];
362
			$dimensions['hauteur'] = $format_largeur_hauteur[1];
379
			$dimensions['hauteur'] = $format_largeur_hauteur[1];
363
		}
380
		}
364
		
381
		
365
		return $dimensions;
382
		return $dimensions;
366
		
383
		
367
	}
384
	}
368
	
385
	
369
	public function calculerTailleImage($informations_images, $taille_max) {
386
	public function calculerTailleImage($informations_images, $taille_max) {
370
	
387
	
371
	        $HL_redimension = array();
388
	        $HL_redimension = array();
372
	        
389
	        
373
	        if ($this->estPaysage($informations_images)) {
390
	        if ($this->estPaysage($informations_images)) {
374
	        
391
	        
375
		        $rapport = $informations_images['hauteur']/$informations_images['largeur'] ;
392
		        $rapport = $informations_images['hauteur']/$informations_images['largeur'] ;
376
		        $HL_redimension['largeur'] = round($taille_max) ;
393
		        $HL_redimension['largeur'] = round($taille_max) ;
377
		        $HL_redimension['hauteur'] = round($taille_max*$rapport) ;
394
		        $HL_redimension['hauteur'] = round($taille_max*$rapport) ;
378
		        
395
		        
379
	        } else {
396
	        } else {
380
	        	$rapport = $informations_images['largeur']/$informations_images['hauteur'] ;
397
	        	$rapport = $informations_images['largeur']/$informations_images['hauteur'] ;
381
		        $HL_redimension['hauteur'] = round($taille_max) ;
398
		        $HL_redimension['hauteur'] = round($taille_max) ;
382
		        $HL_redimension['largeur'] = round($taille_max*$rapport) ;
399
		        $HL_redimension['largeur'] = round($taille_max*$rapport) ;
383
	        }
400
	        }
384
	        
401
	        
385
	        return $HL_redimension;
402
	        return $HL_redimension;
386
	}
403
	}
387
	
404
	
388
	public function getFormats() {
405
	public function getFormats() {
389
		return $this->formats;
406
		return $this->formats;
390
	}
407
	}
391
	
408
	
392
	public function estUnFormatCarre($format) {
409
	public function estUnFormatCarre($format) {
393
		
410
		
394
		return (strpos($format,'C') === 0);		
411
		return (strpos($format,'C') === 0);		
395
	}
412
	}
396
	
413
	
397
	public function estUnFormatRogne($format) {
414
	public function estUnFormatRogne($format) {
398
 
415
 
399
		return (strpos($format,'R') === 1);		
416
		return (strpos($format,'R') === 1);		
400
	}
417
	}
401
	
418
	
402
	public function estPaysage($informations_images) {
419
	public function estPaysage($informations_images) {
403
		return $informations_images['largeur'] > $informations_images['hauteur'];
420
		return $informations_images['largeur'] > $informations_images['hauteur'];
404
	}
421
	}
405
	
422
	
406
	public function estPortait($informations_images) {
423
	public function estPortait($informations_images) {
407
		return $informations_images['largeur'] < $informations_images['hauteur'];
424
		return $informations_images['largeur'] < $informations_images['hauteur'];
408
	}
425
	}
409
	
426
	
410
	public function renvoyerTauxCompressionPourPoids($poids_octets) {
427
	public function renvoyerTauxCompressionPourPoids($poids_octets) {
411
 
428
 
412
		$poids_max_octets = $this->config['cel_db']['taille_max'];
429
		$poids_max_octets = $this->config['cel_db']['taille_max'];
413
		
430
		
414
		$ratio_compression = 100 ;
431
		$ratio_compression = 100 ;
415
		
432
		
416
	    if ($poids_octets >= $poids_max_octets) {
433
	    if ($poids_octets >= $poids_max_octets) {
417
	      $ratio_compression = 75 ;
434
	      $ratio_compression = 75 ;
418
	    }
435
	    }
419
	    
436
	    
420
	    return $ratio_compression;
437
	    return $ratio_compression;
421
	}
438
	}
422
	
439
	
423
	public function convertirIdBddVersNomFichier($id, $format, $extension = 'jpg') {
440
	public function convertirIdBddVersNomFichier($id, $format, $extension = 'jpg') {
424
		
441
		
425
			// creation du format original
442
			// creation du format original
426
		$id_avec_zeros = sprintf('%09s', $id) ;
443
		$id_avec_zeros = sprintf('%09s', $id) ;
427
		$id_avec_zeros_underscores = wordwrap($id_avec_zeros, 3 , '_', true) ;
444
		$id_avec_zeros_underscores = wordwrap($id_avec_zeros, 3 , '_', true) ;
428
		
445
		
429
		$nom_fichier = $id_avec_zeros_underscores.'_'.$format.'.'.$extension;
446
		$nom_fichier = $id_avec_zeros_underscores.'_'.$format.'.'.$extension;
430
		
447
		
431
		return $nom_fichier;
448
		return $nom_fichier;
432
	}
449
	}
433
	
450
	
434
	public function convertirBaseNomFichierVersIdBdd($nom_fichier, $formats) {
451
	public function convertirBaseNomFichierVersIdBdd($nom_fichier, $formats) {
435
		
452
		
436
		$nom_fichier_sans_extension = trim($nom_fichier, '.jpg');
453
		$nom_fichier_sans_extension = trim($nom_fichier, '.jpg');
437
		
454
		
438
		foreach($formats as $format) {
455
		foreach($formats as $format) {
439
			$nom_fichier_sans_extension = trim($nom_fichier_sans_extension, '_'.$format);
456
			$nom_fichier_sans_extension = trim($nom_fichier_sans_extension, '_'.$format);
440
		}
457
		}
441
		
458
		
442
		$id_image = str_replace('_', '', $nom_fichier_sans_extension);
459
		$id_image = str_replace('_', '', $nom_fichier_sans_extension);
443
			
460
			
444
		// suppression des 0 devant
461
		// suppression des 0 devant
445
		$id_image += 0;
462
		$id_image += 0;
446
		
463
		
447
		return $id_image;
464
		return $id_image;
448
	}
465
	}
449
	
466
	
450
	public function ecrireImageSurDisque($image_binaire, $id, $format, $compression = 100) {
467
	public function ecrireImageSurDisque($image_binaire, $id, $format, $compression = 100) {
451
		
468
		
452
		umask(0);
469
		umask(0);
453
		
470
		
454
		$chemin_sur_serveur_final = $this->creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, $format);
471
		$chemin_sur_serveur_final = $this->creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, $format);
455
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, $format);
472
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, $format);
456
		
473
		
457
		if (file_exists($chemin_sur_serveur_final.'/'.$nom_fichier)) {
474
		if (file_exists($chemin_sur_serveur_final.'/'.$nom_fichier)) {
458
			unlink($chemin_sur_serveur_final.'/'.$nom_fichier);
475
			unlink($chemin_sur_serveur_final.'/'.$nom_fichier);
459
		}
476
		}
460
		
477
		
461
		// attention ceci ne preserve pas les metadonnées
478
		// attention, ceci ne preserve pas les metadonnées
462
		imagejpeg($image_binaire, $chemin_sur_serveur_final.'/'.$nom_fichier, $compression);
479
		imagejpeg($image_binaire, $chemin_sur_serveur_final.'/'.$nom_fichier, $compression);
463
		chmod($chemin_sur_serveur_final.'/'.$nom_fichier,$this->droits);
480
		chmod($chemin_sur_serveur_final.'/'.$nom_fichier,$this->droits);
464
	}
481
	}
465
		
482
		
466
	public function ecrireImageSurDisqueAvecMeta($image_a_stocker, $compression = 100) {
483
	public function ecrireImageSurDisqueAvecMeta($image_a_stocker, $compression = 100) {
467
 
-
 
468
		$image_a_stocker = new Imagick();
484
 
469
		$image_a_stocker->readImageBlob($image_a_stocker);
485
		$image_a_stocker = new Imagick($image_a_stocker);
470
	
486
	
471
		// l'utilisation d'image magick préserve les métadonnées lors d'une recompression
487
		// l'utilisation d'image magick préserve les métadonnées lors d'une recompression
472
		$image_a_stocker->setformat("jpeg");
488
		$image_a_stocker->setformat("jpeg");
473
		$image_a_stocker->setImageCompression(imagick::COMPRESSION_JPEG); 
489
		$image_a_stocker->setImageCompression(imagick::COMPRESSION_JPEG); 
474
		$image_a_stocker->setCompressionQuality($compression);
490
		$image_a_stocker->setCompressionQuality($compression);
475
		$image_a_stocker->writeImage($chemin_fichier);
491
		$image_a_stocker->writeImage($image_a_stocker);
476
		$image_a_stocker->destroy();	
492
		$image_a_stocker->destroy();	
477
		
493
		
478
		chmod($chemin_sur_serveur_final.'/'.$nom_fichier,$this->droits);
494
		chmod($image_a_stocker,$this->droits);
479
	}
495
	}
480
	
496
	
481
	public function renvoyerEtCreerImageCarreeBlancheSelonFormat($cote) {
497
	public function renvoyerEtCreerImageCarreeBlancheSelonFormat($cote) {
482
		
498
		
483
		$image_blanche = imagecreatetruecolor($cote, $cote);		
499
		$image_blanche = imagecreatetruecolor($cote, $cote);		
484
		$blanc = imagecolorallocate($image_blanche, 255, 255, 255);
500
		$blanc = imagecolorallocate($image_blanche, 255, 255, 255);
485
		imagefilledrectangle($image_blanche, 0, 0, $cote, $cote, $blanc);
501
		imagefilledrectangle($image_blanche, 0, 0, $cote, $cote, $blanc);
486
		
502
		
487
		return $image_blanche;
503
		return $image_blanche;
488
	}
504
	}
489
	
505
	
490
	public function detruireImageEnMemoire($image) {		
506
	public function detruireImageEnMemoire($image) {		
491
		imagedestroy($image);
507
		imagedestroy($image);
492
	}
508
	}
493
	
509
	
494
	public function detruireImageSurDisque($id) {
510
	public function detruireImageSurDisque($id) {
495
		
511
		
496
		$formats = $this->getFormats();
512
		$formats = $this->getFormats();
497
		
513
		
498
		// on detruit aussi l'image originale
514
		// on detruit aussi l'image originale
499
		$formats[] = 'O';
515
		$formats[] = 'O';
500
		
516
		
501
		$destruction_formats_fichier = false;
517
		$destruction_formats_fichier = false;
502
		
518
		
503
		// destructions de chacuns des formats définis
519
		// destructions de chacuns des formats définis
504
		foreach($formats as $format) {
520
		foreach($formats as $format) {
505
			
521
			
506
			$dossier_format = $this->obtenirDossierPourFormat($id, $format);
522
			$dossier_format = $this->obtenirDossierPourFormat($id, $format);
507
			$nom_fichier = $this->convertirIdBddVersNomFichier($id, $format);
523
			$nom_fichier = $this->convertirIdBddVersNomFichier($id, $format);
508
			
524
			
509
			if (file_exists($dossier_format.'/'.$nom_fichier)) {
525
			if (file_exists($dossier_format.'/'.$nom_fichier)) {
510
				$destruction_formats_fichier = unlink($dossier_format.'/'.$nom_fichier);
526
				$destruction_formats_fichier = unlink($dossier_format.'/'.$nom_fichier);
511
			} else {
527
			} else {
512
				$destruction_formats_fichier = true;
528
				$destruction_formats_fichier = true;
513
			}
529
			}
514
		}
530
		}
515
		
531
		
516
		return $destruction_formats_fichier;
532
		return $destruction_formats_fichier;
517
	}
533
	}
518
	
534
	
519
	/* 
535
	/* 
520
	 * edge-maximizing crop
536
	 * edge-maximizing crop
521
	 * determines center-of-edginess, then tries different-sized crops around it. 
537
	 * determines center-of-edginess, then tries different-sized crops around it. 
522
	 * picks the crop with the highest normalized edginess.
538
	 * picks the crop with the highest normalized edginess.
523
	 * see documentation on how to tune the algorithm
539
	 * see documentation on how to tune the algorithm
524
	 *
540
	 *
525
	 * $informations_image - le tableau d'informations sur l'image tel que renvoyé par la fonction obtenirImageEtInfosPourChemin
541
	 * $informations_image - le tableau d'informations sur l'image tel que renvoyé par la fonction obtenirImageEtInfosPourChemin
526
	 * $format - le format (ex. : CS, XS, XL, CRS)
542
	 * $format - le format (ex. : CS, XS, XL, CRS)
527
	*/
543
	*/
528
	function opticrop($informations_image, $format) {
544
	function opticrop($informations_image, $format) {
529
 
545
 
530
		umask(0);
546
		umask(0);
531
		
547
		
532
		$nom_temp = md5(time());
548
		$nom_temp = md5(time());
-
 
549
		$chemin_temp = 
-
 
550
		
533
		$out = '/home/aurelien/web/file_tmp/'.$nom_temp;
551
		$out = $this->config['cel_db']['chemin_stockage_temp'].'/'.$nom_temp;
534
		
552
		
535
		$dimension_vignettes = $this->obtenirDimensionsPourFormat($format);
553
		$dimension_vignettes = $this->obtenirDimensionsPourFormat($format);
536
		
554
		
537
		$largeur_vignette = $dimension_vignettes['largeur'];
555
		$largeur_vignette = $dimension_vignettes['largeur'];
538
		$hauteur_vignette = $dimension_vignettes['hauteur'];
556
		$hauteur_vignette = $dimension_vignettes['hauteur'];
539
		
557
		
540
	    // source dimensions
558
	    // source dimensions
541
	    $largeur_image_originale = $informations_image['largeur'];
559
	    $largeur_image_originale = $informations_image['largeur'];
542
	    $hauteur_image_originale = $informations_image['hauteur'];
560
	    $hauteur_image_originale = $informations_image['hauteur'];
543
	    
561
	    
544
	    $chemin_image = $informations_image['chemin'];
562
	    $chemin_image = $informations_image['chemin'];
545
	    
563
	    
546
	    //if ($largeur_vignette > $largeur_image_originale || $hauteur_vignette > $hauteur_image_originale)
564
	    //if ($largeur_vignette > $largeur_image_originale || $hauteur_vignette > $hauteur_image_originale)
547
	    //    die("Target dimensions must be smaller or equal to source dimensions.");
565
	    //    die("Target dimensions must be smaller or equal to source dimensions.");
548
	
566
	
549
	    // parameters for the edge-maximizing crop algorithm
567
	    // parameters for the edge-maximizing crop algorithm
550
	    $r = 1;         // radius of edge filter
568
	    $r = 1;         // radius of edge filter
551
	    $nk = 9;        // scale count: number of crop sizes to try
569
	    $nk = 9;        // scale count: number of crop sizes to try
552
	    $gamma = 0.2;   // edge normalization parameter -- see documentation
570
	    $gamma = 0.2;   // edge normalization parameter -- see documentation
553
	    $ar = $largeur_vignette/$hauteur_vignette;    // target aspect ratio (AR)
571
	    $ar = $largeur_vignette/$hauteur_vignette;    // target aspect ratio (AR)
554
	    $ar0 = $largeur_image_originale/$hauteur_image_originale;    // original aspect ratio (AR)
572
	    $ar0 = $largeur_image_originale/$hauteur_image_originale;    // original aspect ratio (AR)
555
	
573
	
556
	    //echo("$chemin_image: $largeur_image_originale x $hauteur_image_originale => $largeur_vignette x $hauteur_vignette");
574
	    //echo("$chemin_image: $largeur_image_originale x $hauteur_image_originale => $largeur_vignette x $hauteur_vignette");
557
	    $img = new Imagick($chemin_image);
575
	    $img = new Imagick($chemin_image);
558
	    $imgcp = clone $img;
576
	    $imgcp = clone $img;
559
	
577
	
560
	    // compute center of edginess
578
	    // compute center of edginess
561
	    $img->edgeImage($r);
579
	    $img->edgeImage($r);
562
	    $img->modulateImage(100,0,100); // grayscale
580
	    $img->modulateImage(100,0,100); // grayscale
563
	    $img->blackThresholdImage("#0f0f0f");
581
	    $img->blackThresholdImage("#0f0f0f");
564
	    $img->writeImage($out);
582
	    $img->writeImage($out);
565
	    // use gd for random pixel access
583
	    // use gd for random pixel access
566
	    $im = ImageCreateFromJpeg($out);
584
	    $im = ImageCreateFromJpeg($out);
567
	    $xcenter = 0;
585
	    $xcenter = 0;
568
	    $ycenter = 0;
586
	    $ycenter = 0;
569
	    $sum = 0;
587
	    $sum = 0;
570
	    $n = 100000;
588
	    $n = 100000;
571
	    for ($k=0; $k<$n; $k++) {
589
	    for ($k=0; $k<$n; $k++) {
572
	        $i = mt_rand(0,$largeur_image_originale-1);
590
	        $i = mt_rand(0,$largeur_image_originale-1);
573
	        $j = mt_rand(0,$hauteur_image_originale-1);
591
	        $j = mt_rand(0,$hauteur_image_originale-1);
574
	        $val = imagecolorat($im, $i, $j) & 0xFF;
592
	        $val = imagecolorat($im, $i, $j) & 0xFF;
575
	        $sum += $val;
593
	        $sum += $val;
576
	        $xcenter += ($i+1)*$val;
594
	        $xcenter += ($i+1)*$val;
577
	        $ycenter += ($j+1)*$val;
595
	        $ycenter += ($j+1)*$val;
578
	    }
596
	    }
579
	    $xcenter /= $sum;
597
	    $xcenter /= $sum;
580
	    $ycenter /= $sum;
598
	    $ycenter /= $sum;
581
	
599
	
582
	    // crop source img to target AR
600
	    // crop source img to target AR
583
	    if ($largeur_image_originale/$hauteur_image_originale > $ar) {
601
	    if ($largeur_image_originale/$hauteur_image_originale > $ar) {
584
	        // source AR wider than target
602
	        // source AR wider than target
585
	        // crop width to target AR
603
	        // crop width to target AR
586
	        $wcrop0 = round($ar*$hauteur_image_originale);
604
	        $wcrop0 = round($ar*$hauteur_image_originale);
587
	        $hcrop0 = $hauteur_image_originale;
605
	        $hcrop0 = $hauteur_image_originale;
588
	    } 
606
	    } 
589
	    else {
607
	    else {
590
	        // crop height to target AR
608
	        // crop height to target AR
591
	        $wcrop0 = $largeur_image_originale;
609
	        $wcrop0 = $largeur_image_originale;
592
	        $hcrop0 = round($largeur_image_originale/$ar);
610
	        $hcrop0 = round($largeur_image_originale/$ar);
593
	    }
611
	    }
594
	
612
	
595
	    // crop parameters for all scales and translations
613
	    // crop parameters for all scales and translations
596
	    $params = array();
614
	    $params = array();
597
	
615
	
598
	    // crop at different scales
616
	    // crop at different scales
599
	    $hgap = $hcrop0 - $hauteur_vignette;
617
	    $hgap = $hcrop0 - $hauteur_vignette;
600
	    $hinc = ($nk == 1) ? 0 : $hgap / ($nk - 1);
618
	    $hinc = ($nk == 1) ? 0 : $hgap / ($nk - 1);
601
	    $wgap = $wcrop0 - $largeur_vignette;
619
	    $wgap = $wcrop0 - $largeur_vignette;
602
	    $winc = ($nk == 1) ? 0 : $wgap / ($nk - 1);
620
	    $winc = ($nk == 1) ? 0 : $wgap / ($nk - 1);
603
	
621
	
604
	    // find window with highest normalized edginess
622
	    // find window with highest normalized edginess
605
	    $n = 10000;
623
	    $n = 10000;
606
	    $maxbetanorm = 0;
624
	    $maxbetanorm = 0;
607
	    $maxfile = '';
625
	    $maxfile = '';
608
	    $maxparam = array('w'=>0, 'h'=>0, 'x'=>0, 'y'=>0);
626
	    $maxparam = array('w'=>0, 'h'=>0, 'x'=>0, 'y'=>0);
609
	    
627
	    
610
	    for ($k = 0; $k < $nk; $k++) {
628
	    for ($k = 0; $k < $nk; $k++) {
611
	        $hcrop = round($hcrop0 - $k*$hinc);
629
	        $hcrop = round($hcrop0 - $k*$hinc);
612
	        $wcrop = round($wcrop0 - $k*$winc);
630
	        $wcrop = round($wcrop0 - $k*$winc);
613
	        $xcrop = $xcenter - $wcrop / 2;
631
	        $xcrop = $xcenter - $wcrop / 2;
614
	        $ycrop = $ycenter - $hcrop / 2;
632
	        $ycrop = $ycenter - $hcrop / 2;
615
	        //echo("crop: $wcrop, $hcrop, $xcrop, $ycrop");
633
	        //echo("crop: $wcrop, $hcrop, $xcrop, $ycrop");
616
	
634
	
617
	        if ($xcrop < 0) $xcrop = 0;
635
	        if ($xcrop < 0) $xcrop = 0;
618
	        if ($xcrop+$wcrop > $largeur_image_originale) $xcrop = $largeur_image_originale-$wcrop;
636
	        if ($xcrop+$wcrop > $largeur_image_originale) $xcrop = $largeur_image_originale-$wcrop;
619
	        if ($ycrop < 0) $ycrop = 0;
637
	        if ($ycrop < 0) $ycrop = 0;
620
	        if ($ycrop+$hcrop > $hauteur_image_originale) $ycrop = $hauteur_image_originale-$hcrop;
638
	        if ($ycrop+$hcrop > $hauteur_image_originale) $ycrop = $hauteur_image_originale-$hcrop;
621
		        
639
		        
622
	        /*if (self::MODE_DEBUG) {
640
	        /*if (self::MODE_DEBUG) {
623
	        	// debug
641
	        	// debug
624
	        	$currfile = '/home/aurelien/web/file_tmp/'."image$k.jpg";
642
	        	$currfile = '/home/aurelien/web/file_tmp/'."image$k.jpg";
625
	        	
643
	        	
626
	            $currimg = clone $img;
644
	            $currimg = clone $img;
627
	            $c= new ImagickDraw(); 
645
	            $c= new ImagickDraw(); 
628
	            $c->setFillColor("red"); 
646
	            $c->setFillColor("red"); 
629
	            $c->circle($xcenter, $ycenter, $xcenter, $ycenter+4); 
647
	            $c->circle($xcenter, $ycenter, $xcenter, $ycenter+4); 
630
	            $currimg->drawImage($c); 
648
	            $currimg->drawImage($c); 
631
	            $currimg->cropImage($wcrop, $hcrop, $xcrop, $ycrop);
649
	            $currimg->cropImage($wcrop, $hcrop, $xcrop, $ycrop);
632
	            $currimg->writeImage($currfile);
650
	            $currimg->writeImage($currfile);
633
	            $currimg->destroy();
651
	            $currimg->destroy();
634
	        }*/
652
	        }*/
635
	
653
	
636
	        $beta = 0;
654
	        $beta = 0;
637
	        for ($c=0; $c<$n; $c++) {
655
	        for ($c=0; $c<$n; $c++) {
638
	            $i = mt_rand(0,$wcrop-1);
656
	            $i = mt_rand(0,$wcrop-1);
639
	            $j = mt_rand(0,$hcrop-1);
657
	            $j = mt_rand(0,$hcrop-1);
640
	            $beta += imagecolorat($im, $xcrop+$i, $ycrop+$j) & 0xFF;
658
	            $beta += imagecolorat($im, $xcrop+$i, $ycrop+$j) & 0xFF;
641
	        }
659
	        }
642
	        $area = $wcrop * $hcrop;
660
	        $area = $wcrop * $hcrop;
643
	        $betanorm = $beta / ($n*pow($area, $gamma-1));
661
	        $betanorm = $beta / ($n*pow($area, $gamma-1));
644
	        // echo("beta: $beta; betan: $betanorm");
662
	        // echo("beta: $beta; betan: $betanorm");
645
	        // echo("image$k.jpg:<br/>\n<img src=\"$currfile\"/>");
663
	        // echo("image$k.jpg:<br/>\n<img src=\"$currfile\"/>");
646
	        // best image found, save it
664
	        // best image found, save it
647
	        
665
	        
648
	        if ($betanorm > $maxbetanorm) {
666
	        if ($betanorm > $maxbetanorm) {
649
	        	
667
	        	
650
	            $maxbetanorm = $betanorm;
668
	            $maxbetanorm = $betanorm;
651
	            $maxparam['w'] = $wcrop;
669
	            $maxparam['w'] = $wcrop;
652
	            $maxparam['h'] = $hcrop;
670
	            $maxparam['h'] = $hcrop;
653
	            $maxparam['x'] = $xcrop;
671
	            $maxparam['x'] = $xcrop;
654
	            $maxparam['y'] = $ycrop;
672
	            $maxparam['y'] = $ycrop;
655
	            // $maxfile = $currfile;
673
	            // $maxfile = $currfile;
656
	        }
674
	        }
657
	    }
675
	    }
658
	
676
	
659
        // return image
677
        // return image
660
        $imgcp->cropImage($maxparam['w'], $maxparam['h'], $maxparam['x'], $maxparam['y']);
678
        $imgcp->cropImage($maxparam['w'], $maxparam['h'], $maxparam['x'], $maxparam['y']);
661
        $imgcp->scaleImage($largeur_vignette, $hauteur_vignette);
679
        $imgcp->scaleImage($largeur_vignette, $hauteur_vignette);
662
        $imgcp->writeImage($out);
680
        $imgcp->writeImage($out);
663
	    chmod($out, 0777);
681
	    chmod($out, 0777);
664
	    $img->destroy();
682
	    $img->destroy();
665
	    $imgcp->destroy();
683
	    $imgcp->destroy();
666
	    
684
	    
667
	    $image_sortie = ImageCreateFromJpeg($out);
685
	    $image_sortie = ImageCreateFromJpeg($out);
668
	    unlink($out);
686
	    unlink($out);
669
	    
687
	    
670
	    return $image_sortie;
688
	    return $image_sortie;
671
	}
689
	}
672
	
690
	
673
}
691
}
674
?>
692
?>