C 语言的 GD 验证码生成器

发布于 2024-09-13 10:28:59 字数 171 浏览 4 评论 0原文

我正在用 C 语言为我的网站编写一个 fastcgi 应用程序。

如何使用 GD 生成验证码图像?

我在谷歌上徒劳地搜索了一些东西(搜索仍在进行中),但如果有人能给我有关该过程的基本想法,那就太好了。

对于随机数,我将使用纳秒作为种子(或使用它本身)。

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2024-09-20 10:28:59

查看 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);

http://www.libgd.org/Font

噪声是通过随机像素/线条/等完成的,这很简单:

  /*... 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 有无数的例子在他们的网站上。

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.

音盲 2024-09-20 10:28:59

我不确定 GD 是什么,但我假设它是某种图像库,但我可以给你一个关于实现整个验证码的想法:

  1. 你添加一个 标签链接到您的 cgi 应用程序并发送“种子”参数。请注意,由于第二步,您从 php 代码中编写了种子。
  2. 您在表单中添加一个隐藏字段来保存步骤 1 中的种子。它必须相同。
  3. 在表单中链接到的 php 文件中,您有一个函数可以从种子生成验证码。 这会在您的 C 文件中复制!
  4. 在您的 cgi 文件中,您使用上面生成的验证码并在图像上绘制数字,应用一些随机转换(小东西,例如像素移动 2-3 个像素,绘制线条等)然后返回图像数据。
  5. 在 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文