標籤:height create nop fill fonts type ima png images
<?php /** * php產生驗證碼 * @param $width 畫布寬 * @param $height 畫布高 * @param $vcodelen 驗證碼長度 * @param $pointnum 幹擾像素點數量 * @param $linenum 幹擾線條數量 * * 思路:建立驗證碼畫布,產生並填充背景色,產生驗證碼內容/幹擾像素點/線,填充到畫布,輸出。 */ $width = 100; $height = 30; $vcodelen = 4; $pointnum = 200; $linenum = 3; // 建立畫布 $image = imagecreatetruecolor($width, $height); // 建立色塊 $bgcolor = imagecolorallocate($image, 255, 255, 255); // 填充畫布背景色 imagefill($image, 0, 0, $bgcolor); // 驗證碼內容 for ($i=0; $i < $vcodelen; $i++) { // 字型大小 $fontsize = 5; // 字型顏色,顏色在限定範圍內隨機 $fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120)); $data = ‘abcdefghijklmnopqrstuvwxyz0123456789‘; // 驗證碼內容在以上字串內隨機截取 $fontcontent = substr($data, rand(0,strlen($data)),1); // 字串顯示位置 $x = ($i*$width/4)+rand(5,15); $y = rand(5,10); // 字串填充圖片 // imagestring的字型大小可選1-5,字型再大需要用imagettftext函數(需要字型檔) imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); // imagettftext($image, $fontsize, 0, $x, $y, $fontcolor, ‘/font/Geneva.dfont‘, $fontcontent); } // 幹擾像素點 for ($i=0; $i < $pointnum; $i++) { $pointcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120)); // 畫布填充像素點函數 imagesetpixel($image, rand(0,$width), rand(0,$height), $pointcolor); } // 幹擾線條 for ($i=0; $i < $linenum; $i++) { $linecolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120)); // 畫布填充線條函數 imageline($image, rand(0,$width), rand(0,$height), rand(0,$width), rand(0,$height), $linecolor); } // 圖片輸出格式 header(‘content-type: image/png‘); // 輸出驗證碼圖片 imagepng($image); // 銷毀畫布 imagedestroy($image);?>
PHP產生驗證碼