Wednesday 4 September 2013

How to resize and crop Image Using Coldfusion script

I had requirement on my site www.eventaway.com  to resize Image after uploaded by user in two sizes one for view page and one as thumbnail, I found some script on web and improved it to fit in my requirement. here it is. I expect this code will reside in a CFC function



var l = {}; // local struct

/* Read file  and we will resize and crop it to make 200×200 */

l.img = ImageRead(“path of the image to be resized”);

l.resizeOriginalFlag = 0;
If( l.img.height GT l.img.width )
{
If( l.img.height GT 200)
{
ImageResize( l.img,’200′,”);
l.resizeOriginalFlag = 1;
l.fromX = l.img.Height / 2 – 100;
ImageCrop(l.img,0,l.fromX,200,200);
}
}
else if( l.img.width GT l.img.height )
{
If( l.img.width GT 200 )
{
ImageResize( l.img, ”,’200′);
l.resizeOriginalFlag = 1;
l.fromY = l.img.Width / 2 – 100;
ImageCrop( l.img,l.fromY,0,200,200);
}
}
else
{
If( l.img.height GT 200)
{
ImageResize(l.img,’200′,”);
l.resizeOriginalFlag = 1;
ImageCrop(l.img,0,0,200,200);
}
}




source=”#l.img#”
destination=”
overwrite=”yes”>

<!— <cfdump format="”html”" output="”/sites/resize.html”" var="”#cfcatch#”"> —>



No comments: