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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With