轉載路徑:http://hi.baidu.com/asdasd_cn/item/62977d1e26ca36e85e53b142
生成驗證碼圖片的兩個函數
第一個是用imagestring
? bool imagestring ? ?( resource $image ? , int $font ? , int $x ? , int $y ? , string $s ? , int $col ? )
? imagestring() 用 col ? 顏色將字符串 s 畫到 image ? 所代表的圖像的 x,y ? 坐標處(這是字符串左上角坐標,整幅圖像的左上角為 0,0)。如果 ? font 是 1,2,3,4 或 5,則使用內置字體。 ?
public function captcha1() {//生成驗證碼圖片Header("Content-type: image/PNG");$rand ="";for($i=0;$i<4;$i++){$rand.=dechex(rand(0,15));}$im = imagecreate(100,50); //制定圖片背景大小$black = ImageColorAllocate($im, 0,0,0); //設定三種顏色$white = ImageColorAllocate($im, 255,255,255);$gray = ImageColorAllocate($im, 200,200,200);for($i=0;$i<3;$i++){$te=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));}imagefill($im,0,0,$gray); //采用區域填充法,設定(0,0)while(($rand=rand()%100000)<10000);//將四位整數驗證碼繪入圖片$_SESSION['rand']=$rand;imagestring($im, 5, 30, 20, $rand, $te);// 用 col 顏色將字符串 s 畫到 image 所代表的圖像的 x,y 座標處(圖像的左上角為 0, 0)。//如果 font 是 1,2,3,4 或 5,則使用內置字體for($i=0;$i<200;$i++) //加入干擾象素{$randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));imagesetpixel($im, rand()%100 , rand()%50 , $randcolor);}imageline($im,0,rand(0,100),100,rand(0,15),$randcolor);imageline($im,0,rand(0,100),100,rand(0,15),$randcolor);imageline($im,0,rand(0,100),100,rand(0,15),$randcolor);ImagePNG($im);ImageDestroy($im);}
第二個函數可根據根據字體庫設置大小Linux端的注意路徑
imagefttext — 使用 FreeType 2 字體將文本寫入圖像
? array imagefttext ? ?( resource $image ? , float $size ? , float $angle ? , int $x ? , int $y ? , int $color ? , string $fontfile ? , string $text ? [, array $extrainfo ?] )
//可以修改字體大小的驗證碼function captcha2($num=4,$size=20, $width=0,$height=0){ !$width && $width = $num*$size*4/5+5; !$height && $height = $size + 10; // 去掉了 0 1 O l 等 $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW"; $code = ''; for ($i=0; $i<$num; $i++){ $code.= $str[mt_rand(0, strlen($str)-1)]; } // 畫圖像 $im = imagecreatetruecolor($width,$height); // 定義要用到的顏色 $back_color = imagecolorallocate($im, 235, 236, 237); $boer_color = imagecolorallocate($im, 118, 151, 199); $text_color = imagecolorallocate($im, mt_rand(0,200), mt_rand(0,120), mt_rand(0,120)); // 畫背景 imagefilledrectangle($im,0,0,$width,$height,$back_color); // 畫邊框 imagerectangle($im,0,0,$width-1,$height-1,$boer_color); // 畫干擾線 for($i=0;$i<5;$i++){ $font_color = imagecolorallocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); imagearc($im,mt_rand(-$width,$width),mt_rand(-$height,$height),mt_rand(30,$width*2),mt_rand(20,$height*2),mt_rand(0,360),mt_rand(0,360),$font_color); } // 畫干擾點 for($i=0;$i<50;$i++){ $font_color = imagecolorallocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$font_color); } // 畫驗證碼 @imagefttext($im, $size , 0, 5, $size+3, $text_color, 'c://WINDOWS//Fonts//simsun.ttc',$code); header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate"); header("Content-type: image/png"); imagepng($im);imagedestroy($im);}
附:Linux-debian 字體默認路徑:/usr/share/fonts/truetype/ttf-dejavu/
考慮到程序的可移植性,在程序目錄中新建字體目錄fonts來使用。