Flipkart

Saturday, July 18, 2009

Image Resize

function image($img,$w,$h) {
$a = getimagesize($img);
$image_attribs = getimagesize($img);
if($a['mime'] == "image/jpeg") {
$im_old = imageCreateFromJpeg($img);
}else if($a['mime'] == "image/png") {
$im_old = imageCreateFrompng($img);
}else if($a['mime'] == "image/gif") {
$im_old = imageCreateFromgif($img);
}

$th_max_width = $w;
$th_max_height = $h;
if($w >99 && $h>99){
$ratio = ($width > $height) ? $th_max_width/$image_attribs[0] : $th_max_height/$image_attribs[1];
$th_width = $image_attribs[0] * $ratio;
$th_height = $image_attribs[1] * $ratio;
}else{
$th_width=$w;
$th_height=$h;
}
$im_new = imagecreatetruecolor($th_width,$th_height);
imageAntiAlias($im_new,true);

imageCopyResampled($im_new,$im_old,0,0,0,0,$th_width,$th_height, $image_attribs[0], $image_attribs[1]);

header("Content-type:".$a['mime']);
imageJpeg($im_new,$th_file_name,100);


}
$img = base64_decode($_GET['src']);
//$img = PATH.$img;
$width = $_GET['w'];
$height = $_GET['h'];
$val = image($img,$width,$height);

No comments:

Post a Comment