Subversion Repositories eFlore/Applications.cel

Rev

Rev 621 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 621 Rev 632
Line 1... Line 1...
1
<?php
1
<?php
2
Class ImageRecreation extends DBAccessor {
2
Class ImageRecreation {
Line 3... Line 3...
3
	
3
	
4
	private $droits = 0755;
4
	private $droits = 0755;
-
 
5
	private $formats = array('CRX2S','CXS','CS','CRS','XS','S','M','L','XL','X2L','X3L');
-
 
6
	const MODE_GD = 'gd';
-
 
7
	const MODE_IMAGEMAGICK = 'imagemagick';
-
 
8
	private $mode;
-
 
9
	
Line 5... Line 10...
5
	private $formats = array('CRX2S','CXS','CS','S','M','L','XL','X2L','X3L');
10
	private $verbose = true;
Line 6... Line 11...
6
 
11
 
-
 
12
	public function ImageRecreation($config) {
-
 
13
 
-
 
14
		$this->config=$config;
-
 
15
		$this->mode = self::MODE_GD;
-
 
16
		
-
 
17
		if (extension_loaded('imagick')) {
7
	public function ImageRecreation($config) {
18
			putenv("MAGICK_TEMPORARY_PATH=/home/aurelien/web/file_tmp");
Line -... Line 19...
-
 
19
			$this->mode = self::MODE_IMAGEMAGICK;
-
 
20
		}
-
 
21
	}
-
 
22
	
8
 
23
	public function recreerMiniaturesRecursivement() {
-
 
24
		$this->itererRecursivement($this->config['cel_db']['chemin_images']);
-
 
25
	}
-
 
26
	
Line 9... Line 27...
9
		$this->config=$config;
27
	public function testOptiCrop() {
Line 10... Line 28...
10
	}
28
		
Line -... Line 29...
-
 
29
		$image = $this->obtenirImageEtInfosPourChemin('/home/aurelien/web/file_tmp/test.jpg');
-
 
30
		return $this->opticrop($image,'CRXS','');
-
 
31
		
-
 
32
	}
-
 
33
	
-
 
34
	public function regenererMiniaturesIntervalle($params) {
-
 
35
		
-
 
36
		$id_debut = $params[0];
-
 
37
		$id_fin = $params[1];
-
 
38
 
-
 
39
		if (is_numeric($id_debut) && is_numeric($id_fin)) {
-
 
40
			
-
 
41
			
-
 
42
			//echo("Début du traitement des images de ".$id_debut." à ".$id_fin."\n");
-
 
43
			
-
 
44
			for ($i = $id_debut; $i <= $id_fin; $i++) {
11
	
45
					
Line 12... Line 46...
12
	public function getRessource() {
46
				echo($i."\n");
-
 
47
				
-
 
48
			    $tab_param = array($i);
-
 
49
			    $this->regenererMiniaturesPourId($tab_param);
-
 
50
			}
-
 
51
		}
-
 
52
	}
-
 
53
	
13
		
54
	public function regenererMiniaturesPourId($params) {
-
 
55
 
-
 
56
		$id = $params[0];
-
 
57
 
-
 
58
		if (!is_numeric($id)) {
-
 
59
		      return;
-
 
60
		}
-
 
61
 
-
 
62
		$dossier_fichier = $this->obtenirDossierPourFormat($id, 'O');
-
 
63
 
-
 
64
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, 'O');
-
 
65
		
-
 
66
		$chemin_fichier = $dossier_fichier.'/'.$nom_fichier;
-
 
67
 
-
 
68
		if(file_exists($chemin_fichier)) {
-
 
69
			$infos_image_originale = $this->obtenirImageEtInfosPourChemin($chemin_fichier);
-
 
70
							
-
 
71
			// creation de miniatures pour chacuns des formats définis
-
 
72
			foreach($this->formats as $format) {
-
 
73
				$this->creerEtStockerMiniatureFichierImageSelonFormat($id, $infos_image_originale, $format);
14
	}
74
			}
Line 15... Line 75...
15
	
75
					      
Line 16... Line 76...
16
	public function getElement($param) {
76
	      	//if($this->verbose) {
17
		
77
	      		//echo("traitement de l'image ".$infos_image_originale."\n");
Line 18... Line 78...
18
	}
78
	      	//}
Line 19... Line 79...
19
	
79
		} else {
20
	public function recreerMiniaturesRecursivement() {
80
			//echo 'le fichier '.$chemin_fichier.'n\'existe pas '."\n";
21
		$this->itererRecursivement($this->config['cel_db']['chemin_images']);
81
		}
Line 22... Line 82...
22
	}
82
	}
23
	
83
	
24
	public function itererRecursivement($dossier) {
84
	public function itererRecursivement($dossier) {
Line 25... Line 85...
25
		
85
		
Line 26... Line 86...
26
		// on ne parse que le dossier des images originales
86
		// on ne parse que le dossier des images originales
27
		$dossiers_a_exclure = $this->getFormats();
87
		$dossiers_a_exclure = $this->getFormats();
Line 28... Line 88...
28
				
88
				
Line 57... Line 117...
57
		}
117
		}
58
	}
118
	}
Line 59... Line 119...
59
			
119
			
Line 60... Line 120...
60
	public function creerEtStockerMiniatureFichierImageSelonFormat($id ,$infos_image_originale, $format = 'O') {
120
	public function creerEtStockerMiniatureFichierImageSelonFormat($id ,$infos_image_originale, $format = 'O') {
61
		
121
		
62
		if($format == 'O') {		
122
		if ($format == 'O') {		
Line 63... Line 123...
63
			// format original : rien à faire
123
			// format original : rien à faire
64
			$image_redimensionnee = $infos_image_originale['image'];
124
			$image_redimensionnee = $infos_image_originale['image'];
-
 
125
			
-
 
126
		} else {
-
 
127
			 if ($this->estUnFormatRogne($format)) {
-
 
128
			 	
-
 
129
			 	if ($this->mode == self::MODE_IMAGEMAGICK) {
-
 
130
			 		// si l'on dispose de la librairie imageMagick
-
 
131
			 		// on applique l'algorithme d'auto détection de sujets
-
 
132
			 		// qui centre la miniature sur le sujet de l'image
65
			
133
			 		$image_redimensionnee = $this->opticrop($infos_image_originale, $format);
66
		} else {
134
			 	} else {
-
 
135
			 		// si l'on ne dispose que de gd
67
			 if($this->estUnFormatRogne($format)) {
136
					// la minature est une image redimensionnée rognée au centre
68
				// la minature est une image redimensionnée rognée au centre
137
					$image_redimensionnee = $this->creerMiniatureCarreeRognee($infos_image_originale, $format); 
69
				$image_redimensionnee = $this->creerMiniatureCarreeRognee($infos_image_originale, $format); 														
138
			 	}	 	
70
			} else if($this->estUnFormatCarre($format)) {
139
			} 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
140
				// 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);				
141
				$image_redimensionnee = $this->creerMiniatureCarree($infos_image_originale, $format);				
Line 114... Line 183...
114
		$cote_carre = $taille_reference_pour_format['largeur'];
183
		$cote_carre = $taille_reference_pour_format['largeur'];
Line 115... Line 184...
115
		
184
		
116
		$image_redimensionnee_avec_rapport = $this->creerMiniature($informations_image, $format);
185
		$image_redimensionnee_avec_rapport = $this->creerMiniature($informations_image, $format);
Line 117... Line 186...
117
		$taille_redimensionnee_avec_rapport = $this->calculerTailleImage($informations_image, $taille_reference_pour_format['hauteur']);
186
		$taille_redimensionnee_avec_rapport = $this->calculerTailleImage($informations_image, $taille_reference_pour_format['hauteur']);
118
		
187
		
119
		if($this->estPaysage($informations_image)) {				
188
		if ($this->estPaysage($informations_image)) {				
120
				$debut_largeur_a_copier = 0 ;
189
				$debut_largeur_a_copier = 0 ;
121
				$debut_hauteur_a_copier = ($cote_carre - $taille_redimensionnee_avec_rapport['hauteur'])/2 ;	
190
				$debut_hauteur_a_copier = ($cote_carre - $taille_redimensionnee_avec_rapport['hauteur'])/2 ;	
122
		} else {
191
		} else {
Line 138... Line 207...
138
		
207
		
139
		$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
208
		$taille_reference_pour_format = $this->obtenirDimensionsPourFormat($format);
140
		$cote_carre = $taille_reference_pour_format['largeur'];
209
		$cote_carre = $taille_reference_pour_format['largeur'];
Line 141... Line 210...
141
		$cote_carre_non_redimensionne = 0;
210
		$cote_carre_non_redimensionne = 0;
142
		
211
		
143
		if($this->estPaysage($informations_image)) {	
212
		if ($this->estPaysage($informations_image)) {	
144
				$cote_carre_non_redimensionne = $informations_image['hauteur'];		
213
				$cote_carre_non_redimensionne = $informations_image['hauteur'];		
145
				$debut_largeur_a_copier = ($informations_image['hauteur'] - $informations_image['hauteur'])/2 ;
214
				$debut_largeur_a_copier = ($informations_image['hauteur'] - $informations_image['hauteur'])/2 ;
146
				$debut_hauteur_a_copier = 0;
215
				$debut_hauteur_a_copier = 0;
Line 180... Line 249...
180
		);
249
		);
Line 181... Line 250...
181
 
250
 
182
		return $image_redimensionnee;
251
		return $image_redimensionnee;
Line 183... Line -...
183
	}
-
 
184
	
-
 
185
	
-
 
186
	/*public function extractionSelonDate() {
-
 
187
 
-
 
188
		$DB=$this->connectDB($this->config,'cel_db');
-
 
189
		
-
 
190
		$requete_selection_diff = 'SELECT * FROM cel_images WHERE ci_meta_date_ajout >= "2011-02-23"';
-
 
191
		
-
 
192
		$diff_image =& $DB->query($query_ordre);
-
 
193
		
-
 
194
		if (PEAR::isError($diff_imag))
-
 
195
		{
-
 
196
			$erreur = 'ERROR' ;
-
 
197
	        die($diff_image->getMessage());
-
 
198
	    }
-
 
199
		
-
 
200
		while ($row =& $diff_image->fetchrow(DB_FETCHMODE_ASSOC)) {
-
 
201
			
-
 
202
			$id = $row['ci_id_image'];
-
 
203
			
-
 
204
			$dossier = $this->obtenirDossierPourFormat($id,'L');
-
 
205
			$nom_fichier = $this->convertirIdBddVersNomFichier($id, 'L');
-
 
206
			
-
 
207
			$chemin_complet = $dossier.'/'.$nom_fichier;
-
 
208
			
-
 
209
			
-
 
210
			
-
 
211
			$chemin_stockage_fichier = '/home/telabotap/www/sites/eflore/projets/cel_sauvegarde/'.str_replace(' ','_',$row['ci_nom_original']);
-
 
212
			copy($chemin_complet,$chemin_stockage_fichier);
-
 
213
			
-
 
214
		}
-
 
215
	}*/
252
	}
Line 216... Line 253...
216
	
253
	
217
	public function stockerFichierEtCreerMiniatures($fichier, $id) {
254
	public function stockerFichierEtCreerMiniatures($fichier, $id) {
Line 218... Line 255...
218
		
255
		
Line 219... Line 256...
219
		$chemin_base_fichier = $this->creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, 'O');	
256
		$chemin_base_fichier = $this->creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, 'O');	
-
 
257
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, 'O');
-
 
258
	
Line 220... Line 259...
220
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, 'O');
259
		$chemin_fichier = $chemin_base_fichier.'/'.$nom_fichier;
221
	
260
		
Line 222... Line 261...
222
		$chemin_fichier = $chemin_base_fichier.'/'.$nom_fichier;
261
		$deplacement_fichier = $this->stockerImageExterne($fichier['tmp_name'], $chemin_fichier);
223
		
262
		
224
		if(rename($fichier['tmp_name'],$chemin_fichier)) {
263
		if ($deplacement_fichier) {
Line 225... Line 264...
225
 
264
 
Line 226... Line 265...
226
			$infos_image_originale = $this->obtenirImageEtInfosPourChemin($chemin_fichier);
265
			$infos_image_originale = $this->obtenirImageEtInfosPourChemin($chemin_fichier);
Line 246... Line 285...
246
			$this->logger('CEL_bugs',$erreur);
285
			$this->logger('CEL_bugs',$erreur);
247
  			return false ;
286
  			return false ;
248
		}
287
		}
249
	}
288
	}
Line -... Line 289...
-
 
289
	
-
 
290
	public function stockerImageExterne($chemin_fichier_temp, $chemin_destination) {
-
 
291
		
-
 
292
		if(is_uploaded_file($chemin_fichier_temp)) {
-
 
293
			$deplacement = move_uploaded_file($chemin_fichier_temp, $chemin_destination);
-
 
294
		} else {
-
 
295
			$deplacement = rename($chemin_fichier_temp, $chemin_destination);
-
 
296
		}
-
 
297
		
-
 
298
		return $deplacement;
-
 
299
	}
250
	
300
	
Line 251... Line 301...
251
	public function creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, $format) {
301
	public function creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, $format) {
Line 252... Line 302...
252
		
302
		
253
		$chemin_sur_serveur_final = $this->obtenirDossierPourFormat($id,$format);
303
		$chemin_sur_serveur_final = $this->obtenirDossierPourFormat($id,$format);
254
		
304
		
255
		if(!file_exists($chemin_sur_serveur_final))
305
		if (!file_exists($chemin_sur_serveur_final))
256
		{
306
		{
257
			if(mkdir($chemin_sur_serveur_final,$this->droits, true)) {
307
			if (mkdir($chemin_sur_serveur_final,$this->droits, true)) {
258
				chmod($chemin_sur_serveur_final,$this->droits);
308
				chmod($chemin_sur_serveur_final,$this->droits);
259
			}
309
			}
Line 293... Line 343...
293
		$image_et_infos = array();
343
		$image_et_infos = array();
Line 294... Line 344...
294
		
344
		
295
		list($image_et_infos['largeur'], $image_et_infos['hauteur']) = getimagesize($chemin_fichier);		
345
		list($image_et_infos['largeur'], $image_et_infos['hauteur']) = getimagesize($chemin_fichier);		
296
		$image_et_infos['poids_octets'] = filesize($chemin_fichier);
346
		$image_et_infos['poids_octets'] = filesize($chemin_fichier);
-
 
347
		$image_et_infos['image'] = imagecreatefromjpeg($chemin_fichier);
Line 297... Line 348...
297
		$image_et_infos['image'] = imagecreatefromjpeg($chemin_fichier);
348
		$image_et_infos['chemin'] = $chemin_fichier;
298
		
349
		
Line 299... Line 350...
299
		return $image_et_infos;
350
		return $image_et_infos;
Line 300... Line 351...
300
	}
351
	}
Line 301... Line 352...
301
	
352
	
Line 302... Line 353...
302
	public function obtenirDimensionsPourFormat($format) {
353
	public function obtenirDimensionsPourFormat($format) {
Line 303... Line 354...
303
		
354
		
304
		$dimensions = array('largeur' => 0, 'hauteur' => 0);
355
		$dimensions = array('largeur' => 0, 'hauteur' => 0);
Line 317... Line 368...
317
	
368
	
Line 318... Line 369...
318
	public function calculerTailleImage($informations_images, $taille_max) {
369
	public function calculerTailleImage($informations_images, $taille_max) {
Line 319... Line 370...
319
	
370
	
Line 320... Line 371...
320
	        $HL_redimension = array();
371
	        $HL_redimension = array();
321
	        
372
	        
322
	        if($this->estPaysage($informations_images)) {
373
	        if ($this->estPaysage($informations_images)) {
Line 360... Line 411...
360
 
411
 
Line 361... Line 412...
361
		$poids_max_octets = $this->config['cel_db']['taille_max'];
412
		$poids_max_octets = $this->config['cel_db']['taille_max'];
Line 362... Line 413...
362
		
413
		
363
		$ratio_compression = 100 ;
414
		$ratio_compression = 100 ;
364
		
415
		
Line 365... Line 416...
365
	    if($poids_octets >= $poids_max_octets) {
416
	    if ($poids_octets >= $poids_max_octets) {
366
	      $ratio_compression = 75 ;
417
	      $ratio_compression = 75 ;
Line 394... Line 445...
394
		$id_image += 0;
445
		$id_image += 0;
Line 395... Line 446...
395
		
446
		
396
		return $id_image;
447
		return $id_image;
Line 397... Line 448...
397
	}
448
	}
Line 398... Line 449...
398
	
449
	
Line 399... Line 450...
399
	public function ecrireImageSurDisque($image, $id, $format, $compression = 100) {
450
	public function ecrireImageSurDisque($image_binaire, $id, $format, $compression = 100) {
400
		
451
		
Line 401... Line 452...
401
		umask(0);
452
		umask(0);
402
		
453
		
403
		$chemin_sur_serveur_final = $this->creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, $format);
454
		$chemin_sur_serveur_final = $this->creerSiNecessaireEtRenvoyerCheminStockageFichierPourIdEtFormat($id, $format);
Line 404... Line 455...
404
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, $format);
455
		$nom_fichier = $this->convertirIdBddVersNomFichier($id, $format);
405
		
456
		
406
		if(file_exists($chemin_sur_serveur_final.'/'.$nom_fichier)) {
457
		if (file_exists($chemin_sur_serveur_final.'/'.$nom_fichier)) {
407
			unlink($chemin_sur_serveur_final.'/'.$nom_fichier);
458
			unlink($chemin_sur_serveur_final.'/'.$nom_fichier);
408
		}
-
 
409
		
459
		}
410
		// attention ceci ne preserve pas les metadonnées
460
		
Line -... Line 461...
-
 
461
		// attention ceci ne preserve pas les metadonnées
411
		imagejpeg($image,$chemin_sur_serveur_final.'/'.$nom_fichier, $compression);
462
		imagejpeg($image_binaire, $chemin_sur_serveur_final.'/'.$nom_fichier, $compression);
412
		chmod($chemin_sur_serveur_final.'/'.$nom_fichier,$this->droits);
463
		chmod($chemin_sur_serveur_final.'/'.$nom_fichier,$this->droits);
413
	}
-
 
414
	
464
	}
415
	
465
		
416
	public function recompresserImageSurDisqueEnPreservantMeta($chemin_fichier, $compression = 100) {
466
	public function ecrireImageSurDisqueAvecMeta($image_a_stocker, $compression = 100) {
417
 
467
 
418
		/*$blob = file_get_contents($chemin_fichier);
468
		$image_a_stocker = new Imagick();
419
			
469
		$image_a_stocker->readImageBlob($image_a_stocker);
Line 420... Line 470...
420
		$image_a_compresser = new Imagick();
470
	
421
		$image_a_compresser->readImageBlob($blob, $chemin_fichier);
471
		// l'utilisation d'image magick préserve les métadonnées lors d'une recompression
Line 422... Line 472...
422
		$image_a_compresser->setformat("jpeg");
472
		$image_a_stocker->setformat("jpeg");
Line 423... Line 473...
423
		$image_a_compresser->setImageCompression(imagick::COMPRESSION_JPEG); 
473
		$image_a_stocker->setImageCompression(imagick::COMPRESSION_JPEG); 
Line 454... Line 504...
454
		foreach($formats as $format) {
504
		foreach($formats as $format) {
Line 455... Line 505...
455
			
505
			
456
			$dossier_format = $this->obtenirDossierPourFormat($id, $format);
506
			$dossier_format = $this->obtenirDossierPourFormat($id, $format);
Line 457... Line 507...
457
			$nom_fichier = $this->convertirIdBddVersNomFichier($id, $format);
507
			$nom_fichier = $this->convertirIdBddVersNomFichier($id, $format);
458
			
508
			
459
			if(file_exists($dossier_format.'/'.$nom_fichier)) {
509
			if (file_exists($dossier_format.'/'.$nom_fichier)) {
460
				$destruction_formats_fichier = unlink($dossier_format.'/'.$nom_fichier);
510
				$destruction_formats_fichier = unlink($dossier_format.'/'.$nom_fichier);
461
			} else {
511
			} else {
462
				$destruction_formats_fichier = true;
512
				$destruction_formats_fichier = true;
Line 463... Line 513...
463
			}
513
			}
464
		}
514
		}
-
 
515
		
-
 
516
		return $destruction_formats_fichier;
-
 
517
	}
-
 
518
	
-
 
519
	/* 
-
 
520
	 * edge-maximizing crop
-
 
521
	 * determines center-of-edginess, then tries different-sized crops around it. 
-
 
522
	 * picks the crop with the highest normalized edginess.
-
 
523
	 * see documentation on how to tune the algorithm
-
 
524
	 *
-
 
525
	 * $informations_image - le tableau d'informations sur l'image tel que renvoyé par la fonction obtenirImageEtInfosPourChemin
-
 
526
	 * $format - le format (ex. : CS, XS, XL, CRS)
-
 
527
	*/
-
 
528
	function opticrop($informations_image, $format) {
-
 
529
 
-
 
530
		umask(0);
-
 
531
		
-
 
532
		$nom_temp = md5(time());
-
 
533
		$out = '/home/aurelien/web/file_tmp/'.$nom_temp;
-
 
534
		
-
 
535
		$dimension_vignettes = $this->obtenirDimensionsPourFormat($format);
-
 
536
		
-
 
537
		$largeur_vignette = $dimension_vignettes['largeur'];
-
 
538
		$hauteur_vignette = $dimension_vignettes['hauteur'];
-
 
539
		
-
 
540
	    // source dimensions
-
 
541
	    $largeur_image_originale = $informations_image['largeur'];
-
 
542
	    $hauteur_image_originale = $informations_image['hauteur'];
-
 
543
	    
-
 
544
	    $chemin_image = $informations_image['chemin'];
-
 
545
	    
-
 
546
	    //if ($largeur_vignette > $largeur_image_originale || $hauteur_vignette > $hauteur_image_originale)
-
 
547
	    //    die("Target dimensions must be smaller or equal to source dimensions.");
-
 
548
	
-
 
549
	    // parameters for the edge-maximizing crop algorithm
-
 
550
	    $r = 1;         // radius of edge filter
-
 
551
	    $nk = 9;        // scale count: number of crop sizes to try
-
 
552
	    $gamma = 0.2;   // edge normalization parameter -- see documentation
-
 
553
	    $ar = $largeur_vignette/$hauteur_vignette;    // target aspect ratio (AR)
-
 
554
	    $ar0 = $largeur_image_originale/$hauteur_image_originale;    // original aspect ratio (AR)
-
 
555
	
-
 
556
	    //echo("$chemin_image: $largeur_image_originale x $hauteur_image_originale => $largeur_vignette x $hauteur_vignette");
-
 
557
	    $img = new Imagick($chemin_image);
-
 
558
	    $imgcp = clone $img;
-
 
559
	
-
 
560
	    // compute center of edginess
-
 
561
	    $img->edgeImage($r);
-
 
562
	    $img->modulateImage(100,0,100); // grayscale
-
 
563
	    $img->blackThresholdImage("#0f0f0f");
-
 
564
	    $img->writeImage($out);
-
 
565
	    // use gd for random pixel access
-
 
566
	    $im = ImageCreateFromJpeg($out);
-
 
567
	    $xcenter = 0;
-
 
568
	    $ycenter = 0;
-
 
569
	    $sum = 0;
-
 
570
	    $n = 100000;
-
 
571
	    for ($k=0; $k<$n; $k++) {
-
 
572
	        $i = mt_rand(0,$largeur_image_originale-1);
-
 
573
	        $j = mt_rand(0,$hauteur_image_originale-1);
-
 
574
	        $val = imagecolorat($im, $i, $j) & 0xFF;
-
 
575
	        $sum += $val;
-
 
576
	        $xcenter += ($i+1)*$val;
-
 
577
	        $ycenter += ($j+1)*$val;
-
 
578
	    }
-
 
579
	    $xcenter /= $sum;
-
 
580
	    $ycenter /= $sum;
-
 
581
	
-
 
582
	    // crop source img to target AR
-
 
583
	    if ($largeur_image_originale/$hauteur_image_originale > $ar) {
-
 
584
	        // source AR wider than target
-
 
585
	        // crop width to target AR
-
 
586
	        $wcrop0 = round($ar*$hauteur_image_originale);
-
 
587
	        $hcrop0 = $hauteur_image_originale;
-
 
588
	    } 
-
 
589
	    else {
-
 
590
	        // crop height to target AR
-
 
591
	        $wcrop0 = $largeur_image_originale;
-
 
592
	        $hcrop0 = round($largeur_image_originale/$ar);
-
 
593
	    }
-
 
594
	
-
 
595
	    // crop parameters for all scales and translations
-
 
596
	    $params = array();
-
 
597
	
-
 
598
	    // crop at different scales
-
 
599
	    $hgap = $hcrop0 - $hauteur_vignette;
-
 
600
	    $hinc = ($nk == 1) ? 0 : $hgap / ($nk - 1);
-
 
601
	    $wgap = $wcrop0 - $largeur_vignette;
-
 
602
	    $winc = ($nk == 1) ? 0 : $wgap / ($nk - 1);
-
 
603
	
-
 
604
	    // find window with highest normalized edginess
-
 
605
	    $n = 10000;
-
 
606
	    $maxbetanorm = 0;
-
 
607
	    $maxfile = '';
-
 
608
	    $maxparam = array('w'=>0, 'h'=>0, 'x'=>0, 'y'=>0);
-
 
609
	    
-
 
610
	    for ($k = 0; $k < $nk; $k++) {
-
 
611
	        $hcrop = round($hcrop0 - $k*$hinc);
-
 
612
	        $wcrop = round($wcrop0 - $k*$winc);
-
 
613
	        $xcrop = $xcenter - $wcrop / 2;
-
 
614
	        $ycrop = $ycenter - $hcrop / 2;
-
 
615
	        //echo("crop: $wcrop, $hcrop, $xcrop, $ycrop");
-
 
616
	
-
 
617
	        if ($xcrop < 0) $xcrop = 0;
-
 
618
	        if ($xcrop+$wcrop > $largeur_image_originale) $xcrop = $largeur_image_originale-$wcrop;
-
 
619
	        if ($ycrop < 0) $ycrop = 0;
-
 
620
	        if ($ycrop+$hcrop > $hauteur_image_originale) $ycrop = $hauteur_image_originale-$hcrop;
-
 
621
		        
-
 
622
	        /*if (self::MODE_DEBUG) {
-
 
623
	        	// debug
-
 
624
	        	$currfile = '/home/aurelien/web/file_tmp/'."image$k.jpg";
-
 
625
	        	
-
 
626
	            $currimg = clone $img;
-
 
627
	            $c= new ImagickDraw(); 
-
 
628
	            $c->setFillColor("red"); 
-
 
629
	            $c->circle($xcenter, $ycenter, $xcenter, $ycenter+4); 
-
 
630
	            $currimg->drawImage($c); 
-
 
631
	            $currimg->cropImage($wcrop, $hcrop, $xcrop, $ycrop);
-
 
632
	            $currimg->writeImage($currfile);
-
 
633
	            $currimg->destroy();
-
 
634
	        }*/
-
 
635
	
-
 
636
	        $beta = 0;
-
 
637
	        for ($c=0; $c<$n; $c++) {
-
 
638
	            $i = mt_rand(0,$wcrop-1);
-
 
639
	            $j = mt_rand(0,$hcrop-1);
-
 
640
	            $beta += imagecolorat($im, $xcrop+$i, $ycrop+$j) & 0xFF;
-
 
641
	        }
-
 
642
	        $area = $wcrop * $hcrop;
-
 
643
	        $betanorm = $beta / ($n*pow($area, $gamma-1));
-
 
644
	        // echo("beta: $beta; betan: $betanorm");
-
 
645
	        // echo("image$k.jpg:<br/>\n<img src=\"$currfile\"/>");
-
 
646
	        // best image found, save it
-
 
647
	        
-
 
648
	        if ($betanorm > $maxbetanorm) {
-
 
649
	        	
-
 
650
	            $maxbetanorm = $betanorm;
-
 
651
	            $maxparam['w'] = $wcrop;
-
 
652
	            $maxparam['h'] = $hcrop;
-
 
653
	            $maxparam['x'] = $xcrop;
-
 
654
	            $maxparam['y'] = $ycrop;
-
 
655
	            // $maxfile = $currfile;
-
 
656
	        }
-
 
657
	    }
-
 
658
	
-
 
659
        // return image
-
 
660
        $imgcp->cropImage($maxparam['w'], $maxparam['h'], $maxparam['x'], $maxparam['y']);
-
 
661
        $imgcp->scaleImage($largeur_vignette, $hauteur_vignette);
-
 
662
        $imgcp->writeImage($out);
-
 
663
	    chmod($out, 0777);
-
 
664
	    $img->destroy();
-
 
665
	    $imgcp->destroy();
-
 
666
	    
-
 
667
	    $image_sortie = ImageCreateFromJpeg($out);
-
 
668
	    unlink($out);
-
 
669
	    
465
		
670
	    return $image_sortie;
466
		return $destruction_formats_fichier;
671
	}
467
	}
672