Witam, napisałem sobie system captcha, oto klasa:
class captcha{
public $width;
public $height;
public $characters;
public $lines;
function __construct($width, $height, $characters, $lines){
$this -> width = $width;
$this -> height = $height;
$this -> characters = $characters;
$this -> lines = $lines;
}
function drawCaptcha(){
// set up margin as 10% of image height
$margin = ceil($this -> height * 0
.1
);
// randomize background RGB color
// font color
// lines color
// create image
$image = imagecreatetruecolor($this -> width, $this -> height);
// colors
$backgroundColor = imagecolorallocate($image, $backgroundR, $backgroundG, $backgroundB);
$fontColor = imagecolorallocate($image, $fontR, $fontG, $fontB);
$linesColor = imagecolorallocate($image, $linesR, $linesG, $linesB);
imagefill($image, 0, 0, $backgroundColor);
// draw lines on background
for ($i=0; $i<$this -> lines; $i++) {
imageline
($image, rand(0
, $this -> width), 0
, rand(0
, $this -> width), $this -> height, $linesColor); imageline
($image, 0
, rand(0
, $this -> height), $this -> width, rand(0
, $this -> height), $linesColor); }
$font = '../font/Delicious-Roman.otf';
$charTable = '0123456789abcdefghijklmnoprstuwyzABCDEFGHIJKLMNOPRSTUWYZ';
// code generation
for ($i=1; $i<=$this -> characters; $i++) {
}
$captchaCode = implode($character); $_SESSION['captchaCode'] = $captchaCode;
// count max font size
$fontSizeMax = floor($this -> height - $margin * 2
);
do {
$fontSizeMax--;
$captchaWidth = 0;
for ($i=0; $i<sizeof($character); $i++) {
$bbox = imageftbbox($fontSizeMax, $angle[$i], $font, $character[$i]);
$captchaWidth += $charWidth = max(abs($bbox[0
])+abs($bbox[2
]), abs($bbox[4
])+abs($bbox[6
]))+$margin; $charHeight[] = max(abs($bbox[1
])+abs($bbox[3
]), abs($bbox[5
])+abs($bbox[7
]));
}
} while (max($charHeight) >= $this -> height OR
$captchaWidth >= $this -> width - 2
*$margin);
// draw text to image
$x = ($this -> width - $captchaWidth) / 2;
$fontSpacing=$margin;
for ($i=0; $i<sizeof($character); $i++) {
$bbox = imageftbbox($fontSizeMax, $angle[$i], $font, $character[$i]);
$charWidth = max(abs($bbox[0
])+abs($bbox[2
]), abs($bbox[4
])+abs($bbox[6
]))+$margin; imagettftext($image, $fontSizeMax, $angle[$i], $x+$fontSpacing, $this -> height/2+$margin, $fontColor, $font, $character[$i]);
$fontSpacing += $charWidth;
}
Header('Content-type: image/png'); imagepng($image);
}
}
Plik index wygląda tak:
echo '<img src="img/captcha.php"/>'; echo $_SESSION['captchaCode'];
oraz plik obrazka:
function __autoload($class){
require_once('../class/'.$class.'.class.php');
}
$captcha = new captcha(150, 50, 6, 10);
$captcha -> drawCaptcha();
No i po wyświetleniu pliku index.php generuje mi elegancko obrazek, natomiast $_SESSION['captchaCode'] na początku zwraca notice, że zmienna jest pusta, a po odświeżeniu jest nowo wygenerowany obrazek a $_SESSION zawiera kod poprzedniego, jak to przekazać, nie musi być w sesji, bym miał jednocześnie kod w postaci stringa i obrazek z tym samym kodem...
Ten post edytował b4rt3kk 17.07.2012, 00:50:19