- <?php
- /**
- * php生成<a href="http://www.49028c.com/zhuanti/yanzhengma/" class="anchor" target="_blank">驗證碼</a>
- * @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++) {
- // <a href="/tags.php/%D7%D6%CC%E5%B4%F3%D0%A1/" target="_blank">字體大小</a>
- $fontsize= 5;
- // 字體顏色,顏色在限定范圍內隨機
- $fontcolor= imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
- $data='abcdefghijklmnopqrstuvwxyz0123456789'
- // 驗證碼內容在以上字符串內隨機截取
- $fontcontent=<a href="/tags.php/substr/" target="_blank">substr</a>($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);
- ?>
新聞熱點
疑難解答