Hey, Would you like to work at Home ?? Just click here No need to pay, just register free and activate your account and get data Entry Work at your Home.

Saturday, September 1, 2007

getExtension, get to generate Thumbnails and other size of images

It is used to convert any image to specific size of image which maintains it's aspect ratio so it will not distort the image and maintain it's resolution.

you save this page with getImage.php
and use it like

getImage.php?file=images/img.jpg&width=100&height=100

so you can able to create thumbnail for the image with height and width of 100,100 respectively.

Using this way, you can also create images with different sizes to use in your web page.


===========================================================================


function getExtension($str)


{

$rVal="";
$pos=strrpos($str,".");
// echo "<br />str=".$str.",Pos=".$pos.",Len=".strlen($str)."<br />";
if($pos!==false)
{
$rVal=substr($str,($pos+1));
}
return $rVal;

}



$inputFilename=$_REQUEST['file'];
$imagedata = getimagesize($inputFilename);
$ext=getExtension($inputFilename);
$new_w=$_REQUEST['width'];
$new_h=$_REQUEST['height'];

//print_r($_REQUEST);

switch($ext)
{
case "bmp":
$image=imagecreatefromwbmp($inputFilename);
break;
case "jpg" || "jpeg" :
$image = ImageCreateFromJpeg($inputFilename);
break;
case "png":
$image = imagecreatefrompng($inputFilename);
break;
case "gif":
$image = imagecreatefromgif($inputFilename);
break;
default:
$image=NULL;
}
if($image)
{
//get the image size of the picture and load it into an array
$old_x=imageSX($image);
$old_y=imageSY($image);
if ($old_x > $old_y)
{
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y)
{
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y)
{
$thumb_w=$new_w;
$thumb_h=$new_h;
}

$im2=ImageCreateTrueColor($thumb_w,$thumb_h);

imagecopyresized($im2,$image,0,0,0,0,$thumb_w,$thumb_h,imagesx($image),imagesy($image)); //

switch($ext)
{
case "bmp":
image2wbmp($im2);
break;
case "jpg" || "jpeg" :
imagejpeg($im2);
break;
case "png":
imagepng($im2);
break;
case "gif":
imagegif($im2);
break;
default:
$image=NULL;
}
}


===========================================================================

No comments:

Your Ad Here