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;
}
Tips and helps for my fellow PHP programmers.Here we are bound to build a PHP world.Anyone is welcome to share thier knowledge in PHP with others.Lets share and grow together
Showing posts with label function. Show all posts
Showing posts with label function. Show all posts
Tuesday, July 13, 2010
Thursday, January 18, 2007
PHP function to get extension of a file
Here is a simple usefull function to get extension of a file
function getFileExtension($filename){
$dot = substr (strrchr ($filename, "."), 1);
return ($dot);
}
function getFileExtension($filename){
$dot = substr (strrchr ($filename, "."), 1);
return ($dot);
}
PHP function see if url exists
Here is an simple usefull function see if an url exists
function url_exists($url)
{
$handle = @fopen($url, "r");
if ($handle === false)
return false;
fclose($handle);
return true;
}
function url_exists($url)
{
$handle = @fopen($url, "r");
if ($handle === false)
return false;
fclose($handle);
return true;
}
Subscribe to:
Posts (Atom)