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