Function to resize an image
function resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
// create an image of the given filetype
if ($ext == 'jpg' || $ext == 'jpeg'){
if(!function_exists("imagecreatefromjpeg")) return 0;
$image = @imagecreatefromjpeg($from);
}elseif($ext == 'png') {
if(!function_exists("imagecreatefrompng")) return 0;
$image = @imagecreatefrompng($from);
}elseif($ext == 'gif') {
if(!function_exists("imagecreatefromgif")) return 0;
$image = @imagecreatefromgif($from);
}
if(!$image) return 0;
if(function_exists("imagecreatetruecolor")){
$newimg = @imagecreatetruecolor($to_w, $to_h);
}
if(!$newimg) $newimg = @imagecreate($to_w, $to_h);
if(!$newimg){
imagedestroy($image);
return 0;
}
//keep png alpha channel if possible
if($ext == 'png' && function_exists('imagesavealpha')){
imagealphablending($newimg, 0);
imagesavealpha($newimg,true);
}
//try resampling first
if(function_exists("imagecopyresampled")){
if(!@imagecopyresampled($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h)) {
imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
}
}else{
imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
}
$okay = 0;
if ($ext == 'jpg' || $ext == 'jpeg'){
if(!function_exists('imagejpeg')){
$okay = 0;
}else{
$okay = imagejpeg($newimg, $to);
}
}elseif($ext == 'png') {
if(!function_exists('imagepng')){
$okay = 0;
}else{
$okay = imagepng($newimg, $to);
}
}elseif($ext == 'gif') {
if(!function_exists('imagegif')){
$okay = 0;
}else{
$okay = imagegif($newimg, $to);
}
}
// destroy GD image ressources
if($image) imagedestroy($image);
if($newimg) imagedestroy($newimg);
return $okay;
}
No comments:
Post a Comment