Subversion Repositories eFlore/Applications.cel

Rev

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

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