Codice PHP:
<?php
ini_set("memory_limit","256M");
function makeThumb($dir,$pic,$n,$t){
list($width, $height, $type, $attr) = getimagesize($pic);
$max_w = 300;
$max_h = 100;
// area utile e offset
if ($width>$height*$max_w/$max_h) {
$a_h = $height;
$a_w = $a_h*$max_w/$max_h;
$delta_x = floor(($width-$a_w)/2);
$delta_y = 0;
}
else {
$a_w = $width;
$a_h = $a_w*$max_h/$max_w;
$delta_x = 0;
$delta_y = floor(($height-$a_h)/2);
}
// verifico che l'immagine originale sia più grande delle dimensioni massime 300*100px
if ($max_w<$width && $max_h<$height) {
// creo una nuova immagine
$thumb = imagecreatetruecolor($max_w,$max_h);
if ($t == 'image/jpeg'){$temp = imagecreatefromjpeg($pic);}
elseif ($t == 'image/gif'){$temp = imagecreatefromgif($pic);}
elseif ($t == 'image/png'){$temp = imagecreatefrompng($pic);}
// ridimensiono l'originale e salvo nella cartella di destinazione
imagecopyresampled($thumb,$temp,0,0,$delta_x,$delta_y,$max_w,$max_h,$a_w,$a_h);
if ($t == 'image/jpeg'){imagejpeg($thumb,"$dir/tb_".$n, 100);}
elseif ($t == 'image/gif'){imagegif($thumb,"$dir/tb_".$n);}
elseif ($t == 'image/png'){imagepng($thumb,"$dir/tb_".$n);}
}
}
function makeThumb2($dir,$pic,$n,$t){
list($width, $height, $type, $attr) = getimagesize($pic);
$max_w = 1024;
$max_h = 768;
$ratio = min($max_w/$width,$max_h/$height);
// verifico che l'immagine originale sia più grande delle dimensioni massime 1024*768px
if ($ratio < 1){
// individuo le nuove dimensioni da assegnare all'immagine
$w = floor($ratio*$width);
$h = floor($ratio*$height);
// creo una nuova immagine con le dimensioni appena calcolate
$thumb = imagecreatetruecolor($w,$h);
if ($t == 'image/jpeg'){$temp = imagecreatefromjpeg($pic);}
elseif ($t == 'image/gif'){$temp = imagecreatefromgif($pic);}
elseif ($t == 'image/png'){$temp = imagecreatefrompng($pic);}
// ridimensiono l'originale e salvo nella cartella di destinazione
imagecopyresampled($thumb,$temp,0,0,0,0,$w,$h,$width,$height);
if ($t == 'image/jpeg'){imagejpeg($thumb,"$dir/".$n, 90);}
elseif ($t == 'image/gif'){imagegif($thumb,"$dir/".$n);}
elseif ($t == 'image/png'){imagepng($thumb,"$dir/".$n);}
}
}
?>
con un colpo solo faccio 2 operazioni, un ridimensionamento e crop 300x100 ed un ridimensionamento proporzionato 1024x768.