Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create random curve lines in an image for captcha

Tags:

php

captcha

gd

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

enter image description here

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.

like image 679
user3452098 Avatar asked Oct 12 '25 18:10

user3452098


1 Answers

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:

enter image description here enter image description here enter image description here enter image description here

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.

like image 59
timclutton Avatar answered Oct 14 '25 08:10

timclutton