Subversion Repositories eFlore/Applications.cel

Rev

Rev 591 | Rev 621 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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