Archive

Posts Tagged ‘ColdFusion’

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>

Few frequently used Regular Expressions by me.

August 13, 2010 Leave a comment

In ColdFusion:
1) To match a valid UK postcode and returns an array with one item of valid postcode otherwise returns array with no item in it:
Pattern to test (with or without spaces):

^ : is for start of the string

$: is for end of the string

?: zero or one letter (optional)

{1,2}: minimum 1 or maximum 2 characters

Pattern Example
A9 9AA M1 1BA
A99 9AA N12 1UD
AA9 9AA EH9 4UA
AA99 9AA RG6 1WG
A9A 9AA W1A 1NA
AA9A 9AA SW1A 1HQ
Special case for National Girobank
AAA 9AA GIR 0AA

<cfset arr = REMatch(“^[A-Za-z]{1,2}[0-9R][0-9A-Za-z]?[[:space:]]?[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z][A-Za-z]$”,request.postcode)>
<cfif len(arr) GT 1>
<cfreturn true >
</cfif>
<cfreturn true >

2) Another one for UK post code, little bit complicated

<cfset arr = REMatch(“^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$”,request.postcode)>

Few references for UK Postcode regex)

Ref Site Link 1, Ref Site Link 2

3) To validate number with minimum 11 char and maximum 14 car using isValid function

isValid(‘regex’,request.mobileNo,”^[0-9]{11,14}”)

4) To remove all characters from the string except alpha numeric:

REReplace(stringvar, “[^a-zA-Z0-9]”,””,”All”);

In JavaScript:

To restrict any thing except alpha numeric (case insensitive)

string.replace(/[^a-zA-Z0-9]/g, “”)

To Replace brackets, dots and spaces from string globally

string.replace(/[\(\)\.\-\ ]/g, ”)

In ActionScript:

1) Phone number check through regEx validator:

private const UK_PHONE_NO_REGEX:String = ‘^[0+][0-9]{10,14}$’;

number can start with 0 or plus (+) , with minimum 10 and maximum 14 characters

2) removes double quotes from string:

var pattern1:RegExp = new RegExp(‘”‘, “g”);

str.replace(pattern1,”);

JavaScript Regular expression replacement:

Add comma separator to numbers like 10,000.00

variable.toString().split(“”).reverse().join(“”).replace(/(\d\d\d)(?=.)/g, “$1,”).split(“”).reverse().join(“”);
if you want to separate by (.) dot than replace comma with dot in here “$1,” in above string.

Complax Array Sorting in ColdFusion

August 4, 2010 Leave a comment

I needed to sort my cold fusion array which had complex Structure as CFC DTO in the array item.

I found very useful blog of Martin

Its very easy to implement. Actually I changed a little bit, I saved the UDF in a CFC and extended my main CFC to this new CFC and it worked straight away.

Here is the code how to call it.

sortArrayOfObjects(
arrayToSort = ,
sortKeys = [{'keyName':'yourKeyToSortOn'},{'sortOrder':'Ascending OR Descending'}],
doDuplicate = false,
useGetterMethods = false);

To download the code

Happy Coding

Categories: ColdFusion Tags: