<?php public function captcha(){ $font_dir = $_SERVER ["DOCUMENT_ROOT"] . "your_ttf_file.ttf"; // 字型庫 $img_w = 58; // 設定圖片寬 $img_h = 20; // 設定圖片高 $font_size = 11; // 字型大小 $angle_l = -10; // 左偏角 $angle_r = 10; // 右偏角 $code_str = "ABCDEFGHJKLMNPQRSTUVWXYZ36"; $word_len = 4; // 驗證碼位元 $padding = 5; // 每兩個文字之間間隔 $margin = 2; // 左側邊距 $base_line = 15; // 文字基準位置 $base_line_offset = 2; // 基準線位移量 $pixel_num = 3; // 雜點數目基數 $pixel_color= 8; // 雜點只有 $pixel_color 種顏色 總的雜點數為$pixel_num*$pixel_color $noise_font_size = 1; // 雜點字型大小 $session_key= "my.xoyo_captcha"; //自訂session鍵名 header("Cache-Control: no-cache, must-revalidate"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Pragma: no-cache"); header("Cache-control: private"); header('Content-Type: image/png'); session_start(); $word = ""; $code_str_len = strlen($code_str) - 1; for ($i = 0; $i < $word_len; $i++) { $word.= $code_str[rand(0, $code_str_len)]; } $_SESSION [$session_key] = strtolower($word); $image = imagecreatetruecolor($img_w, $img_h); imagefilledrectangle($image, 0, 0, $img_w - 1, $img_h - 1, imagecolorallocate($image, mt_rand(235, 255), mt_rand(235, 255), mt_rand(235, 255))); //繪製雜點 for($i = 0; $i < $pixel_color; $i++){ $noise_color = imagecolorallocate( $image, mt_rand(150,225), mt_rand(150,225), mt_rand(150,225) ); for($j = 0; $j < $pixel_num; $j++) { imagestring( $image, $noise_font_size, mt_rand(-10, $img_w), mt_rand(-10, $img_h), $code_str[mt_rand(0, $code_str_len)], $noise_color ); } } //繪製文字 for ($i = 0; $i < $word_len; ++$i) { $color = imagecolorallocate($image, mt_rand(0, 100), mt_rand(20, 120), mt_rand(50, 150)); imagettftext($image, $font_size, mt_rand($angle_l, $angle_r), $margin, mt_rand($base_line-$base_line_offset, $base_line+$base_line_offset), $color, $font, mb_substr($word, $i, 1, 'utf-8')); $margin += (imagefontwidth($font_size) + $padding); } imagepng($image); imagedestroy($image); exit; } |