Sunday, October 11, 2009

Resize Image On The Fly

Filename index.php
--------------------------------------------------
// include required files
include_once("image_class.php");


// assign image path
$filename = "f2.jpg";
if(file_exists($filename)){

// Create image class object
$im = new image();
// resize your image using this function
// $filename = image path
// 200 = width
// 200 = height
$im->display_image($filename , 200 , 200);

// opacity
$percent = 0.5;

$image = @getImageSize($filename);
$img_flg = $image[2];

if($img_flg=='1'){

// Resize gif image
// Content type
header('Content-type: image/gif');

// Resample
$image_p = imagecreatetruecolor($im->required['width'], $im->required['height']);
$image = imagecreatefromgif($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $im->required['width'], $im->required['height'], $im->image_property[0], $im->image_property[1]);

// Outputf
imagegif($image_p, null, 100);
}

if($img_flg=='2') {
// Resize jpeg image
// Content type
header('Content-type: image/jpeg');

// Resample
$image_p = imagecreatetruecolor($im->required['width'], $im->required['height']);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $im->required['width'], $im->required['height'], $im->image_property[0], $im->image_property[1]);

// Outputf
imagejpeg($image_p, null, 100);
}


if($img_flg=='3'){
// Resize png Image
// Content type
header('Content-type: image/png');

// Resample
$image_p = imagecreatetruecolor($im->required['width'], $im->required['height']);
$image = imagecreatefrompng($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $im->required['width'], $im->required['height'], $im->image_property[0], $im->image_property[1]);

// Outputf
imagepng($image_p, null, 100);
}

}//if

Filename image_class.php
--------------------------------------------------
/*
+----------------------------------------------------------------------------------------------------------------------------+
# File Name : image_class.php
# File Description :
# Class Name : image
# Class Description : buffureing image and display on web pages
+----------------------------------------------------------------------------------------------------------------------------+
*/
class image{
var $image_property = "";
var $required = "";

function getImageInfo($imagename)
{
if(!file_exists($imagename))
{
trigger_error("Image Library version 2.0 : The image path provided is not a valid path",E_USER_ERROR);
return false;
} // end of the if condition

$this->image_property = getimagesize($imagename); //getting the image size
return true;
}

function __getRatio()
{
if($this->required["height"] > $this->required["width"])
{
if($this->image_property[0] > $this->image_property[1])
{
$ratio = $this->image_property[1] / $this->image_property[0];
$this->image_property[5] = $this->required["width"];
$this->image_property[6] = $this->image_property[5] * $ratio;
}
else
{
$ratio = $this->image_property[0] / $this->image_property[1];
$this->image_property[6] = $this->required["width"];
$this->image_property[5] = $this->image_property[6] * $ratio;

}
}
else
{

if($this->image_property[0] > $this->image_property[1])
{
$ratio = $this->image_property[1] / $this->image_property[0];
$this->image_property[5] = $this->required["width"];
$this->image_property[6] = $this->image_property[5] * $ratio;

}
else
{
$ratio = $this->image_property[0] / $this->image_property[1];
$this->image_property[6] = $this->required["height"];
$this->image_property[5] = $this->image_property[6] * $ratio;
}
}


$this->required["width"] = $this->image_property[5];
$this->required["height"] = $this->image_property[6];
}


function createthumb($img, $new_width, $new_height)
{
//First, check for a valid file.
if (is_file($img))
{
//Now, get the current file size.
if ($this->getImageInfo($img))
{

$this->required['width'] = $new_width;
$this->required['height'] = $new_height;

$this->__getRatio();

//Now that we have the size constraints, let's find the file type.
$thepath = pathinfo ($img);
//Set up our thumbnail.

$dst = imagecreatetruecolor($this->required["width"], $this->required["height"]);
//Make a file name.
$filename = str_replace (".".$thepath['extension'], "", $thepath["basename"]);
$filename = $thepath["dirname"]."/thumb/".$filename . "" . $size . "." . $thepath['extension'];
$types = array('jpg' => array('imagecreatefromjpeg', 'imagejpeg'),
'jpeg' => array('imagecreatefromjpeg', 'imagejpeg'),
'gif' => array('imagecreatefromgif', 'imagegif'),
'png' => array('imagecreatefrompng', 'imagepng'));
$x_type = strtolower($thepath['extension']);
$func = $types[$x_type][0];
$src = $func($img);

//Create the copy.
imagecopyresampled($dst, $src, 0, 0, 0, 0,$this->required["width"], $this->required["height"],$this->image_property[0], $this->image_property[1]);
//Create the thumbnail.
$func = $types[$x_type][1];

$func($dst, $filename);
return $dest;
}
}
//echo "No image found.";
}

function display_image($imagename , $new_width , $new_height)
{
$this->required['width'] = $new_width;
$this->required['height'] = $new_height;
//echo $imagename;
$this->image_property = getimagesize($imagename);
$this->__getRatio();
}


}