I have made a simple captcha class today, it's already functional but I'm thinking of some clever way to add some random geometrical shapes in order to create a noise.
Should I just flood the captcha with some random lines?
Also, I suppose the captcha text should be stored in the session, am I right?
Here is what I have so far:
Just copy and paste the code to test it, if interested.PHP Code:<?php
/*
@desc Creates captcha verification image
@author Richard Knop
*/
class Captcha
{
private $length;
private $string = array();
private $image;
public function generateString($length, $base = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM')
{
$this->length = $length;
for ($i = 0, $max = strlen($base) - 1; $i < $length; $i++)
{
$this->string[] = $base[rand(0,$max)];
}
}
public function draw($fontSize)
{
header("Content-type: image/png");
$width = $fontSize * 2 * $this->length;
$height = 3 * $fontSize;
$this->image = imagecreatetruecolor($width, $height);
$r = rand(0, 127);
$g = rand(0, 127);
$b = rand(0, 127);
$bgColor = imagecolorallocate($this->image, $r, $g, $b);
imagefill($this->image, 0, 0, $bgColor);
$font = 'arial.ttf';
$partialWidth = $width / $this->length;
for ($i = 0; $i < $this->length; $i++)
{
$angle = rand(-45, 45);
$bbox = imagettfbbox($fontSize, $angle, $font, $this->string[$i]);
$w = $bbox[2] - $bbox[0];
$h = $bbox[1] - $bbox[7];
$x = $i * $partialWidth + ($partialWidth - $w) / 2;
$y = $height - ($height - $h) / 2;
$color = imagecolorallocate($this->image, rand(192, 255), rand(192, 255), rand(192, 255));
imagettftext($this->image, $fontSize, $angle, $x, $y, $color, $font, $this->string[$i]);
}
$lineColor = imagecolorallocate($this->image, 255 - $r, 255 - $g, 255 - $b);
for ($increment = $height / 20, $i = $increment; $i < $height; $i += $increment)
{
imageline($this->image, 0, $i, $width, $i, $lineColor);
}
imagepng($this->image);
imagedestroy($this->image);
}
public function getString()
{
return implode('', $this->string);
}
}
$captcha = new Captcha();
$captcha->generateString(5);
$captcha->draw(20);




Bookmarks