Archive

Posts Tagged ‘CFSCRIPT’

How to resize and crop Image Using Coldfusion script

April 7, 2013 Leave a comment

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

<cfscript>

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);
}
}

</cfscript>

<cfif l.resizeOriginalFlag>
<cftry>
<cfimage action=”write”
source=”#l.img#”
destination=”<path with filename>”
overwrite=”yes”>
<cfcatch>
<!— <cfdump output=”/sites/resize.html” var=”#cfcatch#” format=”html”> —>
</cfcatch>
</cftry>
</cfif>

Advertisements