GlisWeb framework
_image.tools.php
Vai alla documentazione di questo file.
1 <?php
2 
18  function imageSize( $f ) {
19 
20  // inizializzazione variabili
21  fullPath( $f );
22 
23  // se il file da processare esiste
24  if( file_exists( $f ) ) {
25 
26  // debug
27  // echo $f . PHP_EOL;
28 
29  // prelevo le dimensioni del file
30  $d = getimagesize( $f );
31 
32  $w = $d[0];
33  $h = $d[1];
34 
35  $o = ( $w >= $h ) ? 'l' : 'p';
36  $g = ( $w >= $h ) ? $w : $h;
37  $l = ( $w >= $h ) ? $h : $w;
38 
39  $r = ( $o == 'l' ) ? ( $w / $h ) : ( $h / $w );
40 
41  // restituzione del risultato
42  return array(
43  "h" => $h // altezza dell'immagine
44  ,
45  "w" => $w // larghezza dell'immagine
46  ,
47  "r" => $r // rapporto lato maggiore / lato minore
48  ,
49  "o" => $o // orientamento dell'immagine
50  ,
51  "g" => $g // lato maggiore dell'immagine
52  ,
53  "l" => $l // lato minore dell'immagine
54  );
55 
56  } else {
57 
58  // errore
59  return false;
60 
61  }
62 
63  }
64 
70  function imageOpen( $f ) {
71 
72  fullPath( $f );
73  checkfolder( dirname( $f ) );
74 
75  switch( exif_imagetype( $f ) ) {
76  case IMAGETYPE_JPEG:
77  return imagecreatefromjpeg( $f );
78  break;
79  case IMAGETYPE_PNG:
80  return imagecreatefrompng( $f );
81  break;
82  case IMAGETYPE_WEBP:
83  return imagecreatefromwebp( $f );
84  break;
85  default:
86  return false;
87  break;
88  }
89 
90  }
91 
97  function imageCut( $fs, $d = 1024, $fd = false, $b = 'MIDDLE' ) {
98 
99  // handler dell'immagine sorgente
100  $is = imageOpen( $fs );
101 
102  // dimensioni dell'immagine
103  $dim = imageSize( $fs );
104 
105  // orientamento dell'immagine
106  if( $dim['o'] == 'l' ) {
107 
108  // se l'immagine è orizzontale, tengo bloccata l'altezza e croppo la larghezza
109  $ch = $dim['h'];
110  $cw = $d;
111  $x = ( $b == 'START' ) ? 0 : ( ( $b == 'MIDDLE' ) ? round( ( $dim['w'] - $d ) / 2 ) : ( $dim['w'] - $d ) );
112  $y = 0;
113 
114  } else {
115 
116  // se l'immagine è verticale, tengo bloccata la larghezza e croppo l'altezza
117  $ch = $d;
118  $cw = $dim['w'];
119  $x = 0;
120  $y = ( $b == 'START' ) ? 0 : ( ( $b == 'MIDDLE' ) ? round( ( $dim['h'] - $d ) / 2 ) : ( $dim['h'] - $d ) );
121 
122  }
123 
124  // crop
125  $id = imagecrop( $is, array( 'x' => $x, 'y' => $y, 'width' => $cw, 'height' => $ch ) );
126 
127  // palette e canale alfa
128  if( strtolower( getFileExtension( $fd ) ) == 'png' ) {
129  imagealphablending( $id, false );
130  imagesavealpha( $id, true );
131  imagepalettecopy( $is, $id );
132  }
133 
134  // debug
135  // imagestring( $id, 5, 5, 30, $cw . 'x' . $ch, imagecolorallocate( $id, 255, 0, 0 ) );
136 
137  // scrivo l'immagine
138  imageWrite( $id, $fd );
139 
140  }
141 
147  function imageWrite( $id, $f, $t = NULL ) {
148 
149  fullPath( $f );
150  checkfolder( dirname( $f ) );
151 
152  if( $t === NULL ) {
153  $t = strtolower( getFileExtension( $f ) );
154  }
155 
156  switch( $t ) {
157  case 'jpg':
158  case 'jpeg':
159  case IMAGETYPE_JPEG:
160  imagejpeg( $id, $f );
161  break;
162  case 'png':
163  case IMAGETYPE_PNG:
164  imagealphablending( $id, false );
165  imagesavealpha( $id, true );
166  imagepng( $id, $f );
167  break;
168  case 'webp':
169  case IMAGETYPE_WEBP:
170  imagewebp( $id, $f );
171  break;
172  default:
173  return false;
174  break;
175  }
176 
177  }
178 
184  function imageConvert( $fs, $td, $fd = NULL ) {
185 
186  // apro l'immagine sorgente
187  $id = imageOpen( $fs );
188 
189  // file di destinazione
190  if( $fd === NULL ) {
191  $fd = str_replace( '.'.getFileExtension($fs), '.'.$td, $fs );
192  }
193 
194  // debug
195  // echo $fd . PHP_EOL;
196 
197  // scrivo l'immagine
198  imageWrite( $id, $fd );
199 
200  }
201 
207  function imageResize( $fs, $d = 1024, $fd = false, $o = NULL ) {
208 
209  // estensioni supportate
210  $ext = array( 'jpg', 'jpeg', 'png', 'webp' );
211 
212  // se non viene specificato un file di destinazione, assume che sia uguale al sorgente
213  if( $fd == false ) {
214  $fd = $fs;
215  }
216 
217  // creo i full path
218  fullPath( $fs );
219  fullPath( $fd );
220 
221  // controllo il path di destinazione
222  checkFolder( dirname( $fd ) );
223 
224  // estensione
225  $x = strtolower( getFileExtension( $fs ) );
226 
227  // controllo che la dimensione max sia diversa da 0 altrimenti copio l'immagine senza scalarla
228  if( empty( $d ) ) {
229 
230  // copio l'immagine
231  copy( $fs, $fd );
232 
233  // restituisco true
234  return true;
235 
236  } elseif( file_exists( $fs ) && in_array( $x, $ext ) ) {
237 
238  // prende in entrata le dimensioni dell'immagine originale
239  $dimensioni = getimagesize( $fs );
240 
241  $altezza = $dimensioni[1]; // l'altezza dell'immagine
242  $larghezza = $dimensioni[0]; // la larghezza dell'immagine
243 
244  // se l'immagine ha dimensioni nulle, allora il file è danneggiato
245  if( empty( $altezza ) || empty( $larghezza ) ) {
246 
247  // log
248  logWrite( 'file danneggiato: ' . $fs, 'image', LOG_ERR );
249 
250  // restituisco false
251  return false;
252 
253  } else {
254 
255  // nel caso l'immagine sia piu' alta che larga...
256  if( $altezza > $larghezza && $o != 'l' ) {
257 
258  // ...viene ridimensionata a partire dalla larghezza e l'altezza viene scalata proporzionalmente
259  $valore_riferimento = $larghezza;
260  $valore_secondario = $altezza;
261 
262  // scalatura proporzionale della dimensione secondaria
263  $alpha_dim = round( ( $d * $valore_riferimento ) / $valore_secondario );
264  $beta_dim = $d;
265 
266  } else {
267 
268  // ...altrimenti viene ridimensionata a partire dall'altezza e la larghezza viene scalata proporzionalmente
269  $valore_riferimento = $altezza;
270  $valore_secondario = $larghezza;
271 
272  // dimensione principale valorizzata al massimo consentito
273  $alpha_dim = $d;
274 
275  // scalatura proporzionale della dimensione secondaria
276  $beta_dim = round( ( $d * $valore_riferimento ) / $valore_secondario );
277 
278  }
279 
280  // creazione dell'immagine di destinazione
281  $identificatore_destinazione = imagecreatetruecolor( $alpha_dim , $beta_dim );
282 
283  // TODO questa cosa andrebbe fatta con
284  // $identificatore_provenienza = imageOpen( $fs );
285 
286  // a seconda del tipo di file viene creato un file immagine differente
287  switch( $x ) {
288  case 'jpg':
289  case 'jpeg':
290  $identificatore_provenienza = imagecreatefromjpeg( $fs );
291  break;
292  case 'png':
293  imagealphablending( $identificatore_destinazione , false );
294  imagesavealpha( $identificatore_destinazione , true );
295  $identificatore_provenienza = imagecreatefrompng( $fs );
296  break;
297  case 'webp':
298  $identificatore_provenienza = imagecreatefromwebp( $fs );
299  break;
300  case 'gif':
301  default:
302  $exit_message = 'formato non supportato: ' . $x;
303  break;
304  }
305 
306  // controllo che l'immagine sia valida
307  if( empty( $identificatore_provenienza ) ) {
308 
309  // log
310  logWrite( 'impossibile leggere: ' . $fs, 'image', LOG_DEBUG );
311 
312  // restituisco false
313  return false;
314 
315  } else {
316 
317  // log
318  logWrite( 'lettura ok: ' . $fs, 'image', LOG_DEBUG );
319 
320  // trasferimento della palette dall'immagine creata in memoria al file di destinazione
321  imagepalettecopy( $identificatore_provenienza , $identificatore_destinazione );
322 
323  // copiatura dell'immagine creata in memoria sul file
324  $cont = imagecopyresampled(
325  $identificatore_destinazione,
326  $identificatore_provenienza,
327  0,0,0,0,
328  $alpha_dim,
329  $beta_dim,
330  $larghezza,
331  $altezza
332  );
333 
334  // esito dell'operazione
335  if( $cont ) {
336 
337  logWrite( 'scalamento corretto a ' . $d . 'px di: ' . $fs, 'image' , LOG_DEBUG );
338  $exit_message = 'file creato con successo';
339 
340  } else {
341 
342  logWrite( 'impossibile scalare a ' . $d . 'px: ' . $fs, 'image' , LOG_DEBUG );
343  $exit_message = 'impossibile creare il file ' . $fd;
344 
345  // output
346  return false;
347 
348  }
349 
350  // debug
351  // imagestring( $identificatore_destinazione, 5, 5, 5, $alpha_dim . 'x' . $beta_dim, imagecolorallocate( $identificatore_destinazione, 255, 0, 0 ) );
352 
353  // scrittura del file
354  switch( $x ) {
355 
356  case 'jpg':
357  case 'jpeg':
358  $cont = imagejpeg( $identificatore_destinazione , $fd );
359  break;
360 
361  case 'png':
362  $cont = imagepng( $identificatore_destinazione , $fd );
363  break;
364 
365  case 'webp':
366  $cont = imagewebp( $identificatore_destinazione , $fd );
367  break;
368 
369  case 'gif':
370  default:
371  logWrite( 'formato ' . $x . ' non supportato' , 'image' , LOG_DEBUG );
372  break;
373 
374  }
375 
376  // esito dell'operazione
377  if( $cont ) {
378 
379  logWrite( 'scrittura corretta: ' . $fd, 'image' , LOG_DEBUG );
380  $exit_message = 'file creato con successo';
381 
382  // output
383  return true;
384 
385  } else {
386 
387  logWrite( 'impossibile scrivere: ' . $fd, 'image' , LOG_DEBUG );
388  $exit_message = 'impossibile creare il file ' . $fd;
389 
390  // output
391  return false;
392 
393  }
394 
395  }
396 
397  }
398 
399  } else {
400 
401  // output
402  return false;
403 
404  }
405 
406  }
407 
408  if( ! function_exists( 'imagecrop' ) ) {
409  function imagecrop( $src, array $rect ) {
410  $dest = imagecreatetruecolor( $rect['width'], $rect['height'] );
411  imagecopyresampled(
412  $dest,
413  $src,
414  0,
415  0,
416  $rect['x'],
417  $rect['y'],
418  $rect['width'],
419  $rect['height'],
420  $rect['width'],
421  $rect['height']
422  );
423  return $dest;
424  }
425  }
426 
427 ?>
imageResize( $fs, $d=1024, $fd=false, $o=NULL)
logWrite( $m, $f='site', $l=LOG_NOTICE, $d=DIRECTORY_LOG, $t=CURRENT_LOG_LEVEL, $s=SITE_STATUS)
scrive un messaggio nei log del sito
Definition: _log.utils.php:48
$f
Definition: _filesystem.php:21
if(!empty($_REQUEST['id'])) $d
imageCut( $fs, $d=1024, $fd=false, $b='MIDDLE')
checkFolder( $p, &$e=NULL)
verifica l&#39;esistenza di un path di directory creando quelle mancanti
$r
Definition: _osm.php:25
imageConvert( $fs, $td, $fd=NULL)
imageWrite( $id, $f, $t=NULL)
imageSize( $f)
getFileExtension( $f)
recupera l&#39;estensione del file
if(! isset( $_REQUEST['__view__'][ $ct['view']['id']]['__extra__']['assegnato'])|| $_REQUEST['__view__'][ $ct['view']['id']]['__extra__']['assegnato']=='__me__') elseif($_REQUEST[ '__view__'][$ct[ 'view'][ 'id']][ '__extra__'][ 'assegnato']=='__nessuno__')
imageOpen( $f)
fullPath(&$f)