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 // 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++) { } $font = '../font/Delicious-Roman.otf'; $charTable = '0123456789abcdefghijklmnoprstuwyzABCDEFGHIJKLMNOPRSTUWYZ'; // code generation for ($i=1; $i<=$this -> characters; $i++) { } $_SESSION['captchaCode'] = $captchaCode; // count max font size do { $fontSizeMax--; $captchaWidth = 0; for ($i=0; $i<sizeof($character); $i++) { $bbox = imageftbbox($fontSizeMax, $angle[$i], $font, $character[$i]); } // 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]); imagettftext($image, $fontSizeMax, $angle[$i], $x+$fontSpacing, $this -> height/2+$margin, $fontColor, $font, $character[$i]); $fontSpacing += $charWidth; } imagepng($image); } }
Plik index wygląda tak:
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...