Subversion Repositories eFlore/Applications.cel

Rev

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

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