create random curve lines in an image for captcha

浪子不回头ぞ 提交于 2020-01-14 19:35:21

问题


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

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.


回答1:


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.

Here are 4 generated images:

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

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



来源:https://stackoverflow.com/questions/24860288/create-random-curve-lines-in-an-image-for-captcha

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!