Subversion Repositories eFlore/Applications.cel

Rev

Rev 605 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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