class Image
{
function getInfo($file)
{
return getimagesize($file);
}
function prop($file)
{
list($width, $height) = getimagesize($file);
return floor($width/$height);
}
/*
RETURN: true ok
0 errore
-1 immagine troppo piccola
*/
function ridimensiona($from,$to,$x,$y,$rimpicciolisci=true,$taglia=false,$riempi=false)
{
$info=Image::getInfo($from);
switch ($info[2])
{
case(2):
$funzione1="ImageCreateFromJPEG";
$funzione2="ImageJPEG";
break;
case(1):
$funzione1="ImageCreateFromGIF";
$funzione2="ImageGIF";
break;
case(3):
$funzione1="ImageCreateFromPNG";
$funzione2="ImagePNG";
break;
default:
return 3;
}
if (!$first=$funzione1($from))
return 1;
$width = imagesx($first);
$height = imagesy($first);
if ($rimpicciolisci)
{
if ((is_numeric($y) && $height<$y) && (is_numeric($x) && $width<$x))
return 2;
if (!is_numeric($y) && (is_numeric($x) && $width<$x))
return 2;
if ((is_numeric($y) && $height<$y) && !is_numeric($x))
return 2;
}
if (!is_numeric($x)) //larghezza massima infinita
$scale = $y/$height;
else if (!is_numeric($y)) //altezza massima infinita
$scale = $x/$width;
else if (!$taglia || $riempi) //non si taglia ma si rimpicciolisce e poi (in caso) si riempie di colore lo sfondo
$scale = min($x/$width, $y/$height);
else //si taglia ma il meno possibile
$scale = max($x/$width, $y/$height);
$new_width = round($scale*$width);
if (!is_numeric($x))
{
if (!$riempi)
$x=$new_width;
else
{
list($x_da,$x_a)=split(":",$x);
if (is_numeric($x_da) && $x_da>$new_width)
$x=$x_da;
else
$x=$new_width;
}
}
$new_height = round($scale*$height);
if (!is_numeric($y))
{
if (!$riempi)
$y=$new_height;
else
{
list($y_da,$y_a)=split(":",$y);
if (is_numeric($y_da) && $y_da>$new_height)
$y=$y_da;
else
$y=$new_height;
}
}
if (!($second=imagecreatetruecolor($new_width, $new_height)))
return 1;
if (!imagecopyresampled($second,$first,0,0,0,0,$new_width,$new_height,$width,$height))
return 1;
if ($taglia || $riempi)
{
if (!($terza=imagecreatetruecolor($x, $y)))
return 1;
if ($riempi)
{
list($r,$g,$b)=split(",",$riempi);
$color = ImageColorAllocate( $terza, $r, $g, $b );
imagefill($terza,0,0,$color);
}
$left= ($new_width-$x)>0?($new_width-$x)/2:0;
$top = ($new_height-$y)>0?($new_height-$y)/2:0;
$dst_left=($x-$new_width)>0?($x-$new_width)/2:0;
$dst_top = ($y-$new_height)>0?($y-$new_height)/2:0;
if (!imagecopy($terza,$second,$dst_left,$dst_top,$left,$top,$new_width,$new_height))
return 1;
if (!$funzione2($terza,$to,100))
return 1;
imagedestroy($terza);
}
else
{
if (!$funzione2($second,$to,100))
return 1;
}
imagedestroy($second);
imagedestroy($first);
return false;
}
}
?>