Tuesday, July 21, 2009

How to Create image file and get image Resource like image height and width in PHP

How to Create image file and get image Resource like image height and width in PHP

In some case u may need the image height or width or image resolution in php function to check weather these image suites with your site or not…..

The following function gives the list

-width of image

-height of image

-resolution of the image

-how to create the image file inside your php

-how to get the image Resource

-hoe to get Extension of image file etc….

To get all this image related resource call the below function

echo $image_width = getImageWidth(/image/path/image.jpg)//here the correct image path should give

function getImageWidth($filename){

if(file_exists($filename))

{

$ext = getExtension($filename);//to get the image extension its defined bellow

if(!$ext)

return 0;

$img_src = getImageResource($ext, $filename);//to get the image resource its defined bellow

return imagesx($img_src);

}

return 0;

}

function getImageHeight($filename){

if(file_exists($filename))

{

$ext = getExtension($filename);

if(!$ext)

return 0;

$img_src = getImageResource($ext, $filename);

return imagesy($img_src);

}

return 0;

}

To get the image Extension here is the function

function getExtension($filename)//the filename is path of the image

{

if($filename == '')

return false;

$ext = substr( $filename, strrpos( $filename, "." )+1 );

return $ext;

}

To get the image resource to get the image width or height

So the following function gives you the resource of the image type

function getImageResource($ext, $filename)//the image extension and the image path should be given

{

$imageresource = '';

if($ext == "jpg" || $ext == "JPG" || $ext == "jpeg" || $ext == "JPEG"){

$imageresource = imagecreatefromjpeg($filename);

}

if($ext == "gif" || $ext == "GIF"){

$imageresource = imagecreatefromgif($filename);

}

if($ext == "bmp" || $ext == "BMP"){

$imageresource = imagecreatefromwbmp($filename);

}

if($ext == "png" || $ext == "PNG"){

$imageresource = imagecreatefrompng($filename);

}

/*if(!$imageresource){//if not valid image

return false;

}*/

return $imageresource;

}

And finally how to create image file by using the extension , image resource and destination filename. The following function create image file with this parameters

Parameter list for the function

//here $ext is extension of the image

//$imgresource is the resource of the image with the width and height this you can get by $dst_im = imagecreatetruecolor($width, $height);

//$dest_filename is the destination file name where you want to save the file

function createImagefile ($ext, $imgresource, $dest_filename)

{

$imgfile = false;

if($ext == "jpg" || $ext == "JPG" || $ext == "jpeg" || $ext == "JPEG"){

$imgfile = imagejpeg($imgresource, $dest_filename);

}

if($ext == "gif" || $ext == "GIF"){

$imgfile = imagegif($imgresource, $dest_filename);

}

if($ext == "png" || $ext == "PNG"){

$imgfile = imagepng($imgresource, $dest_filename);

}

if($ext == "bmp" || $ext == "BMP"){

$imgfile = imagewbmp($imgresource, $dest_filename);

}

/*if($imgfile)

return true;*/

return $imgfile;

}

Have any Bug Please contact me

No comments: