標籤:寬高 pre locate des 填充 div 畫像 ++ code
1 <?php 2 3 header("content-type:image/jpeg"); 4 //驗證碼函數 5 function getVerify($type = 4){ 6 7 //字碼錶 8 $chars = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"; 9 //字串的長度10 $len = strlen($chars);11 //預定義code12 $verify = ‘‘;13 14 //產生四位隨機數 ,從字碼錶當中15 for ($i=1; $i <= $type ; $i++) { 16 $one_index = mt_rand(0 ,$len-1);17 18 $verify.= $chars[$one_index];19 }20 21 //建立畫布22 $width = 500;23 $height = 300;24 25 $image = imagecreatetruecolor($width, $height);26 27 //配色28 $color = "";29 30 //隨機顏色31 $color = imagecolorallocate($image,210,105,30); 32 33 34 //填充35 imagefill($image, 0, 0, $color);36 37 38 39 $font = 5;40 //獲得映像的寬高41 $img_w = imagesx($image);42 $img_h = imagesy($image);43 44 //獲得字型的寬高45 $font_w = imagefontwidth($font);46 $font_h = imagefontheight($font);47 48 //獲得字串的寬高49 $str_w = $font_w*$type;50 $str_h = $font_h;51 52 //置中顯示53 $x = ($img_w - $str_w)/2;54 $y = ($img_h - $str_h)/2;55 $color = imagecolorallocate($image, 128,0,0);56 imagestring($image, $font, $x, $y, $verify, $color);57 58 //輸出59 imagejpeg($image);60 61 //銷毀畫像資源62 imagedestroy($image);63 }64 65 getVerify(10);
PHP之驗證碼