在图像中为验证码创建随机曲线 [英] create random curve lines in an image for captcha

查看:151
本文介绍了在图像中为验证码创建随机曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个脚本,以创建类似于某些流行网站所使用的验证码的验证码图像,如下图所示。
我已经创建了生成验证码的脚本,但我想使其类似于以下内容

I want to create an script for creating captcha images similar to the captcha used by some popular websites like in the image below. I have created the script which generates captcha but I want to make it somewhat like below

我想在图片中添加这些随机线,但我无法弄清楚该如何处理实现它,请建议如何在PHP。或我可以参考的任何类似开源项目中实现它。

And I want to add those random lines in the image but I cant figure our how can I achieve it,Please suggest how to do it in PHP.or any similar open-source project I can reference to.

推荐答案

下面的代码为您提供了执行所需操作的起点。请注意,这提供了比您发布的示例图像更简单的输出。

The below code gives you a starting point to do what you want. Note that this gives a much simpler output than the example images you posted.

这里生成了4张图像:

您真正感兴趣的唯一部分是 for 循环,但这是一个完全正常的示例:

The only part you are really are interested in is the for loop, but this is a fully working example:

$im = imagecreatetruecolor(150, 75);

$bg = imagecolorallocate($im, 220, 220, 220);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

// set background colour.
imagefilledrectangle($im, 0, 0, 150, 75, $bg);

// output text.
imagettftext($im, 35, 0, 10, 55, $black, 'arial.ttf', 'ABCD');

for ($i = 0; $i < 50; $i++) {
    //imagefilledrectangle($im, $i + $i2, 5, $i + $i3, 70, $black);
    imagesetthickness($im, rand(1, 5));
    imagearc(
        $im,
        rand(1, 300), // x-coordinate of the center.
        rand(1, 300), // y-coordinate of the center.
        rand(1, 300), // The arc width.
        rand(1, 300), // The arc height.
        rand(1, 300), // The arc start angle, in degrees.
        rand(1, 300), // The arc end angle, in degrees.
        (rand(0, 1) ? $black : $white) // A color identifier.
    );
}

header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

调整 for 循环的限制,然后 rand()调用中的最大值将影响弧的密度。

Tweaking the limit of the for loop and the max value in the rand() calls will affect the 'density' of the arcs.

这篇关于在图像中为验证码创建随机曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆