Open Source CAPTCHA CFC v0.1 Released

Here is the initial release of my Captcha component .

It is a reworking of the code I use on this site to output my Captcha images, into a nice CFC that has two public methods -

  • captchaToFile(…) – This method writes a Captcha image defined by the arguments passed in to the file you tell it to set.
  • captchaToBinary(…) – This returns binary data that can be used by cfcontent to display the generated image on the fly.

The code here is pretty much 'as is'. The CFC should be relatively self explanatory, but I have provided examples in the zip file on how to use the CFC both ways.

If you have any feedback, feel free to send me an email .

Leave a Comment

Comments

  • Peter J. Farrell | January 20, 2006

    Looks very interesting…can’t wait to try it out.

  • Derek | January 22, 2006

    when using the cfcontent one, I get a strange issue. I tries to save the file as a .cfm when i go to the page to preview it. I think it has to do with the MIME type ..

  • Derek | January 22, 2006

    found the problem, your type needs to be image/jpeg not jpg.

  • Mark | January 23, 2006

    Derek,
    If you look in the examples, you will see that the type set on the cfcontent tag is ‘image/jpg’.

    Let me know if you run into any other issues.

  • Meikel | January 24, 2006

    When using the cfcontent one, the image isn’t resolved.
    You can edit the source code and see:
    <img src="captcha.jpg">
    <br/><br/>
    <img src="cfcontent.cfm">

    Do I need cf 7.0? I tried it wit CF 6.1.

    Thank you

    Meikel

  • Derek Perez | January 24, 2006

    No no, just change the type="image/jpg" to "image/jpeg" it was a typo, you need the "e" (for iis 6 at least).

  • Derek Perez | January 24, 2006

    hey! I added you to wikipedia:
    http://en.wikipedia.org/wiki/Captcha

    there is a list of coldfusion Captcha components, i added you to the list :) hope you don’t mind!

  • Mark | January 24, 2006

    Oh My G-d.

    I’m so sorry Derek.. I didn’t read your post properly.

    You are totally correct in your statement about the mime type.

    I’ve updated the example, and am uploading it now.

    And of course I don’t mind you added me to the Wiki, totally cool.

    Thanks, mate.

  • Ryan K | January 25, 2006

    Nice…I’ll have to check it out. I actually found you from the wiki, which looks like you were just added today. How’s that for results?

    I’ll bring up your captcha project in our MMUG meeting tomorrow.

  • Peter J. Farrell | January 25, 2006

    Mark,

    BTW, the writeToStream() is missing two var’ed variables – encode and ecodeParam. This might create some threading problems.

    Best,
    .Peter

  • Meikel | January 26, 2006

    Hi,
    I changed the code to: <cfcontent type="image/jpeg" variable="#binary#">
    but the image isn’t shown. I always get the small red cross and the
    coldfusion statement in index.cfm: <img src="cfcontent.cfm"> isn’t replaced with the image. What’s wrong?

    Thank you for your help

    Meikel

  • Mark | January 26, 2006

    Peter –

    You are totally correct, looks like I missed that one. I’ve fixed it and re-uploaded the code.

    Mekel – What happens when you access cfocntent directly? What browser are you using? What version of CF?

  • Meikel | January 26, 2006

    Hi Mark,
    here is the error:
    Attribute validation error for tag CFCONTENT.
    The tag does not allow the attribute(s) VARIABLE. The valid attribute(s) are DELETEFILE,FILE,RESET,TYPE.

    The error occurred in E:Inetpubwwwrootcaptchacfcontent.cfm: line 37

    35 : </cfscript>
    36 :
    37 : <cfcontent type="image/jpeg" variable="#binary#">

    Thank you

    Meikel

  • Meikel | January 26, 2006

    Sorry,
    I forgot:
    CF: 6.1,
    Browser Internet Explorer 6, Mozilla, Netscape 7.1

    Greetings

    Meikel

  • Mark | January 27, 2006

    I should have noted somewhere that the variable attribute is only available in CF7. Sorry, I only just realised.

    For CF6.1, you will have to write the captcha out to a file, or you can experiment with pushing the bytes out the response.getOutputStream() like so:

    http://weblogs.macromedia.com/cantrell/archives/2003/06/using_coldfusio.cfm

    I found there were issues doing that with the JpegEncoder lib in CF7, I’m not sure if they exitst in CF6.1.

  • Meikel | January 30, 2006

    Thank you very much

    Great Job!!!

    Meikel

  • David | February 2, 2006

    Is there a way to limit the set of fonts that are used?

  • Mark | February 3, 2006

    David –

    There is currently no way of explicitly setting the fonts to be used without editing the underlying code.

    The Captcha grabs a lists of the fonts that are available on the computer, and selects one at random to display the next character. If the next character can be displayed with the given font, it will use it, otherwise it will keep searching for the next font that can display the given character.

    That’s pretty much it.

  • vivek | March 9, 2006

    Attribute validation error for tag CFCONTENT.

    I am using captcha for image genrated with random text but Second image is not showing up.

    can you give the suggestion to work out this issue.

    I saw above comments and follow some instruction also but it doesnt work out for me.
    i used this solution also
    35 : </cfscript>
    36 :
    37 : <cfcontent type="image/jpeg" variable="#binary#">

    Please help me…

    vivek

  • Jason Sheedy | March 28, 2006

    Hi Mark, looks interesting. I’ll give it a go. cheers

  • John Igham | April 7, 2006

    I was trying to get the cfscript, below, to be more reactive to the length of the words I am using but I am not able to get it working, the error is:

    Unable to find a constructor for class java.awt.Dimension that accepts parameters of type ( java.lang.Double, java.lang.String ).

    The code is here:

    <cfset c_word_list = "Pir8te,Hoists,Ballest,Flogger,Captain, Planker,Leeward">
    <cfset c_word_day = DayOfWeek(now())>
    <cfset captcha_word = ListGetAt(c_word_list, c_word_day)>
    <cfset c_word_length = len(captcha_word)>
    <cfset x = c_word_length * 35>

    <!— <cfabort> —>

    <cfscript>
    //create the captcha cfc (could be singleton)
    captcha = createObject("component", "captcha").init();

    //push to a file
    captcha.captchaToFile(expandPath("captcha.jpg"),
    ‘#captcha_word#’,
    #x#,
    50,
    30,
    10,
    35,
    45,
    60);
    </cfscript>

    If replace x with 350 all is well but ai do not have control over the width.

    I am sure it is some thing simple I am missing because I was so easily able to implement my word var list.

    Any leads would be appreciated…

    John

  • Mark | April 10, 2006

    John,

    Sorry for the late reply.

    Looks like you will need to run a JavaCast("int", x) to make sure it comes through the right type. This should be something I should do inline within my CFC, but obviously missed. I’ll keep this in mind for the next version.

  • Peter J. Farrell | April 12, 2006

    Don’t mean to be spamming your blog Mark, but I thought I would mention that I release an open source CAPTCHA called LylaCAPTCHA today. Available at lyla dot maestropublishing dot com or you can click my name above and it should take you there.

    Thanks Mark for letting me base my Captcha on yours. Your blessing meant a lot.

  • otnateos | July 13, 2006

    great work, combining Java & CF. I like the simplicity. cheers

  • Coulouvrat | November 23, 2006

    how can I define a fixed font instead of a random in the following line?
    >>setNewFont(graphics, allFonts, size, arguments.shearXRange, arguments.shearYRange)

  • Mark | November 23, 2006

    You could simply remove the calls to setFont, and set the font you want to the graphic before all the calls are made.

    If you want something more configurable, you should probably look at lylaCaptcha

  • steve | January 3, 2007

    6.1:
    <cfcontent type="image/jpeg">
    <cfoutput>#toString(binary)#</cfoutput>

  • Farrah | January 12, 2007

    Hi,
    I am at point where I am ready to pull my hairs out. I am using version 6.1 and ofcourse the 2nd image doesn’t get displayed.
    I tried this from Steve’s last comment:
    <cfcontent type="image/jpeg">
    <cfoutput>#toString(binary)#</cfoutput>

    But this doesnt work either. Please can anyone help me with this??? Anyone has implement is successfully with 6.1??

    I appreciate any help.

  • vijay | February 3, 2007

    Good one

  • Rodney L. Bickham | March 8, 2007

    How do you apply this cfc, its not clear from the contents of the zip file, a step by step for newbies would be nice.

  • Mark | March 8, 2007

    Rodney – I would suggest looking at lylaCaptcha. You can get the URL from the comments in this blog post, or by google.

  • Fabian | May 7, 2007

    Thanks a bunch for the CFC.
    You saved my Ass :-)

  • Joel | May 30, 2007

    Very Nice.

  • Joel | May 30, 2007

    But, your implementation on this page has an easy work around.

  • Haseeb Khan | June 7, 2007

    Hi All,

    I am also having issues with configuring CAPTCHA CFC on ColdFusion 6. I also tried LylaCaptcha but no luck as well. No image is displayed at all.

    I am a beginner in ColdFusion. The LylaCaptcha also don’t have a step-by-step guide for beginners like me.

    It would be great if some step-by-step guide is provided for newbies like me so that we can benefit from this wonderful component as well.

    Any help would be highly appreciated.

    Thanks and Regards,

    Haseeb Khan

  • Farrah Omar | June 7, 2007

    Haseeb,

    I had the same problem with ColdFusion and I was never able to make it work. But I used this instead and it worked just fine: http://www.opensourcecf.com/imagecfc/.
    Let me know if you need any help with this.

  • Farrah Omar | June 7, 2007

    * I meant same problem with ColdFusion 6.

  • Haseeb Khan | June 7, 2007

    Hi Farrah,

    Thanks a LOT for your input. I have downloaded ImageCFC but I don’t know how to make it work.

    Is it possible that you can point out a step-by-step guide for configuring CAPTCHA using ImageCFC. I am very new to ColdFusion.

    What I know is that CAPTCHA string should be unique everytime the user refreshes a page, but in the example of CAPTCHA at ImageCFC shows the same image even if the screen is refreshed. So, I am confused about its functionality.

    My GTalk is mhaseebkhan@gmail.com. Can you please add me on your GTalk or send me your E-Mail on the above E-Mail address so if possible we can have a chat over the IM. I will be obliged.

    Thanks and Regards,

    Haseeb Khan

  • kurt schroeder | June 23, 2007

    I really owe you thanks for posting this system. i may like to create the same type of system for dot.net 2.0. However, i’d need to borrow the logic from your code to do so. I need the same type of symple functionality in a server control/w a perhaps a captcha class (dot.net)
    Thanks!!!
    KES

  • Joel | August 7, 2007

    I am having difficulty with the captcha image in IE 6.0. For example, when a user incorrectly enters the code, they are re-presented with the form they just filled out. However, in IE 6.0, the captcha image does not change, even though the hidden field I am testing it against does. Any idea on why this is happening? It works as expected in Firefox.

    Thanks for this extremely helpful code. You have saved me from a lot of spam!

  • Mark | August 8, 2007

    @Joel – sounds like IE is caching the image, try putting a random number string at the end after a ?, should solve the problem.

  • Joel | August 10, 2007

    Mark–

    Thanks for the suggestion. It works brilliantly!