01 December 2008

Dynamic Thumbnail Generation


Thumbnail creation is an very important process for websites incorporating image upload functionality.
User can upload any type of image to the server which needs to be re-sized according to our web page designs.
Following is the code required for the task in ASP.NET 2.0:



void create_thumb(String file_path,int pre_width,int pre_height)
{
System.Drawing.Image myImage;
System.Drawing.Image oThumbNail;
int newHeight, newWidth;

Graphics oGraphic;
Rectangle oRectangle;

myImage = System.Drawing.Image.FromFile(Server.MapPath("images") + "/" + file_path);

if (myImage.Height < pre_height && myImage.Width < pre_width)
{
newWidth = myImage.Width;
newHeight = myImage.Height;
}
else
{
if (myImage.Height > myImage.Width)
{
newHeight = pre_height;
newWidth = ((myImage.Width * pre_height) / myImage.Height);
}
else if (myImage.Height < myImage.Width)
{
newWidth = pre_width;
newHeight = ((myImage.Height * pre_width) / myImage.Width);
}
else
{
newWidth = pre_width;
newHeight = ((myImage.Height * pre_width) / myImage.Width);
}
}



if (myImage.PixelFormat == PixelFormat.Indexed)
oThumbNail = new Bitmap(newWidth, newHeight, myImage.PixelFormat);
else
oThumbNail = new Bitmap(newWidth, newHeight);
oGraphic = Graphics.FromImage(oThumbNail);
oGraphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
oGraphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
oGraphic.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
oRectangle = new Rectangle(0, 0, newWidth, newHeight);
oGraphic.DrawImage(myImage, oRectangle);
Response.ContentType = "image/Jpeg";
oThumbNail.Save(Server.MapPath("images/Thumbnails") + "/" + file_path,System.Drawing.Imaging.ImageFormat.Jpeg);

myImage.Dispose(); //needed to free the resources and file handles
}


Following is the code required for the task in PHP:

function create_thumb($file_path,$pre_width,$pre_height)
{
list($orig_width, $orig_height) = getimagesize($file_path);
$target_path="images/thumbnails/"

if ($orig_height < $pre_height && $orig_width < $pre_width)
{
$newWidth = $orig_width;
$newHeight = $orig_height;
}
else
{
if($orig_height > $orig_width)
{
$newHeight = $pre_height;
$newWidth = (($orig_width * $pre_height) / $orig_height);
}
else if ($orig_height < $orig_width)
{
$newWidth = $pre_width;
$newHeight = (($orig_height * $pre_width) / $orig_width);
}
else
{
$newWidth = $pre_width;
$newHeight = (($orig_height * $pre_width) / $orig_width);
}
}

$image_p = imagecreatetruecolor($newWidth, $newHeight);

if($file_ext=='gif' || $file_ext=='GIF')
$image = imagecreatefromgif($file_path);

if($file_ext=='png' || $file_ext=='PNG' )
$image = imagecreatefrompng($file_path);

if($file_ext=='jpeg' || $file_ext=='jpg')
$image = imagecreatefromjpeg($file_path);

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $newWidth, $newHeight, $orig_width,$orig_height);

if($file_ext=='jpeg' || $file_ext=='jpg' || $file_ext=='JPG' )
imagejpeg($image_p, $target_path.$newname, 100);

if($file_ext=='gif' || $file_ext=='GIF')
imagegif($image_p, $target_path.$newname, 100);

if($file_ext=='png' || $file_ext=='PNG')
imagepng($image_p, $target_path.$newname);


}

Note: This function is designed to first save the image to a location on server and then
generated thumbnails and save in a separate folder. You can customize it according to your need.

No comments: