Here is function which:
- resize and crop (crop format 4:3 or 1:1)
- methode resample/resize
- open jpg, gif, bmp, png
- save jpg (85%), gif, bmp, png
- show images
$FILE_MIMES = array(
'image/pjpeg'=>"jpg",
'image/jpeg'=>"jpg",
'image/jpg'=>"jpg",
'image/png'=>"png",
'image/x-png'=>"png",
'image/gif'=>"gif",
'image/bmp'=>"bmp");
$FILE_EXTS = array('.jpeg','.jpg','.png','.gif','.bmp');
$MAX_SIZE = 16777216;
$lng=array(
's_imgDimension'=>array(
"80",
"320"
),
's_imgCrop' =>array(
"- auto -",
"na sirku",
"na vysku",
"ctverec",
"- zadne -"
),
's_imgFormat' =>array(
"4/3",
"1/1"
),
's_imgType' =>array(
"jpg",
"png",
"gif",
"bmp"
),
's_imgMethode' =>array(
"resample",
"resize"
)
);
function PPimageTransform($photo1,$type1,$dim,$crop,
$format,$methode,$photo2,$type2)
{
if ($photo1!=="" && File_Exists($photo1))
{
if (!($crop>0 && $crop<5)) $crop=0;
width=x"
list($w,$h) = GetImageSize($photo1);
$w=($w<1)?1:$w;
$h=($h<1)?1:$h;
$wh=$w/$h;
// format
$format=($format=='4/3')?4/3:1;
// auto crop
if ($crop==0)
{
$z1=$format- ($format-1)/2;
$z2=$format+(1-$format-1)/2;
if ($wh>$z1) {$crop=1;}
elseif ($wh<$z2) {$crop=2;}
else {$crop=3;}
}
// scale
if ($crop==4)
{
$a=$w>$h?$w:$h;
$scale=$dim/$a;
$nw=$w*$scale;$nw=($nw<1)?1:$nw;
$nh=$h*$scale;$nh=($nh<1)?1:$nh;
$d=array(0,0,floor($nw),floor($nh));
$s=array(0,0,$w,$h);
}
else
{
if ($crop==3) {$nw=1;$nh=1;}
elseif ($crop==2) {$nw=1/$format;$nh=1;}
elseif ($crop==1) {$nw=1;$nh=1/$format;}
$nw*=$dim;$nw=($nw<1)?1:$nw;
$nh*=$dim;$nh=($nh<1)?1:$nh;
$s1=$nw/$w;
$s2=$nh/$h;
$h1=$nh*$s1;
$w2=$nw*$s2;
$s1=($h1<=$nh)?$s1:0;
$s2=($w2<=$nw)?$s2:0;
$scale=($s1>$s2)?$s1:$s2;
$scale=($scale<=0)?1:$scale;
$d=array(0,0,floor($nw),floor($nh));
$x1=floor(($w-$d[2]/$scale)/2);$x1=$x1<0?0:$x1;
$y1=floor(($h-$d[3]/$scale)/2);$y1=$y1<0?0:$y1;
$s=array($x1,$y1,floor($w-2*$x1),floor($h-2*$y1));
}
// input/output
switch($type1)
{
case 'jpg': $imgIn = ImageCreateFromJPEG; break;
case 'png': $imgIn = ImageCreateFromPNG; break;
case 'gif': $imgIn = ImageCreateFromGIF; break;
case 'bmp': $imgIn = ImageCreateFromWBMP; break;
default: return FALSE; break;
}
switch($type2)
{
case 'jpg': $imgOut = ImageJPEG; break;
case 'png': $imgOut = ImagePNG; break;
case 'gif': $imgOut = ImageGIF; break;
case 'bmp': $imgOut = ImageWBMP; break;
default: return FALSE; break;
}
// resample (d-destination, s-source)
$image1 = $imgIn($photo1);
if ($methode==0)
{
$image2 = ImageCreateTruecolor($d[2],$d[3]);
ImageCopyResampled($image2, $image1,
$d[0],$d[1], $s[0],$s[1], $d[2],$d[3], $s[2],$s[3]);
}
else {
$image2 = ImageCreate($d[2],$d[3]);
ImageCopyResized($image2, $image1,
$d[0],$d[1], $s[0],$s[1], $d[2],$d[3], $s[2],$s[3]);
}
// output
if ($type2=='jpg')
{$imgOut($image2,$photo2,85);}
else {$imgOut($image2,$photo2);}
ImageDestroy($image2);
ImageDestroy($image1);
}
}