Subversion Repositories eFlore/Applications.cel

Rev

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

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