先給看下 大致的效果
那麼接下來的就直接貼代碼吧
<?php $image = imagecreatetruecolor(100, 30); //建立畫布 $imagecolor = imagecolorallocate($image, 255, 255, 255); //背景色 imagefill($image, 0, 0, $imagecolor); //填充背景色 for($i=0;$i<4;$i++ ){ //迴圈4位元 $fontsize = 6; $fontcolor = imagecolorallocate($image, rand(0, 200), rand(0, 200), rand(0, 200)); $fontcontent = rand(0, 9); $x = $i*100/4 + rand(5, 15); $y = rand(5, 10); imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); } for($i=0;$i<200;$i++ ){ //迴圈 添加幹擾點 $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200)); $x = rand(1, 99); $y = rand(1, 29); imagesetpixel($image, $x, $y, $pointcolor); } for($i=0;$i<3;$i++){ //迴圈 添加幹擾線 $linecolor = imagecolorallocate($image, rand(100, 250), rand(100, 250), rand(100, 250)); $x1 = rand(1, 25); $x2 = rand(50, 75); $y1 = rand(1, 15); $y2 = rand(15, 25); imageline($image, $x1, $y1, $x2, $y2, $linecolor); } header("content-type:image/png"); imagepng($image); imagedestroy($image);?>
再給大家分享一個可以產生中文驗證碼
<?php//1.qi啟用gd庫GD庫提供了一系列用來處理圖片的API,使用GD庫可以處理圖片,或者產生圖片。 // 在網站上GD庫通常用來產生縮圖或者用來對圖片加浮水印或者對網站資料產生報表。session_start();// 把GBK編碼的字串轉換成UTF-8字串,第一個參數之所以寫GBK,是因為本php檔案在主機中儲存的編碼是GBK編碼// UTF-8編碼瀏覽器普遍支援,通用性強,這裡就轉換成UTF-8$str = iconv("GBK", "utf-8", "芸芸眾生綠水青山名勝古迹敞開心胸便會雲蒸霞蔚快樂將永遠伴隨著你");if(!is_string($str) || !mb_check_encoding($str,"utf-8")){exit("不是字串或者不是utf-8");}$zhongwenku_size;// 按UTF-8編碼方式擷取字串的長度$zhongwenku_size = mb_strlen($str,"UTF-8");// 把上述字元匯入數組中$zhongwenku = array();for( $i=0; $i<$zhongwenku_size; $i++){$zhongwenku[$i] = mb_substr($str, $i,1,"UTF-8");}$result = "";// 圖片上要寫入的四個字元for($i=0; $i<4; $i++){switch (rand(0, 1)){case 0:$result.=$zhongwenku[rand(0, $zhongwenku_size-1)];break;case 1:$result.=dechex(rand(0,15));break;}}$_SESSION["check"] = $result;// 建立一個真彩圖片 寬100,高30$img = imagecreatetruecolor(100, 30);// 分配背景顏色$bg = imagecolorallocate($img, 0, 0, 0);// 分配文字顏色$te = imagecolorallocate($img, 255,255,255);// 在圖片上寫字串//imagestring($img, rand(3,8), rand(1,70), rand(1,10), $result, $te);// 在圖片上根據載入字型可以寫出特殊字型imagettftext($img, 13, rand(2, 9), 20 ,20, $te, "MSYH.TTF",$result);$_SESSION["check"] = $result;for($i=0; $i<3; $i++){//$t = imagecolorallocate($img, rand(0, 255),rand(0, 255),rand(0, 255));// 畫線imageline($img, 0, rand(0, 20), rand(70,100), rand(0, 20), $te);}$t = imagecolorallocate($img, rand(0, 255),rand(0, 255),rand(0, 255));// 為圖片添加噪點for($i=0; $i<200; $i++){imagesetpixel($img, rand(1, 100), rand(1, 30), $t);}// 發送http頭資訊 指定本次發送的是image中的jpegheader("Content-type: image/jpeg");// 輸出jpeg圖片至瀏覽器imagejpeg($img);?>
再來一個執行個體吧
<?php session_start();function random($len) { $srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm"; mt_srand(); $strs = ""; for ($i = 0; $i < $len; $i++) { $strs .= $srcstr[mt_rand(0, 30)]; } return $strs;} //隨機產生的字串$str = random(4); //驗證碼圖片的寬度$width = 50; //驗證碼圖片的高度$height = 25; //聲明需要建立的圖層的圖片格式@ header("Content-Type:image/png"); //建立一個圖層$im = imagecreate($width, $height); //背景色$back = imagecolorallocate($im, 0xFF, 0xFF, 0xFF); //模糊點顏色$pix = imagecolorallocate($im, 187, 230, 247); //字型色$font = imagecolorallocate($im, 41, 163, 238); //繪模糊作用的點mt_srand();for ($i = 0; $i < 1000; $i++) { imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pix);} //輸出字元imagestring($im, 5, 7, 5, $str, $font); //輸出矩形imagerectangle($im, 0, 0, $width -1, $height -1, $font); //輸出圖片imagepng($im); imagedestroy($im); $str = md5($str); //選擇 cookie//SetCookie("verification", $str, time() + 7200, "/"); //選擇 Session$_SESSION["verification"] = $str;?>
接下來只要在頁面中調用就可以了:
<img id="checkpic" onclick="changing();" src='/images/checkcode.php' />
如果想實現 "看不清?換一張" 效果,添加如下 JS 到頁面中
function changing(){ document.getElementById('checkpic').src="/images/checkcode.php?"+Math.random();}
以上所述就是本文的全部內容了,希望大家能夠喜歡。