KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Generate CAPTCHA text in SVG
PRODUCT: 4D | VERSION: 14.0 | PLATFORM: Mac & Win
Published On: June 12, 2014

A way to determine human and computer bot input is by using CAPTCHA (Acronyn for Completely Automated Public Turing Test to Tell Computers and Humans Apart). This protects against websites where computer bots attempt to infiltrate/hack. They are distorted text which humans can read while computers can't. Here are some samples below:




// -----------------------------------------------------------------
// Name: GENERATE_SVG_CAPTCHA
// Description: Generates a captcha SVG picture and
// outputs a text for verfication.
//
// Input Parameters:
// $1 (TEXT) - Text
//
// Output:
// $0 (PICTURE) - SVG CAPTCHA picture generated from the Text input
// ------------------------------------------------------------------
C_TEXT($input_text;$1)
C_LONGINT($i;$j;$k;$len_text)
C_PICTURE($0)
ARRAY TEXT(asFont;0)

If (Count parameters>0)

   $input_text:=$1 // input text
   $len_text:=((Length($input_text)/2)*50)+25
   vID_SVG:=SVG_New ($len_text;(Length($input_text)*15)) // Drawing SVG space
   SVG_SET_OPACITY (vID_SVG;100;0) //Set the background to white
   FONT LIST(asFont) // loads all the fonts in the system

   $j:=0
   $k:=20

   For ($i;1;Length($input_text))
      $textID:=SVG_New_text (vID_SVG;$input_text[[$i]];$j;$k;asFont{(Random%(Size of array(asFont)))+1};(Random%20)+13;-1;-1;"";-1*(Random%20)+0)
      $j:=$j+25
      $k:=$k+10
   End for

   SVG EXPORT TO PICTURE(vID_SVG;$0;Copy XML data source)
End if



An example of the method call:

C_TEXT($input)
C_PICTURE($svg_captcha)

$input:="WtZQbxr6"

$svg_captcha:=GENERATE_SVG_CAPTCHA ($input)


Sample CAPTCHA picture from the method call (Note: text size, position, and font will change on every call):



See Also: