tags:

views:

46

answers:

2

I'm writing a fastcgi app for my website in C.

How do I use GD to generate a CAPTCHA image?

I searched for some stuff on google in vain (still search is going on), but it would be nice if someone could give me the basic idea about the procedure.

For random numbers, I will use nanoseconds as seed (or use that itself).

Thanks in advance.

+2  A: 

Look at the void gdImageStringUp(gdImagePtr im, gdFontPtr font, int x, int y, unsigned char *s, int color) (FUNCTION) code example (you can pretty much copy-paste it)..

  #include "gd.h"
  #include "gdfontl.h"
  #include <string.h>

  /*... inside a function ...*/
  gdImagePtr im;
  int black;
  int white;
  /* String to draw. */
  char *s = "Hello.";
  im = gdImageCreate(100, 100);
  /* Background color (first allocated) */
  black = gdImageColorAllocate(im, 0, 0, 0);
  /* Allocate the color white (red, green and blue all maximum). */
  white = gdImageColorAllocate(im, 255, 255, 255);
  /* Draw a centered string going upwards. Axes are reversed,
  and Y axis is decreasing as the string is drawn. */
  gdImageStringUp(im, gdFontGetLarge(),
  im->w / 2 - gdFontGetLarge()->h / 2,
  im->h / 2 + (strlen(s) * gdFontGetLarge()->w / 2), s, white);
  /* ... Do something with the image, such as
  saving it to a file... */
  /* Destroy it */
  gdImageDestroy(im);

http://www.libgd.org/Font

The noise is done with random pixels/lines/etc, which is trivial:

  /*... inside a function ...*/
  gdImagePtr im;
  int black;
  int white;
  im = gdImageCreate(100, 100);
  /* Background color (first allocated) */
  black = gdImageColorAllocate(im, 0, 0, 0);
  /* Allocate the color white (red, green and blue all maximum). */
  white = gdImageColorAllocate(im, 255, 255, 255);
  /* Set a pixel near the center. */
  gdImageSetPixel(im, 50, 50, white);
  /* ... Do something with the image, such as
  saving it to a file... */
  /* Destroy it */
  gdImageDestroy(im);

http://www.libgd.org/Drawing

LibGD has like a zillion examples on their website.

David Titarenco
+1  A: 

I'm not sure what GD is but I'm assuming it's some sort of an image library, but I can give you an idea about implementing the whole captcha thing:

  1. you add an <img> tag that links to your cgi app and sends a "seed" parameter. You write the seed from the php code mind you, because of the 2nd step.
  2. you add a hidden field in your form that holds the seed from step 1. It has to be the same.
  3. in the php file you link to in your form you have a function that generates a captcha number from the seed. This is replicated in your C file!
  4. in your cgi file you use the captcha generated above and draw the numbers on the image, apply some random transformation (minor stuff, like pixels moved 2-3 pixels around, drawing lines over, etc) then return the image data.
  5. in the php file your form redirects to you regenerate the values from the hidden field that holds the seed and tests against what the user enters.

As for a captcha generator from a seed, something like this should suffice:

static int seed; // write to this when you get the value
int nextDigit()
{
  seed=seed*32423+235235;
  seed^=0xc3421d;
  return seed%10; // %26 if you want letters, %36 if you want letters+numbers
}

int captcha()
{
  return nextDigit()+nextDigit()*10+nextDigit()*100+
    nextDigit()*100+nextDigit()*10;
}
Blindy
Most probably I'll be using numeric captcha, but your code is worth a try. :) Thanks.
Nilesh