generateCode(); $background_color = imagecolorallocate($image, 0, 0, 0); $foreground_color = imagecolorallocate($image, 255, 255, 255); header('Content-Type: image/jpeg, Expires: Thu, 1 Jan 1990 00:00:00 GMT'); imagettftext($image, $fontSize, 0, 2, 25, $foreground_color, "font1.ttf", $code); imagettftext($image, 6, 0, 2, 8, $foreground_color, "font.ttf", 'Error: '.$errorText); imagejpeg($image, null, 75); imagedestroy($image); die(); } function oldGenerateCode($characters) { /* list all possible characters, similar looking characters and vowels have been removed */ $possible = '2345678bcdfghjkmnprsvwyz'; $code = ''; $i = 0; while ($i < $characters) { $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1); $i++; } return $code; } function generateCode($characters = 10) { //Open up the wordlist file if (!$wordlist = @file_get_contents("wordlist.txt")) { //No wordlist file present return $this->oldGenerateCode($characters); } $wordlist = explode("\n", $wordlist); $validWord = false; $i = 0; while($validWord == false) { $word = $wordlist[array_rand($wordlist)]; $word = trim($word); $i++; if(substr($word, 0, 1) == "#") $validWord = false; if((strlen($word) != $characters) && $characters != 0) $validWord = false; if($i > 200) $validWord = true; } $possible = 'bcdfhkmnprsvwyz'; //$word .= substr($possible, mt_rand(0, strlen($possible)-1), 1); return strtoupper($word); } function generateImage($width='320',$height='75',$characters='10') { $code = $this->generateCode($characters); /* font size will be 60% of the image height */ $font_size = $height * 0.60; $image = imagecreatetruecolor($width, $height) or $this->error('Cannot initialize new GD image stream'); imageantialias ($image, true) or $this->error('Cannot turn on anti-ailasing'); //Generate some random colors! $c1 = mt_rand(60, 90); $c2 = mt_rand(60, 90); $c3 = mt_rand(60, 90); /* set the colours */ $background_1_color = array($c1+60, $c2+60, $c3+60); $background_2_color = array($c1+160, $c2+160, $c3+160); $background_color = imagecolorallocate($image, $c1+150, $c2+150, $c3+150); $text_color = imagecolorallocate($image, $c1+40, $c2+40, $c3+40); $noise_color = imagecolorallocate($image, $c1+40, $c2+40, $c3+40); $highlight_color = imagecolorallocate($image, 255, 0, 0); //Draw a gradient background fill imagefilledrectangle($image, 0, 0, $width, $height, $background_color); for($i=1; $i<=$width; $i++) { $linecolors[0] = ($background_1_color[0]+(($background_2_color[0] - $background_1_color[0]) / $width * $i)); $linecolors[1] = ($background_1_color[1]+(($background_2_color[1] - $background_1_color[1]) / $width * $i)); $linecolors[2] = ($background_1_color[2]+(($background_2_color[2] - $background_1_color[2]) / $width * $i)); $linecolor = imagecolorallocate($image, $linecolors[0], $linecolors[1], $linecolors[2]); imageline($image, $i, 0, $i, $height, $linecolor); imagecolordeallocate($image, $linecolor); } //Long thick lines for($w=0; $w<2; $w++) { $w1 = mt_rand(10,20) + ($w - 1)*50; $w2 = mt_rand($width/1.3,$width)+20; $h1 = mt_rand(10, $height/2-10) + $w*$height/2; $h2 = mt_rand(0, $height) + (1-$w)*$height/2; for( $i=0; $i<30; $i++) { $j = $i/2; imageline($image, $w1+$j, $h1, $w2+$j, $h2, $text_color) or $this->error("Error creating think line."); } } //Small thick lines for($w=0; $w<3; $w++) { $w1 = mt_rand(0, $width); $w2 = $w1 + rand(-30, 30); $h1 = mt_rand(0, $height); $h2 = $h1 + rand(-30, 30); for( $i=0; $i<10; $i++) { $j = $i/2; imageline($image, $w1+$j, $h1, $w2+$j, $h2, $text_color) or $this->error("Error creating think line."); } } /* create textbox and add text */ $textbox = @imagettfbbox($font_size, 0, "fonta.ttf", $code) or $this->error('Error in imagettfbbox function'); $y = ($height - $textbox[5])/2.3; $x = -10; $i = 0; $xoffset = 0; foreach (str_split($code) as $key => $value) { //This bit of code draws the letters onto the image at random angles and at distances //so that the letters are smothered together (harder for computers to read) $letterfont = "font.ttf"; $prevangle = $angle; $angle = $angle + rand(-30, 30); if($angle < -30) $angle = -20 - rand(-5, 5); if($angle > 30) $angle = 20 - rand(-5, 5); $xoffset += 18*($font_size/35) - ($prevangle - $angle) / 3 + 2; for($j = 0; $j<4; $j++) { @imagettftext($image, $font_size, $angle, $x + $xoffset + $j, $y, $text_color, $letterfont, $value) or $this->error('Error in imagettftext function'); } $i++; } //die(); //Make sure to have the browser not cache the image header('Content-Type: image/jpeg, Expires: Thu, 1 Jan 1990 00:00:00 GMT'); imagejpeg($image, null, 75); imagedestroy($image); $_SESSION['captcha'] = $code; } }