code:
function process_image($image, $folder, $prefix = "", $newwidth = 800, $newheight = NULL,
$allow = array("image/jpeg", "image/png", "image/gif"), $sizelimit = 2000000) {
global $error;
if (!is_dir($folder)) {
mkdir($folder, 0777);
}
if (in_array($image["type"], $allow)) {
if ($image["size"] < $sizelimit) {
$uploadedfile = $image['tmp_name'];
switch($image["type"]) {
case "image/jpeg":
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$ratio_orig = $width/$height;
if ($newwidth/$newheight > $ratio_orig) {
$newwidth = $newheight*$ratio_orig;
} else {
$newheight = $newwidth/$ratio_orig;
}
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = $folder.$prefix.$image['name'];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
return true;
break;
case "image/png":
$src = imagecreatefrompng($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$ratio_orig = $width/$height;
if ($newwidth/$newheight > $ratio_orig) {
$newwidth = $newheight*$ratio_orig;
} else {
$newheight = $newwidth/$ratio_orig;
}
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = $folder.$prefix.$image['name'];
imagepng($tmp,$filename);
imagedestroy($src);
imagedestroy($tmp);
return true;
break;
case "image/gif":
$src = imagecreatefromgif($uploadedfile);
list($width,$height)=getimagesize($uploadedfile);
$ratio_orig = $width/$height;
if ($newwidth/$newheight > $ratio_orig) {
$newwidth = $newheight*$ratio_orig;
} else {
$newheight = $newwidth/$ratio_orig;
}
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = $folder.$prefix.$image['name'];
imagegif($tmp,$filename);
imagedestroy($src);
imagedestroy($tmp);
return true;
break;
}
} else {
$error = "File size to big.";
return false;
}
} else {
$error = "File type not allow.";
return false;
}
}
note: $image is an array of the image uploaded (from $_FILES)
1 comment:
Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.
Post a Comment