$imgfile="photo/myson.jpg"; // The Source of your image file function imageCompression($imgfile="",$thumbsize=0,$savePath=NULL) {
if($savePath==NULL) {
header('Content-type: image/jpeg');
/* To display the image in browser
*/
}
list($width,$height)=getimagesize($imgfile);
/* The width and the height of the image also the getimagesize retrieve other information as well */
echo $width;
echo $height;
$imgratio=$width/$height;
/*
To compress the image we will calculate the ration
For eg. if the image width=700 and the height = 921 then the ration is 0.77...
if means the image must be compression from its height and the width is based on its height
so the newheight = thumbsize and the newwidth is thumbsize*0.77...
*/
if($imgratio>1) {
$newwidth=$thumbsize;
$newheight=$thumbsize/$imgratio;
} else {
$newheight=$thumbsize;
$newwidth=$thumbsize*$imgratio;
}
$thumb=imagecreatetruecolor($newwidth,$newheight); // Making a new true color image
$source=imagecreatefromjpeg($imgfile); // Now it will create a new image from the source
imagecopyresampled($thumb,$source,0,0,0,0,$newwidth,$newheight,$width,$height); // Copy and resize the image
imagejpeg($thumb,$savePath,100);
/*
Out put of image
if the $savePath is null then it will display the image in the browser
*/
imagedestroy($thumb);
/*
Destroy the image
*/
} imageCompression($imgfile,150,"a.jpg"); ?>