Code: Select all
class STCThumb
{
//Thumb::resize('ffff.jpg','fff_.jpg',320,240,65);
function resize($origfile,$dstfile,$max_width,$max_height,$quality)
{
//echo "STCThumb::resize ORIG=[$origfile],DEST=[$dstfile],MAX_WIDTH=[$max_width],MAX_HEIGHT=[$max_height],QUALITY=[$quality]";
// Path to your jpeg
$size = GetImageSize($origfile); // Read the size
$width = $size[0];
$height = $size[1];
if($width == $max_width || $height == $max_height)
{
//dont do anything
copy($origfile, $dstfile);
}
// Proportionally resize the image to the
// max sizes specified above
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if( ($width <= $max_width) && ($height <= $max_height) )
{
$tn_width = $width;
$tn_height = $height;
}
elseif (($x_ratio * $height) < $max_height)
{
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}
else
{
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
// Increase memory limit to support larger files
ini_set('memory_limit', '32M');
// Create the new image!
$src = ImageCreateFromJpeg($origfile);
$dst = ImageCreateTrueColor($tn_width, $tn_height);
ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
// ImageJpeg($dst);
//save new file
$itworked = imagejpeg($dst,$dstfile,$quality);
// Destroy the images
ImageDestroy($src);
ImageDestroy($dst);
return $itworked;
}
function create($name,$filename,$new_w,$new_h,$quality=75)
{
$src_img=imagecreatefromjpeg($name);
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
$dst_img=ImageCreateTrueColor($new_w,$new_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,$old_x,$old_y);
$itworked = imagejpeg($dst_img,$filename,$quality);
imagedestroy($dst_img);
imagedestroy($src_img);
return $itworked;
}
}