Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 591 Rev 605
1
<?php
1
<?php
2
Class ImageRecreation extends DBAccessor {
2
Class ImageRecreation extends DBAccessor {
3
	
3
	
4
	private $droits = 0777;
4
	private $droits = 0755;
5
	private $formats = array('CRX2S','CXS','CS','S','M','L','XL','X2L','X3L');
5
	private $formats = array('CRX2S','CXS','CS','S','M','L','XL','X2L','X3L');
6
 
6
 
7
	public function ImageRecreation($config) {
7
	public function ImageRecreation($config) {
8
 
8
 
9
		$this->config=$config;
9
		$this->config=$config;
10
	}
10
	}
11
	
11
	
12
	public function getRessource() {
12
	public function getRessource() {
13
		return $this->getElement(array());
-
 
-
 
13
		
14
	}
14
	}
15
	
15
	
16
	public function getElement($param) {
16
	public function getElement($param) {
17
		
-
 
18
		ini_set ('max_execution_time',0);
-
 
19
		$this->itererRecursivement($this->config['cel_db']['chemin_images']);
17
		
20
	}
18
	}
-
 
19
	
-
 
20
	public function recreerMiniaturesRecursivement() {
21
	
-
 
22
	public function itererRecursivement($dossier, $formats = null) {
21
		$this->itererRecursivement($this->config['cel_db']['chemin_images']);
23
	
-
 
24
		if($formats == null) {
22
	} 
25
			$formats = $this->getFormats();		
23
	
26
		}
24
	public function itererRecursivement($dossier) {
27
		
25
		
28
		// on ne parse que le dossier des images originales
26
		// on ne parse que le dossier des images originales
29
		$dossiers_a_exclure = $this->getFormats();
27
		$dossiers_a_exclure = $this->getFormats();
30
				
28
				
31
		foreach (new DirectoryIterator($dossier) as $fichier_ou_dossier) {
29
		foreach (new DirectoryIterator($dossier) as $fichier_ou_dossier) {
32
							
30
							
33
			if($fichier_ou_dossier->isDot()) {
31
			if($fichier_ou_dossier->isDot()) {
34
				continue;
32
				continue;
35
			}
33
			}
36
			
34
			
37
			if(in_array($fichier_ou_dossier->getBasename(), $dossiers_a_exclure)) {
35
			if(in_array($fichier_ou_dossier->getBasename(), $dossiers_a_exclure)) {
38
				continue;
36
				continue;
39
			}
37
			}
40
			
38
			
41
		    if($fichier_ou_dossier->isDir()) {
39
		    if($fichier_ou_dossier->isDir()) {
42
		    	
40
		    	
43
		    	$profondeur_dossier_fils = $profondeur + 1;
41
		    	$profondeur_dossier_fils = $profondeur + 1;
44
	    		$this->itererRecursivement($fichier_ou_dossier->getPathname(), $profondeur_dossier_fils);
42
	    		$this->itererRecursivement($fichier_ou_dossier->getPathname(), $profondeur_dossier_fils);
45
	    		
43
	    		
46
		    } else {	
44
		    } else {	
47
		    	    	
45
		    	    	
48
		    	$nom_fichier = $fichier_ou_dossier->getFilename();
46
		    	$nom_fichier = $fichier_ou_dossier->getFilename();
49
		    	
47
		    	
50
				$infos_image_originale = $this->obtenirImageEtInfosPourChemin($fichier_ou_dossier->getPathname());
48
				$infos_image_originale = $this->obtenirImageEtInfosPourChemin($fichier_ou_dossier->getPathname());
51
		    	$id = $this->convertirBaseNomFichierVersIdBdd($nom_fichier, $formats);
49
		    	$id = $this->convertirBaseNomFichierVersIdBdd($nom_fichier, $this->formats);
52
				
50
				
53
		    	// creation de miniatures pour chacuns des formats définis
51
		    	// creation de miniatures pour chacuns des formats définis
54
				foreach($formats as $format) {
52
				foreach($this->formats as $format) {
55
					
53
					
56
					$this->creerEtStockerMiniatureFichierImageSelonFormat($id, $infos_image_originale, $format);
54
					$this->creerEtStockerMiniatureFichierImageSelonFormat($id, $infos_image_originale, $format);
57
				}
55
				}
58
		    }
56
		    }
59
		}
57
		}
60
	}
58
	}
61
			
59
			
62
	public function creerEtStockerMiniatureFichierImageSelonFormat($id ,$infos_image_originale, $format = 'O') {
60
	public function creerEtStockerMiniatureFichierImageSelonFormat($id ,$infos_image_originale, $format = 'O') {
63
		
61
		
64
		if($format == 'O') {		
62
		if($format == 'O') {		
65
			// format original : rien à faire
63
			// format original : rien à faire
66
			$image_redimensionnee = $infos_image_originale['image'];
64
			$image_redimensionnee = $infos_image_originale['image'];
67
			
65
			
68
		} else {
66
		} else {
69
			 if($this->estUnFormatRogne($format)) {
67
			 if($this->estUnFormatRogne($format)) {
70
				// la minature est une image redimensionnée rognée au centre
68
				// la minature est une image redimensionnée rognée au centre
71
				$image_redimensionnee = $this->creerMiniatureCarreeRognee($infos_image_originale, $format); 														
69
				$image_redimensionnee = $this->creerMiniatureCarreeRognee($infos_image_originale, $format); 														
72
			} else if($this->estUnFormatCarre($format)) {
70
			} else if($this->estUnFormatCarre($format)) {
73
				// le format carre et une image redimensionnée en gardant son ratio, insérée dans un carré blanc
71
				// le format carre et une image redimensionnée en gardant son ratio, insérée dans un carré blanc
74
				$image_redimensionnee = $this->creerMiniatureCarree($infos_image_originale, $format);				
72
				$image_redimensionnee = $this->creerMiniatureCarree($infos_image_originale, $format);				
75
			} else {
73
			} else {
76
				$image_redimensionnee = $this->creerMiniature($infos_image_originale, $format);			
74
				$image_redimensionnee = $this->creerMiniature($infos_image_originale, $format);			
77
			}
75
			}
78
		}
76
		}
79
				
77
				
80
		$taux_compression = $this->renvoyerTauxCompressionPourPoids($infos_image_originale['poids_octets']);
78
		$taux_compression = $this->renvoyerTauxCompressionPourPoids($infos_image_originale['poids_octets']);
81
		$this->ecrireImageSurDisque($image_redimensionnee, $id, $format, $taux_compression);
79
		$this->ecrireImageSurDisque($image_redimensionnee, $id, $format, $taux_compression);
82
				
80
				
83
		return true;
81
		return true;
84
	}
82
	}
85
	
83
	
86
	public function creerImageRedimensionnee($infos_image_originale, $hauteur_redimension, $largeur_redimension) {
84
	public function creerImageRedimensionnee($infos_image_originale, $hauteur_redimension, $largeur_redimension) {
87
		
85
		
88
		$image_redimensionnee = imagecreatetruecolor($largeur_redimension, $hauteur_redimension);
86
		$image_redimensionnee = imagecreatetruecolor($largeur_redimension, $hauteur_redimension);
89
		
87
		
90
		imagecopyresampled($image_redimensionnee, 
88
		imagecopyresampled($image_redimensionnee, 
91
						$infos_image_originale['image'], 
89
						$infos_image_originale['image'], 
92
						0, 0, 
90
						0, 0, 
93
						0, 0, 
91
						0, 0, 
94
						$largeur_redimension, 
92
						$largeur_redimension, 
95
						$hauteur_redimension, 
93
						$hauteur_redimension, 
96
						$infos_image_originale['largeur'], 
94
						$infos_image_originale['largeur'], 
97
						$infos_image_originale['hauteur']
95
						$infos_image_originale['hauteur']
98
		);
96
		);
99
		
97
		
100
		return $image_redimensionnee;
98
		return $image_redimensionnee;
101
	}
99
	}
102
	
100
	
103
	public function creerMiniature($informations_images, $format) {
101
	public function creerMiniature($informations_images, $format) {
104
				
102
				
105
		$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
103
		$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
106
		
104
		
107
		$taille_image_redimensionnee = $this->calculerTailleImage($informations_images, $taille_reference_pour_format['hauteur']);
105
		$taille_image_redimensionnee = $this->calculerTailleImage($informations_images, $taille_reference_pour_format['hauteur']);
108
		$image_redimensionnee = $this->creerImageRedimensionnee($informations_images, $taille_image_redimensionnee['hauteur'], $taille_image_redimensionnee['largeur']);
106
		$image_redimensionnee = $this->creerImageRedimensionnee($informations_images, $taille_image_redimensionnee['hauteur'], $taille_image_redimensionnee['largeur']);
109
		
107
		
110
		return $image_redimensionnee;
108
		return $image_redimensionnee;
111
	}
109
	}
112
	
110
	
113
	public function creerMiniatureCarree($informations_image, $format) {
111
	public function creerMiniatureCarree($informations_image, $format) {
114
		
112
		
115
		$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
113
		$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
116
		$cote_carre = $taille_reference_pour_format['largeur'];
114
		$cote_carre = $taille_reference_pour_format['largeur'];
117
		
115
		
118
		$image_redimensionnee_avec_rapport = $this->creerMiniature($informations_image, $format);
116
		$image_redimensionnee_avec_rapport = $this->creerMiniature($informations_image, $format);
119
		$taille_redimensionnee_avec_rapport = $this->calculerTailleImage($informations_image, $taille_reference_pour_format['hauteur']);
117
		$taille_redimensionnee_avec_rapport = $this->calculerTailleImage($informations_image, $taille_reference_pour_format['hauteur']);
120
		
118
		
121
		if($this->estPaysage($informations_image)) {				
119
		if($this->estPaysage($informations_image)) {				
122
				$debut_largeur_a_copier = 0 ;
120
				$debut_largeur_a_copier = 0 ;
123
				$debut_hauteur_a_copier = ($cote_carre - $taille_redimensionnee_avec_rapport['hauteur'])/2 ;	
121
				$debut_hauteur_a_copier = ($cote_carre - $taille_redimensionnee_avec_rapport['hauteur'])/2 ;	
124
		} else {
122
		} else {
125
				$debut_largeur_a_copier = ($cote_carre - $taille_redimensionnee_avec_rapport['largeur'])/2 ;
123
				$debut_largeur_a_copier = ($cote_carre - $taille_redimensionnee_avec_rapport['largeur'])/2 ;
126
				$debut_hauteur_a_copier = 0 ;	
124
				$debut_hauteur_a_copier = 0 ;	
127
		}
125
		}
128
		
126
		
129
		$image_carre_blanc_cible = $this->renvoyerEtCreerImageCarreeBlancheSelonFormat($cote_carre);
127
		$image_carre_blanc_cible = $this->renvoyerEtCreerImageCarreeBlancheSelonFormat($cote_carre);
130
		
128
		
131
		imagecopy($image_carre_blanc_cible, $image_redimensionnee_avec_rapport,
129
		imagecopy($image_carre_blanc_cible, $image_redimensionnee_avec_rapport,
132
				$debut_largeur_a_copier ,$debut_hauteur_a_copier, 0, 0,
130
				$debut_largeur_a_copier ,$debut_hauteur_a_copier, 0, 0,
133
				$taille_redimensionnee_avec_rapport['largeur'], $taille_redimensionnee_avec_rapport['hauteur']
131
				$taille_redimensionnee_avec_rapport['largeur'], $taille_redimensionnee_avec_rapport['hauteur']
134
		);
132
		);
135
 
133
 
136
		return $image_carre_blanc_cible;
134
		return $image_carre_blanc_cible;
137
	}
135
	}
138
	
136
	
139
	public function creerMiniatureCarreeRognee($informations_image, $format) {
137
	public function creerMiniatureCarreeRognee($informations_image, $format) {
140
		
138
		
141
		$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
139
		$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
142
		$cote_carre = $taille_reference_pour_format['largeur'];
140
		$cote_carre = $taille_reference_pour_format['largeur'];
143
		$cote_carre_non_redimensionne = 0;
141
		$cote_carre_non_redimensionne = 0;
144
		
142
		
145
		if($this->estPaysage($informations_image)) {	
143
		if($this->estPaysage($informations_image)) {	
146
				$cote_carre_non_redimensionne = $informations_image['hauteur'];		
144
				$cote_carre_non_redimensionne = $informations_image['hauteur'];		
147
				$debut_largeur_a_copier = ($informations_image['hauteur'] - $informations_image['hauteur'])/2 ;
145
				$debut_largeur_a_copier = ($informations_image['hauteur'] - $informations_image['hauteur'])/2 ;
148
				$debut_hauteur_a_copier = 0;
146
				$debut_hauteur_a_copier = 0;
149
				$nb_pixels_largeur_a_copier = $informations_image['hauteur'];
147
				$nb_pixels_largeur_a_copier = $informations_image['hauteur'];
150
				$nb_pixels_hauteur_a_copier = $informations_image['hauteur'];
148
				$nb_pixels_hauteur_a_copier = $informations_image['hauteur'];
151
		} else {
149
		} else {
152
				$cote_carre_non_redimensionne = $informations_image['largeur'];
150
				$cote_carre_non_redimensionne = $informations_image['largeur'];
153
				$debut_largeur_a_copier = 0 ;
151
				$debut_largeur_a_copier = 0 ;
154
				$debut_hauteur_a_copier = ($informations_image['largeur'] - $informations_image['largeur'])/2;
152
				$debut_hauteur_a_copier = ($informations_image['largeur'] - $informations_image['largeur'])/2;
155
				$nb_pixels_largeur_a_copier = $informations_image['largeur'];
153
				$nb_pixels_largeur_a_copier = $informations_image['largeur'];
156
				$nb_pixels_hauteur_a_copier = $informations_image['largeur'];
154
				$nb_pixels_hauteur_a_copier = $informations_image['largeur'];
157
		}
155
		}
158
		
156
		
159
		$image_carre_temporaire = imagecreatetruecolor($cote_carre_non_redimensionne, $cote_carre_non_redimensionne);
157
		$image_carre_temporaire = imagecreatetruecolor($cote_carre_non_redimensionne, $cote_carre_non_redimensionne);
160
		
158
		
161
		imagecopyresampled($image_carre_temporaire, 
159
		imagecopyresampled($image_carre_temporaire, 
162
						$informations_image['image'], 
160
						$informations_image['image'], 
163
						0, 0, 
161
						0, 0, 
164
						$debut_largeur_a_copier,
162
						$debut_largeur_a_copier,
165
						$debut_hauteur_a_copier, 
163
						$debut_hauteur_a_copier, 
166
						$cote_carre_non_redimensionne, 
164
						$cote_carre_non_redimensionne, 
167
						$cote_carre_non_redimensionne, 
165
						$cote_carre_non_redimensionne, 
168
						$nb_pixels_largeur_a_copier, 
166
						$nb_pixels_largeur_a_copier, 
169
						$nb_pixels_hauteur_a_copier
167
						$nb_pixels_hauteur_a_copier
170
		);
168
		);
171
		
169
		
172
		$image_redimensionnee = imagecreatetruecolor($cote_carre, $cote_carre);
170
		$image_redimensionnee = imagecreatetruecolor($cote_carre, $cote_carre);
173
		
171
		
174
		imagecopyresampled($image_redimensionnee, 
172
		imagecopyresampled($image_redimensionnee, 
175
						$image_carre_temporaire, 
173
						$image_carre_temporaire, 
176
						0, 0, 
174
						0, 0, 
177
						0, 0, 
175
						0, 0, 
178
						$cote_carre, 
176
						$cote_carre, 
179
						$cote_carre, 
177
						$cote_carre, 
180
						$cote_carre_non_redimensionne, 
178
						$cote_carre_non_redimensionne, 
181
						$cote_carre_non_redimensionne
179
						$cote_carre_non_redimensionne
182
		);
180
		);
183
 
181
 
184
		return $image_redimensionnee;
182
		return $image_redimensionnee;
185
	}
183
	}
186
	
184
	
187
	public function stockerFichierUploadeEtCreerMiniatures($fichier, $id) {
185
	public function stockerFichierUploadeEtCreerMiniatures($fichier, $id) {
188
		
186
		
189
		$chemin_base_fichier = $this->creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, 'O');	
187
		$chemin_base_fichier = $this->creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, 'O');	
190
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, 'O');
188
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, 'O');
191
	
189
	
192
		$chemin_fichier = $chemin_base_fichier.'/'.$nom_fichier;
190
		$chemin_fichier = $chemin_base_fichier.'/'.$nom_fichier;
193
		
191
		
194
		if(move_uploaded_file($fichier['tmp_name'],$chemin_fichier)) {
192
		if(move_uploaded_file($fichier['tmp_name'],$chemin_fichier)) {
195
 
193
 
196
			$infos_image_originale = $this->obtenirImageEtInfosPourChemin($chemin_fichier);
194
			$infos_image_originale = $this->obtenirImageEtInfosPourChemin($chemin_fichier);
197
			$taux_compression = $this->renvoyerTauxCompressionPourPoids($infos_image_originale['poids_octets']);
195
			$taux_compression = $this->renvoyerTauxCompressionPourPoids($infos_image_originale['poids_octets']);
198
			
196
			
199
			if($taux_compression < 100) {
197
			if($taux_compression < 100) {
200
				$this->recompresserImageSurDisqueEnPreservantMeta($chemin_fichier, $taux_compression);
198
				$this->recompresserImageSurDisqueEnPreservantMeta($chemin_fichier, $taux_compression);
201
			}
199
			}
202
				
200
				
203
			$infos_image_originale_stockee = $this->obtenirImageEtInfosPourChemin($chemin_fichier);
201
			$infos_image_originale_stockee = $this->obtenirImageEtInfosPourChemin($chemin_fichier);
204
			
202
			
205
			$formats = $this->getFormats();
203
			$formats = $this->getFormats();
206
			
204
			
207
			// creation de miniatures pour chacuns des formats définis
205
			// creation de miniatures pour chacuns des formats définis
208
			foreach($formats as $format) {
206
			foreach($formats as $format) {
209
				$this->creerEtStockerMiniatureFichierImageSelonFormat($id, $infos_image_originale_stockee, $format);
207
				$this->creerEtStockerMiniatureFichierImageSelonFormat($id, $infos_image_originale_stockee, $format);
210
			}
208
			}
211
	
209
	
212
	  		return true ;
210
	  		return true ;
213
	  		
211
	  		
214
		} else {
212
		} else {
215
			$erreur =  'ERROR : probleme durant le déplacement du fichier temporaire \n' ;
213
			$erreur =  'ERROR : probleme durant le déplacement du fichier temporaire \n' ;
216
			$this->logger('CEL_bugs',$erreur);
214
			$this->logger('CEL_bugs',$erreur);
217
  			return false ;
215
  			return false ;
218
		}
216
		}
219
	}
217
	}
220
	
218
	
221
	public function creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, $format) {
219
	public function creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, $format) {
222
		
220
		
223
		$chemin_sur_serveur_final = $this->obtenirDossierPourFormat($id,$format);
221
		$chemin_sur_serveur_final = $this->obtenirDossierPourFormat($id,$format);
224
		
222
		
225
		if(!file_exists($chemin_sur_serveur_final))
223
		if(!file_exists($chemin_sur_serveur_final))
226
		{
224
		{
227
			if(mkdir($chemin_sur_serveur_final,$this->droits, true)) {
225
			if(mkdir($chemin_sur_serveur_final,$this->droits, true)) {
228
				chmod($chemin_sur_serveur_final,$this->droits);
226
				chmod($chemin_sur_serveur_final,$this->droits);
229
			}
227
			}
230
			else
228
			else
231
			{
229
			{
232
				$erreur =  'ERROR : probleme durant l\'écriture du dossier '.$format.' \n' ;
230
				$erreur =  'ERROR : probleme durant l\'écriture du dossier '.$format.' \n' ;
233
				echo $erreur;
231
				echo $erreur;
234
				$this->logger('CEL_bugs',$erreur);
232
				$this->logger('CEL_bugs',$erreur);
235
				return false;
233
				return false;
236
			}
234
			}
237
		}
235
		}
238
		
236
		
239
		return $chemin_sur_serveur_final;
237
		return $chemin_sur_serveur_final;
240
	}
238
	}
241
	
239
	
242
	public function obtenirDossierPourFormat($id, $format) {
240
	public function obtenirDossierPourFormat($id, $format) {
243
		
241
		
244
		$chemin_base = $this->config['cel_db']['chemin_images'];
242
		$chemin_base = $this->config['cel_db']['chemin_images'];
245
	
243
	
246
		$chemin_sur_serveur = $chemin_base ;
244
		$chemin_sur_serveur = $chemin_base ;
247
	
245
	
248
		$id = sprintf('%09s', $id) ;
246
		$id = sprintf('%09s', $id) ;
249
		$id = wordwrap($id, 3 , '_', true) ;
247
		$id = wordwrap($id, 3 , '_', true) ;
250
	
248
	
251
		$niveauDossier = split("_", $id) ;
249
		$niveauDossier = split("_", $id) ;
252
	
250
	
253
		$dossierNiveau1 = $niveauDossier[0] ;
251
		$dossierNiveau1 = $niveauDossier[0] ;
254
		$dossierNiveau2 = $niveauDossier[1] ;
252
		$dossierNiveau2 = $niveauDossier[1] ;
255
		
253
		
256
		$chemin_sur_serveur_final = $chemin_sur_serveur.'/'.$dossierNiveau1.'/'.$dossierNiveau2.'/'.$format;
254
		$chemin_sur_serveur_final = $chemin_sur_serveur.'/'.$dossierNiveau1.'/'.$dossierNiveau2.'/'.$format;
257
		
255
		
258
		return $chemin_sur_serveur_final;
256
		return $chemin_sur_serveur_final;
259
	}
257
	}
260
	
258
	
261
	public function obtenirImageEtInfosPourChemin($chemin_fichier) {
259
	public function obtenirImageEtInfosPourChemin($chemin_fichier) {
262
		
260
		
263
		$image_et_infos = array();
261
		$image_et_infos = array();
264
		
262
		
265
		list($image_et_infos['largeur'], $image_et_infos['hauteur']) = getimagesize($chemin_fichier);		
263
		list($image_et_infos['largeur'], $image_et_infos['hauteur']) = getimagesize($chemin_fichier);		
266
		$image_et_infos['poids_octets'] = filesize($chemin_fichier);
264
		$image_et_infos['poids_octets'] = filesize($chemin_fichier);
267
		$image_et_infos['image'] = imagecreatefromjpeg($chemin_fichier);
265
		$image_et_infos['image'] = imagecreatefromjpeg($chemin_fichier);
268
		
266
		
269
		return $image_et_infos;
267
		return $image_et_infos;
270
	}
268
	}
271
	
269
	
272
	public function obtenirDimensionsPourFormat($format) {
270
	public function obtenirDimensionsPourFormat($format) {
273
		
271
		
274
		$dimensions = array('largeur' => 0, 'hauteur' => 0);
272
		$dimensions = array('largeur' => 0, 'hauteur' => 0);
275
			
273
			
276
		if(isset($this->config['cel_db']['format_'.$format])) {
274
		if(isset($this->config['cel_db']['format_'.$format])) {
277
			
275
			
278
			$format_largeur_hauteur = split('_', $this->config['cel_db']['format_'.$format]);
276
			$format_largeur_hauteur = split('_', $this->config['cel_db']['format_'.$format]);
279
			
277
			
280
			$dimensions['largeur'] = $format_largeur_hauteur[0];
278
			$dimensions['largeur'] = $format_largeur_hauteur[0];
281
			$dimensions['hauteur'] = $format_largeur_hauteur[1];
279
			$dimensions['hauteur'] = $format_largeur_hauteur[1];
282
		}
280
		}
283
		
281
		
284
		return $dimensions;
282
		return $dimensions;
285
		
283
		
286
	}
284
	}
287
	
285
	
288
	public function calculerTailleImage($informations_images, $taille_max) {
286
	public function calculerTailleImage($informations_images, $taille_max) {
289
	
287
	
290
	        $HL_redimension = array();
288
	        $HL_redimension = array();
291
	        
289
	        
292
	        if($this->estPaysage($informations_images)) {
290
	        if($this->estPaysage($informations_images)) {
293
	        
291
	        
294
		        $rapport = $informations_images['hauteur']/$informations_images['largeur'] ;
292
		        $rapport = $informations_images['hauteur']/$informations_images['largeur'] ;
295
		        $HL_redimension['largeur'] = round($taille_max) ;
293
		        $HL_redimension['largeur'] = round($taille_max) ;
296
		        $HL_redimension['hauteur'] = round($taille_max*$rapport) ;
294
		        $HL_redimension['hauteur'] = round($taille_max*$rapport) ;
297
		        
295
		        
298
	        } else {
296
	        } else {
299
	        	$rapport = $informations_images['largeur']/$informations_images['hauteur'] ;
297
	        	$rapport = $informations_images['largeur']/$informations_images['hauteur'] ;
300
		        $HL_redimension['hauteur'] = round($taille_max) ;
298
		        $HL_redimension['hauteur'] = round($taille_max) ;
301
		        $HL_redimension['largeur'] = round($taille_max*$rapport) ;
299
		        $HL_redimension['largeur'] = round($taille_max*$rapport) ;
302
	        }
300
	        }
303
	        
301
	        
304
	        return $HL_redimension;
302
	        return $HL_redimension;
305
	}
303
	}
306
	
304
	
307
	public function getFormats() {
305
	public function getFormats() {
308
		return $this->formats;
306
		return $this->formats;
309
	}
307
	}
310
	
308
	
311
	public function estUnFormatCarre($format) {
309
	public function estUnFormatCarre($format) {
312
		
310
		
313
		return (strpos($format,'C') === 0);		
311
		return (strpos($format,'C') === 0);		
314
	}
312
	}
315
	
313
	
316
	public function estUnFormatRogne($format) {
314
	public function estUnFormatRogne($format) {
317
 
315
 
318
		return (strpos($format,'R') === 1);		
316
		return (strpos($format,'R') === 1);		
319
	}
317
	}
320
	
318
	
321
	public function estPaysage($informations_images) {
319
	public function estPaysage($informations_images) {
322
		return $informations_images['largeur'] > $informations_images['hauteur'];
320
		return $informations_images['largeur'] > $informations_images['hauteur'];
323
	}
321
	}
324
	
322
	
325
	public function estPortait($informations_images) {
323
	public function estPortait($informations_images) {
326
		return $informations_images['largeur'] < $informations_images['hauteur'];
324
		return $informations_images['largeur'] < $informations_images['hauteur'];
327
	}
325
	}
328
	
326
	
329
	public function renvoyerTauxCompressionPourPoids($poids_octets) {
327
	public function renvoyerTauxCompressionPourPoids($poids_octets) {
330
 
328
 
331
		$poids_max_octets = $this->config['cel_db']['taille_max'];
329
		$poids_max_octets = $this->config['cel_db']['taille_max'];
332
		
330
		
333
		$ratio_compression = 100 ;
331
		$ratio_compression = 100 ;
334
		
332
		
335
	    if($poids_octets >= $poids_max_octets) {
333
	    if($poids_octets >= $poids_max_octets) {
336
	      $ratio_compression = 75 ;
334
	      $ratio_compression = 75 ;
337
	    }
335
	    }
338
	    
336
	    
339
	    return $ratio_compression;
337
	    return $ratio_compression;
340
	}
338
	}
341
	
339
	
342
	public function convertirIdBddVersNomFichier($id, $format, $extension = 'jpg') {
340
	public function convertirIdBddVersNomFichier($id, $format, $extension = 'jpg') {
343
		
341
		
344
			// creation du format original
342
			// creation du format original
345
		$id_avec_zeros = sprintf('%09s', $id) ;
343
		$id_avec_zeros = sprintf('%09s', $id) ;
346
		$id_avec_zeros_underscores = wordwrap($id_avec_zeros, 3 , '_', true) ;
344
		$id_avec_zeros_underscores = wordwrap($id_avec_zeros, 3 , '_', true) ;
347
		
345
		
348
		$nom_fichier = $id_avec_zeros_underscores.'_'.$format.'.'.$extension;
346
		$nom_fichier = $id_avec_zeros_underscores.'_'.$format.'.'.$extension;
349
		
347
		
350
		return $nom_fichier;
348
		return $nom_fichier;
351
	}
349
	}
352
	
350
	
353
	public function convertirBaseNomFichierVersIdBdd($nom_fichier, $formats) {
351
	public function convertirBaseNomFichierVersIdBdd($nom_fichier, $formats) {
354
		
352
		
355
		$nom_fichier_sans_extension = trim($nom_fichier, '.jpg');
353
		$nom_fichier_sans_extension = trim($nom_fichier, '.jpg');
356
		
354
		
357
		foreach($formats as $format) {
355
		foreach($formats as $format) {
358
			$nom_fichier_sans_extension = trim($nom_fichier_sans_extension, '_'.$format);
356
			$nom_fichier_sans_extension = trim($nom_fichier_sans_extension, '_'.$format);
359
		}
357
		}
360
		
358
		
361
		$id_image = str_replace('_', '', $nom_fichier_sans_extension);
359
		$id_image = str_replace('_', '', $nom_fichier_sans_extension);
362
			
360
			
363
		// suppression des 0 devant
361
		// suppression des 0 devant
364
		$id_image += 0;
362
		$id_image += 0;
365
		
363
		
366
		return $id_image;
364
		return $id_image;
367
	}
365
	}
368
	
366
	
369
	public function ecrireImageSurDisque($image, $id, $format, $compression = 100) {
367
	public function ecrireImageSurDisque($image, $id, $format, $compression = 100) {
370
		
368
		
371
		umask(0);
369
		umask(0);
372
		
370
		
373
		$chemin_sur_serveur_final = $this->creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, $format);
371
		$chemin_sur_serveur_final = $this->creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, $format);
374
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, $format);
372
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, $format);
375
		
373
		
376
		if(file_exists($chemin_sur_serveur_final.'/'.$nom_fichier)) {
374
		if(file_exists($chemin_sur_serveur_final.'/'.$nom_fichier)) {
377
			unlink($chemin_sur_serveur_final.'/'.$nom_fichier);
375
			unlink($chemin_sur_serveur_final.'/'.$nom_fichier);
378
		}
376
		}
379
		
377
		
380
		// attention ceci ne preserve pas les metadonnées
378
		// attention ceci ne preserve pas les metadonnées
381
		imagejpeg($image,$chemin_sur_serveur_final.'/'.$nom_fichier, $compression);
379
		imagejpeg($image,$chemin_sur_serveur_final.'/'.$nom_fichier, $compression);
382
		chmod($chemin_sur_serveur_final.'/'.$nom_fichier,$this->droits);
380
		chmod($chemin_sur_serveur_final.'/'.$nom_fichier,$this->droits);
383
	}
381
	}
384
	
382
	
385
	
383
	
386
	public function recompresserImageSurDisqueEnPreservantMeta($chemin_fichier, $compression = 100) {
384
	public function recompresserImageSurDisqueEnPreservantMeta($chemin_fichier, $compression = 100) {
387
 
385
 
388
		$blob = file_get_contents($chemin_fichier);
386
		/*$blob = file_get_contents($chemin_fichier);
389
			
387
			
390
		$image_a_compresser = new Imagick();
388
		$image_a_compresser = new Imagick();
391
		$image_a_compresser->readImageBlob($blob, $chemin_fichier);
389
		$image_a_compresser->readImageBlob($blob, $chemin_fichier);
392
		$image_a_compresser->setformat("jpeg");
390
		$image_a_compresser->setformat("jpeg");
393
		$image_a_compresser->setImageCompression(imagick::COMPRESSION_JPEG); 
391
		$image_a_compresser->setImageCompression(imagick::COMPRESSION_JPEG); 
394
		$image_a_compresser->setCompressionQuality($compression);
392
		$image_a_compresser->setCompressionQuality($compression);
395
		$image_a_compresser->writeImage($chemin_fichier);
393
		$image_a_compresser->writeImage($chemin_fichier);
396
		$image_a_compresser->destroy();	
394
		$image_a_compresser->destroy();	
397
		
395
		
398
		chmod($chemin_sur_serveur_final.'/'.$nom_fichier,$this->droits);
396
		chmod($chemin_sur_serveur_final.'/'.$nom_fichier,$this->droits);*/
399
	}
397
	}
400
	
398
	
401
	public function renvoyerEtCreerImageCarreeBlancheSelonFormat($cote) {
399
	public function renvoyerEtCreerImageCarreeBlancheSelonFormat($cote) {
402
		
400
		
403
		$image_blanche = imagecreatetruecolor($cote, $cote);		
401
		$image_blanche = imagecreatetruecolor($cote, $cote);		
404
		$blanc = imagecolorallocate($image_blanche, 255, 255, 255);
402
		$blanc = imagecolorallocate($image_blanche, 255, 255, 255);
405
		imagefilledrectangle($image_blanche, 0, 0, $cote, $cote, $blanc);
403
		imagefilledrectangle($image_blanche, 0, 0, $cote, $cote, $blanc);
406
		
404
		
407
		return $image_blanche;
405
		return $image_blanche;
408
	}
406
	}
409
	
407
	
410
	public function detruireImageEnMemoire($image) {		
408
	public function detruireImageEnMemoire($image) {		
411
		imagedestroy($image);
409
		imagedestroy($image);
412
	}
410
	}
413
	
411
	
414
	public function detruireImageSurDisque($id) {
412
	public function detruireImageSurDisque($id) {
415
		$formats = $this->getFormats();
413
		$formats = $this->getFormats();
416
		
414
		
417
		// on detruit aussi l'image originale
415
		// on detruit aussi l'image originale
418
		$formats[] = 'O';
416
		$formats[] = 'O';
419
		
417
		
420
		// destructions de chacuns des formats définis
418
		// destructions de chacuns des formats définis
421
		foreach($formats as $format) {
419
		foreach($formats as $format) {
422
			$dossier_format = $this->obtenirDossierPourFormat($id, $format);
420
			$dossier_format = $this->obtenirDossierPourFormat($id, $format);
423
			$nom_fichier = $this->convertirIdBddVersNomFichier($id, $format);
421
			$nom_fichier = $this->convertirIdBddVersNomFichier($id, $format);
424
			unlink($dossier_format.'/'.$nom_fichier);
422
			unlink($dossier_format.'/'.$nom_fichier);
425
		}
423
		}
426
	}
424
	}
427
}
425
}
428
?>
426
?>