Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 1154 Rev 1350
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
 
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
 
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;
-
 
236
		
235
		$chemin_base_fichier = $this->creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, 'O');
237
		$chemin_base_fichier = $this->creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, 'O');
236
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, 'O');
238
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, 'O');
237
 
239
 
238
		$chemin_fichier = $chemin_base_fichier.'/'.$nom_fichier;
240
		$chemin_fichier = $chemin_base_fichier.'/'.$nom_fichier;
239
 
241
 
240
		$deplacement_fichier = $this->stockerImageExterne($fichier['tmp_name'], $chemin_fichier);
242
		$deplacement_fichier = $this->stockerImageExterne($chemin_fichier_origine, $chemin_fichier);
241
 
243
 
242
		if ($deplacement_fichier) {
244
		if ($deplacement_fichier) {
243
 
245
 
244
			$infos_image_originale = $this->obtenirImageEtInfosPourChemin($chemin_fichier);
246
			$infos_image_originale = $this->obtenirImageEtInfosPourChemin($chemin_fichier);
245
			$taux_compression = $this->renvoyerTauxCompressionPourPoids($infos_image_originale['poids_octets']);
247
			$taux_compression = $this->renvoyerTauxCompressionPourPoids($infos_image_originale['poids_octets']);
246
 
248
 
247
			if ($taux_compression < 100 && $this->mode == self::MODE_IMAGEMAGICK) {
249
			if ($taux_compression < 100 && $this->mode == self::MODE_IMAGEMAGICK) {
248
				$this->ecrireImageSurDisqueAvecMeta($chemin_fichier, $taux_compression);
250
				$this->ecrireImageSurDisqueAvecMeta($chemin_fichier, $taux_compression);
249
			}
251
			}
250
 
252
 
251
			$infos_image_originale_stockee = $this->obtenirImageEtInfosPourChemin($chemin_fichier);
253
			$infos_image_originale_stockee = $this->obtenirImageEtInfosPourChemin($chemin_fichier);
252
 
254
 
253
			$formats = $this->getFormats();
255
			$formats = $this->getFormats();
254
 
256
 
255
			// creation de miniatures pour chacuns des formats définis
257
			// creation de miniatures pour chacuns des formats définis
256
			foreach($formats as $format) {
258
			foreach($formats as $format) {
257
				$this->creerEtStockerMiniatureFichierImageSelonFormat($id, $infos_image_originale_stockee, $format);
259
				$this->creerEtStockerMiniatureFichierImageSelonFormat($id, $infos_image_originale_stockee, $format);
258
			}
260
			}
259
 
261
 
260
	  		return true ;
262
	  		return true ;
261
 
263
 
262
		} else {
264
		} else {
263
			$erreur =  'ERROR : probleme durant le déplacement du fichier temporaire \n' ;
265
			$erreur =  'ERROR : probleme durant le déplacement du fichier temporaire \n' ;
264
			$this->logger('CEL_bugs',$erreur);
266
			$this->logger('CEL_bugs',$erreur);
265
  			return false ;
267
  			return false ;
266
		}
268
		}
267
	}
269
	}
268
 
270
 
269
	public function stockerImageExterne($chemin_fichier_temp, $chemin_destination) {
271
	public function stockerImageExterne($chemin_fichier_temp, $chemin_destination) {
270
		if (is_uploaded_file($chemin_fichier_temp)) {
272
		if (is_uploaded_file($chemin_fichier_temp)) {
271
			$deplacement = move_uploaded_file($chemin_fichier_temp, $chemin_destination);
273
			$deplacement = move_uploaded_file($chemin_fichier_temp, $chemin_destination);
272
		} else {
274
		} else {
273
			$deplacement = rename($chemin_fichier_temp, $chemin_destination);
275
			$deplacement = rename($chemin_fichier_temp, $chemin_destination);
274
		}
276
		}
275
 
277
 
276
		return $deplacement;
278
		return $deplacement;
277
	}
279
	}
278
 
280
 
279
	public function creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, $format) {
281
	public function creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, $format) {
280
		$chemin_sur_serveur_final = $this->obtenirDossierPourFormat($id, $format);
282
		$chemin_sur_serveur_final = $this->obtenirDossierPourFormat($id, $format);
281
 
283
 
282
		if (!file_exists($chemin_sur_serveur_final)) {
284
		if (!file_exists($chemin_sur_serveur_final)) {
283
			umask(0);
285
			umask(0);
284
			if (!mkdir($chemin_sur_serveur_final, $this->droits, true)) {
286
			if (!mkdir($chemin_sur_serveur_final, $this->droits, true)) {
285
				$erreur =  'ERROR : probleme durant l\'écriture du dossier '.$format.' \n' ;
287
				$erreur =  'ERROR : probleme durant l\'écriture du dossier '.$format.' \n' ;
286
				$this->logger('CEL_bugs', $erreur);
288
				$this->logger('CEL_bugs', $erreur);
287
				return false;
289
				return false;
288
			}
290
			}
289
		}
291
		}
290
 
292
 
291
		return $chemin_sur_serveur_final;
293
		return $chemin_sur_serveur_final;
292
	}
294
	}
293
 
295
 
294
	public function obtenirDossierPourFormat($id, $format) {
296
	public function obtenirDossierPourFormat($id, $format) {
295
		$chemin_base = $this->config['cel']['chemin_images'];
297
		$chemin_base = $this->config['cel']['chemin_images'];
296
 
298
 
297
		$chemin_sur_serveur = $chemin_base;
299
		$chemin_sur_serveur = $chemin_base;
298
 
300
 
299
		$id = sprintf('%09s', $id);
301
		$id = sprintf('%09s', $id);
300
		$id = wordwrap($id, 3 , '_', true);
302
		$id = wordwrap($id, 3 , '_', true);
301
 
303
 
302
		list($dossierNiveau1, $dossierNiveau2) = explode('_', $id);
304
		list($dossierNiveau1, $dossierNiveau2) = explode('_', $id);
303
 
305
 
304
		$chemin_sur_serveur_final = $chemin_sur_serveur.'/'.$dossierNiveau1.'/'.$dossierNiveau2.'/'.$format;
306
		$chemin_sur_serveur_final = $chemin_sur_serveur.'/'.$dossierNiveau1.'/'.$dossierNiveau2.'/'.$format;
305
 
307
 
306
		return $chemin_sur_serveur_final;
308
		return $chemin_sur_serveur_final;
307
	}
309
	}
308
 
310
 
309
	public function obtenirCheminImageOriginale($id_image) {
311
	public function obtenirCheminImageOriginale($id_image) {
310
		$nom = $this->convertirIdBddVersNomFichier($id_image, 'O');
312
		$nom = $this->convertirIdBddVersNomFichier($id_image, 'O');
311
		$dossier = $this->obtenirDossierPourFormat($id_image,'O');
313
		$dossier = $this->obtenirDossierPourFormat($id_image,'O');
312
 
314
 
313
		return $dossier.'/'.$nom;
315
		return $dossier.'/'.$nom;
314
	}
316
	}
315
 
317
 
316
	public function obtenirImageEtInfosPourId($id_image) {
318
	public function obtenirImageEtInfosPourId($id_image) {
317
		$chemin_image_o = $this->obtenirCheminImageOriginale($id_image);
319
		$chemin_image_o = $this->obtenirCheminImageOriginale($id_image);
318
		return $this->obtenirImageEtInfosPourChemin($chemin_image_o);
320
		return $this->obtenirImageEtInfosPourChemin($chemin_image_o);
319
	}
321
	}
320
 
322
 
321
	public function obtenirImageEtInfosPourChemin($chemin_fichier) {
323
	public function obtenirImageEtInfosPourChemin($chemin_fichier) {
322
		$image_et_infos = false;
324
		$image_et_infos = false;
323
 
325
 
324
		if (file_exists($chemin_fichier)) {
326
		if (file_exists($chemin_fichier)) {
325
			$image_et_infos = array();
327
			$image_et_infos = array();
326
			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);
327
			$image_et_infos['poids_octets'] = filesize($chemin_fichier);
329
			$image_et_infos['poids_octets'] = filesize($chemin_fichier);
328
			$image_et_infos['image'] = imagecreatefromjpeg($chemin_fichier);
330
			$image_et_infos['image'] = imagecreatefromjpeg($chemin_fichier);
329
			$image_et_infos['chemin'] = $chemin_fichier;
331
			$image_et_infos['chemin'] = $chemin_fichier;
330
		}
332
		}
331
 
333
 
332
		return $image_et_infos;
334
		return $image_et_infos;
333
	}
335
	}
334
 
336
 
335
	public function obtenirDimensionsPourFormat($format) {
337
	public function obtenirDimensionsPourFormat($format) {
336
		$dimensions = array('largeur' => 0, 'hauteur' => 0);
338
		$dimensions = array('largeur' => 0, 'hauteur' => 0);
337
 
339
 
338
		if (isset($this->config['cel']['format_'.$format])) {
340
		if (isset($this->config['cel']['format_'.$format])) {
339
			list($dimensions['largeur'], $dimensions['hauteur']) = explode('_', $this->config['cel']['format_'.$format]);
341
			list($dimensions['largeur'], $dimensions['hauteur']) = explode('_', $this->config['cel']['format_'.$format]);
340
		}
342
		}
341
 
343
 
342
		return $dimensions;
344
		return $dimensions;
343
	}
345
	}
344
 
346
 
345
	public function calculerTailleImage($informations_images, $taille_max) {
347
	public function calculerTailleImage($informations_images, $taille_max) {
346
        $HL_redimension = array();
348
        $HL_redimension = array();
347
 
349
 
348
        if ($this->estPaysage($informations_images)) {
350
        if ($this->estPaysage($informations_images)) {
349
	        $rapport = $informations_images['hauteur']/$informations_images['largeur'] ;
351
	        $rapport = $informations_images['hauteur']/$informations_images['largeur'] ;
350
	        $HL_redimension['largeur'] = round($taille_max) ;
352
	        $HL_redimension['largeur'] = round($taille_max) ;
351
	        $HL_redimension['hauteur'] = round($taille_max*$rapport) ;
353
	        $HL_redimension['hauteur'] = round($taille_max*$rapport) ;
352
 
354
 
353
        } else {
355
        } else {
354
        	$rapport = $informations_images['largeur']/$informations_images['hauteur'] ;
356
        	$rapport = $informations_images['largeur']/$informations_images['hauteur'] ;
355
	        $HL_redimension['hauteur'] = round($taille_max) ;
357
	        $HL_redimension['hauteur'] = round($taille_max) ;
356
	        $HL_redimension['largeur'] = round($taille_max*$rapport) ;
358
	        $HL_redimension['largeur'] = round($taille_max*$rapport) ;
357
        }
359
        }
358
 
360
 
359
        return $HL_redimension;
361
        return $HL_redimension;
360
	}
362
	}
361
 
363
 
362
	public function getFormats() {
364
	public function getFormats() {
363
		return $this->formats;
365
		return $this->formats;
364
	}
366
	}
365
 
367
 
366
	public function estUnFormatCarre($format) {
368
	public function estUnFormatCarre($format) {
367
		return (strpos($format,'C') === 0);
369
		return (strpos($format,'C') === 0);
368
	}
370
	}
369
 
371
 
370
	public function estUnFormatRogne($format) {
372
	public function estUnFormatRogne($format) {
371
		return (strpos($format,'R') === 1);
373
		return (strpos($format,'R') === 1);
372
	}
374
	}
373
 
375
 
374
	public function estPaysage($informations_images) {
376
	public function estPaysage($informations_images) {
375
		return $informations_images['largeur'] > $informations_images['hauteur'];
377
		return $informations_images['largeur'] > $informations_images['hauteur'];
376
	}
378
	}
377
 
379
 
378
	public function estPortait($informations_images) {
380
	public function estPortait($informations_images) {
379
		return $informations_images['largeur'] < $informations_images['hauteur'];
381
		return $informations_images['largeur'] < $informations_images['hauteur'];
380
	}
382
	}
381
 
383
 
382
	public function renvoyerTauxCompressionPourPoids($poids_octets) {
384
	public function renvoyerTauxCompressionPourPoids($poids_octets) {
383
		$poids_max_octets = $this->config['cel']['taille_max'];
385
		$poids_max_octets = $this->config['cel']['taille_max'];
384
 
386
 
385
		$ratio_compression = 100 ;
387
		$ratio_compression = 100 ;
386
 
388
 
387
	    if ($poids_octets >= $poids_max_octets) {
389
	    if ($poids_octets >= $poids_max_octets) {
388
	      $ratio_compression = 75 ;
390
	      $ratio_compression = 75 ;
389
	    }
391
	    }
390
 
392
 
391
	    return $ratio_compression;
393
	    return $ratio_compression;
392
	}
394
	}
393
 
395
 
394
	public function convertirIdBddVersNomFichier($id, $format, $extension = 'jpg') {
396
	public function convertirIdBddVersNomFichier($id, $format, $extension = 'jpg') {
395
		// creation du format original
397
		// creation du format original
396
		$id_avec_zeros = sprintf('%09s', $id) ;
398
		$id_avec_zeros = sprintf('%09s', $id) ;
397
		$id_avec_zeros_underscores = wordwrap($id_avec_zeros, 3 , '_', true) ;
399
		$id_avec_zeros_underscores = wordwrap($id_avec_zeros, 3 , '_', true) ;
398
 
400
 
399
		$nom_fichier = $id_avec_zeros_underscores.'_'.$format.'.'.$extension;
401
		$nom_fichier = $id_avec_zeros_underscores.'_'.$format.'.'.$extension;
400
 
402
 
401
		return $nom_fichier;
403
		return $nom_fichier;
402
	}
404
	}
403
 
405
 
404
	public function convertirBaseNomFichierVersIdBdd($nom_fichier, $formats) {
406
	public function convertirBaseNomFichierVersIdBdd($nom_fichier, $formats) {
405
		$nom_fichier_sans_extension = trim($nom_fichier, '.jpg');
407
		$nom_fichier_sans_extension = trim($nom_fichier, '.jpg');
406
 
408
 
407
		foreach($formats as $format) {
409
		foreach($formats as $format) {
408
			$nom_fichier_sans_extension = trim($nom_fichier_sans_extension, '_'.$format);
410
			$nom_fichier_sans_extension = trim($nom_fichier_sans_extension, '_'.$format);
409
		}
411
		}
410
 
412
 
411
		$id_image = str_replace('_', '', $nom_fichier_sans_extension);
413
		$id_image = str_replace('_', '', $nom_fichier_sans_extension);
412
 
414
 
413
		// suppression des 0 devant
415
		// suppression des 0 devant
414
		$id_image += 0;
416
		$id_image += 0;
415
 
417
 
416
		return $id_image;
418
		return $id_image;
417
	}
419
	}
418
 
420
 
419
	public function ecrireImageSurDisque($image_binaire, $id, $format, $compression = 100) {
421
	public function ecrireImageSurDisque($image_binaire, $id, $format, $compression = 100) {
420
		umask(0);
422
		umask(0);
421
 
423
 
422
		$chemin_sur_serveur_final = $this->creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, $format);
424
		$chemin_sur_serveur_final = $this->creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, $format);
423
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, $format);
425
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, $format);
424
 
426
 
425
		if (file_exists($chemin_sur_serveur_final.'/'.$nom_fichier)) {
427
		if (file_exists($chemin_sur_serveur_final.'/'.$nom_fichier)) {
426
			unlink($chemin_sur_serveur_final.'/'.$nom_fichier);
428
			unlink($chemin_sur_serveur_final.'/'.$nom_fichier);
427
		}
429
		}
428
 
430
 
429
		// attention, ceci ne preserve pas les metadonnées
431
		// attention, ceci ne preserve pas les metadonnées
430
		imagejpeg($image_binaire, $chemin_sur_serveur_final.'/'.$nom_fichier, $compression);
432
		imagejpeg($image_binaire, $chemin_sur_serveur_final.'/'.$nom_fichier, $compression);
431
		chmod($chemin_sur_serveur_final.'/'.$nom_fichier,$this->droits);
433
		chmod($chemin_sur_serveur_final.'/'.$nom_fichier,$this->droits);
432
	}
434
	}
433
 
435
 
434
	public function ecrireImageSurDisqueAvecMeta($chemin_image_a_stocker, $compression = 100) {
436
	public function ecrireImageSurDisqueAvecMeta($chemin_image_a_stocker, $compression = 100) {
435
		$img = new Imagick($chemin_image_a_stocker);
437
		$img = new Imagick($chemin_image_a_stocker);
436
 
438
 
437
		// 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
438
		$img->setformat("jpeg");
440
		$img->setformat("jpeg");
439
		$img->setImageCompression(imagick::COMPRESSION_JPEG);
441
		$img->setImageCompression(imagick::COMPRESSION_JPEG);
440
		$img->setCompressionQuality($compression);
442
		$img->setCompressionQuality($compression);
441
		$img->writeImage($chemin_image_a_stocker);
443
		$img->writeImage($chemin_image_a_stocker);
442
		$img->destroy();
444
		$img->destroy();
443
 
445
 
444
		chmod($chemin_image_a_stocker, $this->droits);
446
		chmod($chemin_image_a_stocker, $this->droits);
445
	}
447
	}
446
 
448
 
447
	public function renvoyerEtCreerImageCarreeBlancheSelonFormat($cote) {
449
	public function renvoyerEtCreerImageCarreeBlancheSelonFormat($cote) {
448
		$image_blanche = imagecreatetruecolor($cote, $cote);
450
		$image_blanche = imagecreatetruecolor($cote, $cote);
449
		$blanc = imagecolorallocate($image_blanche, 255, 255, 255);
451
		$blanc = imagecolorallocate($image_blanche, 255, 255, 255);
450
		imagefilledrectangle($image_blanche, 0, 0, $cote, $cote, $blanc);
452
		imagefilledrectangle($image_blanche, 0, 0, $cote, $cote, $blanc);
451
 
453
 
452
		return $image_blanche;
454
		return $image_blanche;
453
	}
455
	}
454
 
456
 
455
	public function detruireImageEnMemoire($image) {
457
	public function detruireImageEnMemoire($image) {
456
		imagedestroy($image);
458
		imagedestroy($image);
457
	}
459
	}
458
 
460
 
459
	public function detruireImageSurDisque($id) {
461
	public function detruireImageSurDisque($id) {
460
		$formats = $this->getFormats();
462
		$formats = $this->getFormats();
461
 
463
 
462
		// on detruit aussi l'image originale
464
		// on detruit aussi l'image originale
463
		$formats[] = 'O';
465
		$formats[] = 'O';
464
 
466
 
465
		$destruction_formats_fichier = false;
467
		$destruction_formats_fichier = false;
466
 
468
 
467
		// destructions de chacuns des formats définis
469
		// destructions de chacuns des formats définis
468
		foreach($formats as $format) {
470
		foreach($formats as $format) {
469
 
471
 
470
			$dossier_format = $this->obtenirDossierPourFormat($id, $format);
472
			$dossier_format = $this->obtenirDossierPourFormat($id, $format);
471
			$nom_fichier = $this->convertirIdBddVersNomFichier($id, $format);
473
			$nom_fichier = $this->convertirIdBddVersNomFichier($id, $format);
472
 
474
 
473
			if (file_exists($dossier_format.'/'.$nom_fichier)) {
475
			if (file_exists($dossier_format.'/'.$nom_fichier)) {
474
				$destruction_formats_fichier = unlink($dossier_format.'/'.$nom_fichier);
476
				$destruction_formats_fichier = unlink($dossier_format.'/'.$nom_fichier);
475
			} else {
477
			} else {
476
				$destruction_formats_fichier = true;
478
				$destruction_formats_fichier = true;
477
			}
479
			}
478
		}
480
		}
479
 
481
 
480
		return $destruction_formats_fichier;
482
		return $destruction_formats_fichier;
481
	}
483
	}
482
 
484
 
483
	/*
485
	/*
484
	 * edge-maximizing crop
486
	 * edge-maximizing crop
485
	 * determines center-of-edginess, then tries different-sized crops around it.
487
	 * determines center-of-edginess, then tries different-sized crops around it.
486
	 * picks the crop with the highest normalized edginess.
488
	 * picks the crop with the highest normalized edginess.
487
	 * see documentation on how to tune the algorithm
489
	 * see documentation on how to tune the algorithm
488
	 *
490
	 *
489
	 * $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
490
	 * $format - le format (ex. : CS, XS, XL, CRS)
492
	 * $format - le format (ex. : CS, XS, XL, CRS)
491
	*/
493
	*/
492
	public function opticrop($informations_image, $format) {
494
	public function opticrop($informations_image, $format) {
493
		umask(0);
495
		umask(0);
494
 
496
 
495
		$nom_temp = md5(time());
497
		$nom_temp = md5(time());
496
		$chemin_temp =
498
		$chemin_temp =
497
 
499
 
498
		$out = $this->config['cel']['chemin_stockage_temp'].'/'.$nom_temp;
500
		$out = $this->config['cel']['chemin_stockage_temp'].'/'.$nom_temp;
499
 
501
 
500
		$dimension_vignettes = $this->obtenirDimensionsPourFormat($format);
502
		$dimension_vignettes = $this->obtenirDimensionsPourFormat($format);
501
 
503
 
502
		$largeur_vignette = $dimension_vignettes['largeur'];
504
		$largeur_vignette = $dimension_vignettes['largeur'];
503
		$hauteur_vignette = $dimension_vignettes['hauteur'];
505
		$hauteur_vignette = $dimension_vignettes['hauteur'];
504
 
506
 
505
	    // source dimensions
507
	    // source dimensions
506
	    $largeur_image_originale = $informations_image['largeur'];
508
	    $largeur_image_originale = $informations_image['largeur'];
507
	    $hauteur_image_originale = $informations_image['hauteur'];
509
	    $hauteur_image_originale = $informations_image['hauteur'];
508
 
510
 
509
	    $chemin_image = $informations_image['chemin'];
511
	    $chemin_image = $informations_image['chemin'];
510
 
512
 
511
	    //if ($largeur_vignette > $largeur_image_originale || $hauteur_vignette > $hauteur_image_originale)
513
	    //if ($largeur_vignette > $largeur_image_originale || $hauteur_vignette > $hauteur_image_originale)
512
	    //    die("Target dimensions must be smaller or equal to source dimensions.");
514
	    //    die("Target dimensions must be smaller or equal to source dimensions.");
513
 
515
 
514
	    // parameters for the edge-maximizing crop algorithm
516
	    // parameters for the edge-maximizing crop algorithm
515
	    $r = 1;         // radius of edge filter
517
	    $r = 1;         // radius of edge filter
516
	    $nk = 9;        // scale count: number of crop sizes to try
518
	    $nk = 9;        // scale count: number of crop sizes to try
517
	    $gamma = 0.2;   // edge normalization parameter -- see documentation
519
	    $gamma = 0.2;   // edge normalization parameter -- see documentation
518
	    $ar = $largeur_vignette/$hauteur_vignette;    // target aspect ratio (AR)
520
	    $ar = $largeur_vignette/$hauteur_vignette;    // target aspect ratio (AR)
519
	    $ar0 = $largeur_image_originale/$hauteur_image_originale;    // original aspect ratio (AR)
521
	    $ar0 = $largeur_image_originale/$hauteur_image_originale;    // original aspect ratio (AR)
520
 
522
 
521
	    //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");
522
	    $img = new Imagick($chemin_image);
524
	    $img = new Imagick($chemin_image);
523
	    $imgcp = clone $img;
525
	    $imgcp = clone $img;
524
 
526
 
525
	    // compute center of edginess
527
	    // compute center of edginess
526
	    $img->edgeImage($r);
528
	    $img->edgeImage($r);
527
	    $img->modulateImage(100,0,100); // grayscale
529
	    $img->modulateImage(100,0,100); // grayscale
528
	    $img->blackThresholdImage("#0f0f0f");
530
	    $img->blackThresholdImage("#0f0f0f");
529
	    $img->writeImage($out);
531
	    $img->writeImage($out);
530
	    // use gd for random pixel access
532
	    // use gd for random pixel access
531
	    $im = ImageCreateFromJpeg($out);
533
	    $im = ImageCreateFromJpeg($out);
532
	    $xcenter = 0;
534
	    $xcenter = 0;
533
	    $ycenter = 0;
535
	    $ycenter = 0;
534
	    $sum = 0;
536
	    $sum = 0;
535
	    $n = 100000;
537
	    $n = 100000;
536
	    for ($k=0; $k<$n; $k++) {
538
	    for ($k=0; $k<$n; $k++) {
537
	        $i = mt_rand(0,$largeur_image_originale-1);
539
	        $i = mt_rand(0,$largeur_image_originale-1);
538
	        $j = mt_rand(0,$hauteur_image_originale-1);
540
	        $j = mt_rand(0,$hauteur_image_originale-1);
539
	        $val = imagecolorat($im, $i, $j) & 0xFF;
541
	        $val = imagecolorat($im, $i, $j) & 0xFF;
540
	        $sum += $val;
542
	        $sum += $val;
541
	        $xcenter += ($i+1)*$val;
543
	        $xcenter += ($i+1)*$val;
542
	        $ycenter += ($j+1)*$val;
544
	        $ycenter += ($j+1)*$val;
543
	    }
545
	    }
544
	    $xcenter /= $sum;
546
	    $xcenter /= $sum;
545
	    $ycenter /= $sum;
547
	    $ycenter /= $sum;
546
 
548
 
547
	    // crop source img to target AR
549
	    // crop source img to target AR
548
	    if ($largeur_image_originale/$hauteur_image_originale > $ar) {
550
	    if ($largeur_image_originale/$hauteur_image_originale > $ar) {
549
	        // source AR wider than target
551
	        // source AR wider than target
550
	        // crop width to target AR
552
	        // crop width to target AR
551
	        $wcrop0 = round($ar*$hauteur_image_originale);
553
	        $wcrop0 = round($ar*$hauteur_image_originale);
552
	        $hcrop0 = $hauteur_image_originale;
554
	        $hcrop0 = $hauteur_image_originale;
553
	    } else {
555
	    } else {
554
	        // crop height to target AR
556
	        // crop height to target AR
555
	        $wcrop0 = $largeur_image_originale;
557
	        $wcrop0 = $largeur_image_originale;
556
	        $hcrop0 = round($largeur_image_originale/$ar);
558
	        $hcrop0 = round($largeur_image_originale/$ar);
557
	    }
559
	    }
558
 
560
 
559
	    // crop parameters for all scales and translations
561
	    // crop parameters for all scales and translations
560
	    $params = array();
562
	    $params = array();
561
 
563
 
562
	    // crop at different scales
564
	    // crop at different scales
563
	    $hgap = $hcrop0 - $hauteur_vignette;
565
	    $hgap = $hcrop0 - $hauteur_vignette;
564
	    $hinc = ($nk == 1) ? 0 : $hgap / ($nk - 1);
566
	    $hinc = ($nk == 1) ? 0 : $hgap / ($nk - 1);
565
	    $wgap = $wcrop0 - $largeur_vignette;
567
	    $wgap = $wcrop0 - $largeur_vignette;
566
	    $winc = ($nk == 1) ? 0 : $wgap / ($nk - 1);
568
	    $winc = ($nk == 1) ? 0 : $wgap / ($nk - 1);
567
 
569
 
568
	    // find window with highest normalized edginess
570
	    // find window with highest normalized edginess
569
	    $n = 10000;
571
	    $n = 10000;
570
	    $maxbetanorm = 0;
572
	    $maxbetanorm = 0;
571
	    $maxfile = '';
573
	    $maxfile = '';
572
	    $maxparam = array('w'=>0, 'h'=>0, 'x'=>0, 'y'=>0);
574
	    $maxparam = array('w'=>0, 'h'=>0, 'x'=>0, 'y'=>0);
573
 
575
 
574
	    for ($k = 0; $k < $nk; $k++) {
576
	    for ($k = 0; $k < $nk; $k++) {
575
	        $hcrop = round($hcrop0 - $k*$hinc);
577
	        $hcrop = round($hcrop0 - $k*$hinc);
576
	        $wcrop = round($wcrop0 - $k*$winc);
578
	        $wcrop = round($wcrop0 - $k*$winc);
577
	        $xcrop = $xcenter - $wcrop / 2;
579
	        $xcrop = $xcenter - $wcrop / 2;
578
	        $ycrop = $ycenter - $hcrop / 2;
580
	        $ycrop = $ycenter - $hcrop / 2;
579
	        //echo("crop: $wcrop, $hcrop, $xcrop, $ycrop");
581
	        //echo("crop: $wcrop, $hcrop, $xcrop, $ycrop");
580
 
582
 
581
	        if ($xcrop < 0) $xcrop = 0;
583
	        if ($xcrop < 0) $xcrop = 0;
582
	        if ($xcrop+$wcrop > $largeur_image_originale) $xcrop = $largeur_image_originale-$wcrop;
584
	        if ($xcrop+$wcrop > $largeur_image_originale) $xcrop = $largeur_image_originale-$wcrop;
583
	        if ($ycrop < 0) $ycrop = 0;
585
	        if ($ycrop < 0) $ycrop = 0;
584
	        if ($ycrop+$hcrop > $hauteur_image_originale) $ycrop = $hauteur_image_originale-$hcrop;
586
	        if ($ycrop+$hcrop > $hauteur_image_originale) $ycrop = $hauteur_image_originale-$hcrop;
585
 
587
 
586
	        /*if (self::MODE_DEBUG) {
588
	        /*if (self::MODE_DEBUG) {
587
	        	// debug
589
	        	// debug
588
	        	$currfile = '/home/aurelien/web/file_tmp/'."image$k.jpg";
590
	        	$currfile = '/home/aurelien/web/file_tmp/'."image$k.jpg";
589
 
591
 
590
	            $currimg = clone $img;
592
	            $currimg = clone $img;
591
	            $c= new ImagickDraw();
593
	            $c= new ImagickDraw();
592
	            $c->setFillColor("red");
594
	            $c->setFillColor("red");
593
	            $c->circle($xcenter, $ycenter, $xcenter, $ycenter+4);
595
	            $c->circle($xcenter, $ycenter, $xcenter, $ycenter+4);
594
	            $currimg->drawImage($c);
596
	            $currimg->drawImage($c);
595
	            $currimg->cropImage($wcrop, $hcrop, $xcrop, $ycrop);
597
	            $currimg->cropImage($wcrop, $hcrop, $xcrop, $ycrop);
596
	            $currimg->writeImage($currfile);
598
	            $currimg->writeImage($currfile);
597
	            $currimg->destroy();
599
	            $currimg->destroy();
598
	        }*/
600
	        }*/
599
 
601
 
600
	        $beta = 0;
602
	        $beta = 0;
601
	        for ($c=0; $c<$n; $c++) {
603
	        for ($c=0; $c<$n; $c++) {
602
	            $i = mt_rand(0,$wcrop-1);
604
	            $i = mt_rand(0,$wcrop-1);
603
	            $j = mt_rand(0,$hcrop-1);
605
	            $j = mt_rand(0,$hcrop-1);
604
	            $beta += imagecolorat($im, $xcrop+$i, $ycrop+$j) & 0xFF;
606
	            $beta += imagecolorat($im, $xcrop+$i, $ycrop+$j) & 0xFF;
605
	        }
607
	        }
606
	        $area = $wcrop * $hcrop;
608
	        $area = $wcrop * $hcrop;
607
	        $betanorm = $beta / ($n*pow($area, $gamma-1));
609
	        $betanorm = $beta / ($n*pow($area, $gamma-1));
608
	        // echo("beta: $beta; betan: $betanorm");
610
	        // echo("beta: $beta; betan: $betanorm");
609
	        // echo("image$k.jpg:<br/>\n<img src=\"$currfile\"/>");
611
	        // echo("image$k.jpg:<br/>\n<img src=\"$currfile\"/>");
610
	        // best image found, save it
612
	        // best image found, save it
611
 
613
 
612
	        if ($betanorm > $maxbetanorm) {
614
	        if ($betanorm > $maxbetanorm) {
613
 
615
 
614
	            $maxbetanorm = $betanorm;
616
	            $maxbetanorm = $betanorm;
615
	            $maxparam['w'] = $wcrop;
617
	            $maxparam['w'] = $wcrop;
616
	            $maxparam['h'] = $hcrop;
618
	            $maxparam['h'] = $hcrop;
617
	            $maxparam['x'] = $xcrop;
619
	            $maxparam['x'] = $xcrop;
618
	            $maxparam['y'] = $ycrop;
620
	            $maxparam['y'] = $ycrop;
619
	            // $maxfile = $currfile;
621
	            // $maxfile = $currfile;
620
	        }
622
	        }
621
	    }
623
	    }
622
 
624
 
623
        // return image
625
        // return image
624
        $imgcp->cropImage($maxparam['w'], $maxparam['h'], $maxparam['x'], $maxparam['y']);
626
        $imgcp->cropImage($maxparam['w'], $maxparam['h'], $maxparam['x'], $maxparam['y']);
625
        $imgcp->scaleImage($largeur_vignette, $hauteur_vignette);
627
        $imgcp->scaleImage($largeur_vignette, $hauteur_vignette);
626
        $imgcp->writeImage($out);
628
        $imgcp->writeImage($out);
627
	    chmod($out, 0777);
629
	    chmod($out, 0777);
628
	    $img->destroy();
630
	    $img->destroy();
629
	    $imgcp->destroy();
631
	    $imgcp->destroy();
630
 
632
 
631
	    $image_sortie = ImageCreateFromJpeg($out);
633
	    $image_sortie = ImageCreateFromJpeg($out);
632
	    unlink($out);
634
	    unlink($out);
633
 
635
 
634
	    return $image_sortie;
636
	    return $image_sortie;
635
	}
637
	}
636
}
638
}
637
?>
639
?>