function SetImgSize($img, $width, $height, $file='')
{
if (!is_file($img)) return false;
$mime=getimagesize($img);
$r=str_replace('image/', '', $mime['mime']);
switch ($r)
{
case "gif": {$srcImage = ImageCreateFromGIF($img); break;}
case "png": {$srcImage = ImageCreateFromPNG($img); break;}
case "jpeg": {$srcImage = ImageCreateFromJPEG($img); break;}
case "jpg": {$srcImage = ImageCreateFromJPEG($img); break;}
}
$srcWidth = ImageSX($srcImage);
$srcHeight = ImageSY($srcImage);
$dest = imagecreatetruecolor($width, $width);
if ($srcWidth>$srcHeight)
ImageCopyResampled($dest, $srcImage, 0, 0,
round((max($srcWidth,$srcHeight)-min($srcWidth,$srcHeight))/2),
0, $width, $width, min($srcWidth,$srcHeight), min($srcWidth,$srcHeight));
if ($srcWidth<$srcHeight)
ImageCopyResampled($dest, $srcImage, 0, 0, 0, 0, $width, $width,
min($srcWidth,$srcHeight), min($srcWidth,$srcHeight));
if ($srcWidth==$srcHeight)
ImageCopyResampled($dest, $srcImage, 0, 0, 0, 0, $width, $width, $srcWidth, $srcWidth);
switch ($r)
{
case "gif": {$srcImage = ImageGIF($dest, $file, 90); break;}
case "png": {$srcImage = ImagePNG($dest, $file, 9); break;}
case "jpeg": {$srcImage = ImageJPEG($dest, $file, 90); break;}
case "jpg": {$srcImage = ImageJPEG($dest, $file, 90); break;}
}
@ImageDestroy($srcImage);
@ImageDestroy($resImage);
return true;
}