Version 8 of tclhttpd 2ch-forums

Updated 2006-04-10 14:34:41

Tips for creating NiChannel[L1 ] style forum software implemented in TclHttpd.


Generating captcha images (http://www.captcha.net ) for anti-spam purposes.

Using ImageMagick convert routine, where the captcha code is RKS876:

   convert captcha.png -gravity center -pointsize 24 -fill white -annotate 0 'RKS876' -swirl 90 captcha-done.png

Using a random sequence of numbers and letters for the captcha code.

   proc randomCode {size} {
      set data [list 1 2 3 4 5 6 7 8 9 0 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z]
      set code [list]
      for {set n 0} {$n < $size} {incr n} {
        lappend code [lindex $data [expr {int(rand()*[llength $data])}]]
      }
      return [join $code ""]
    }

Generating the captcha image:

  proc genCaptcha {code} {
    set target captcha-$code-[clock seconds].png
    set key $code
    set stamp [clock seconds]
    exec convert captcha.png -gravity center -pointsize 24 -fill white -annotate 0 '$code' -swirl 90 png:$target
    mk::row append db.captchas key $key stamp $stamp
    return $target
  }

I could store the image inside of a metakit database, but I'm uncertain how to use that in a tml page.

  mk::file open db data.dat
  mk::view layout db.captchas {key stamp data:B}

  mk::row append db.captchas key $key stamp $stamp data [exec convert captcha.png -gravity center -pointsize 24 -fill white -annotate 0 '$code' -swirl 90 png:-]

CJL added: The source image ('captcha.png') doesn't need to exist as a file, e.g.:

  exec convert -size 120x60 xc:black -gravity center -pointsize 24 -fill white -annotate 0 '$code' -swirl 90 png:$target