我正在为我的网站编写一个fastcgi应用程序。
如何使用GD生成验证码图像?
我在google上搜索了一些东西是徒劳的(仍在进行搜索),但是如果有人能给我关于这个程序的基本想法那就太好了。
对于随机数,我将使用纳秒作为种子(或使用它本身)。
提前致谢。
答案 0 :(得分:2)
查看void gdImageStringUp(gdImagePtr im, gdFontPtr font, int x, int y, unsigned char *s, int color) (FUNCTION)
代码示例(你几乎可以复制粘贴它)..
#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);
噪声由随机像素/线/等完成,这是微不足道的:
/*... 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);
LibGD在他们的网站上有很多例子。
答案 1 :(得分:1)
我不确定GD是什么,但我认为它是某种图像库,但我可以给你一个关于实现整个验证码的想法:
<img>
标记,该标记链接到您的cgi应用并发送“种子”参数。你可以从php代码中编写种子,因为第二步。对于来自种子的验证码生成器,这样的东西就足够了:
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;
}