運行沒有報錯,但是不顯示圖片
<?php?
/**
?* @param integer $width 寬度
?* @param? integer $[height] [高度]
?* @param? integer $[type] [1為純數(shù)字,2為大小寫字母,3為字母數(shù)字混合]
?* @param? integer $[length] [輸出的驗證碼數(shù)量]
?* @return string [驗證碼字符串]
?*/
//封裝一個驗證碼函數(shù)
function test_session($width=100,$height=30,$type=3,$length=4){
//先設定一個畫布資源/顏色并覆蓋,這是底色圖片
$image=imagecreatetruecolor($width, $height);
$white=imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, $width, $height, $white);
switch ($type) {
case 1:
//array_rand是隨機返回($length)個數(shù)組中的鍵/下標值
$str=join('',array_rand(range(0,9),$length));
break;
case 2:
//array_merge是合并數(shù)組
$str=join('',array_rand(array_flip(array_merge(range('a','z'),range('A','Z'))),$length));
break;
case 3:
//array_flip是交換數(shù)組的下標和值
$str=join('',array_rand(array_flip(array_merge(range('a','z'),range('A','Z'),range(0,9))),$length));
break;
}
//設置字體
$fize='D:\wamp64\www\project01\session.test.php\STXINGKA.TTF';
//設定一個隨機顏色數(shù)組
function colorrand($image){
return imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
}
for ($i=1; $i <$length ; $i++) {?
imagefttext($image,16, mt_rand(-30,30),($width/$length)*$i, mt_rand($height-15,+15), colorrand($image),$fize,$str);
}
//設置干擾點
for($i=1;$i<200;$i++){
imagesetpixel($image, mt_rand(0,$width), mt_rand(0,$height), colorrand($image));
}
header('Conten-Type:image/png');
imagepng($image);
imagedestroy($image);
return $str;
}
??>
以上是我敲的代碼,望指點,謝謝~!
2019-04-07
<?php
/**
?* @param integer $width 寬度
?* @param? integer $[height] [高度]
?* @param? integer $[type] [1為純數(shù)字,2為大小寫字母,3為字母數(shù)字混合]
?* @param? integer $[length] [輸出的驗證碼數(shù)量]
?* @return string [驗證碼字符串]
?*/
//封裝一個驗證碼函數(shù)
function test_session($width=100,$height=30,$type=2,$length=4,$fize='D:\wamp64\www\project01\session.test.php\STXINGKA.TTF'){
???? //先設定一個畫布資源/顏色并覆蓋,這是底色圖片
???? $image=imagecreatetruecolor($width, $height);
???? $white=imagecolorallocate($image, 255, 255, 255);
???? imagefilledrectangle($image, 0, 0, $width, $height, $white);
???? switch ($type) {
???????? case 0:
???????????? //array_rand是隨機返回($length)個數(shù)組中的鍵/下標值
???????????? $str=join('',array_rand(range(0,9),$length));
???????????? break;
???? ???? case 1:
???????????? //array_merge是合并數(shù)組
???????????? $str=join('',array_rand(array_flip(array_merge(range('a','z'),range('A','Z'))),$length));
???????????? break;
???????? case 2:
???????????? //array_flip是交換數(shù)組的下標和值
???????????? $str=join('',array_rand(array_flip(array_merge(range('a','z'),range('A','Z'),range(0,9))),$length));
???????????? break;
???? }
???? //設置字體
???? //設定一個隨機顏色
???????? //$color = imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
???????? //如果把這個顏色放在設定字體的循環(huán)外面,那么所有的調(diào)用都會是一個顏色
???? for($i=0; $i <$length ; $i++) {
???????? $color = imagecolorallocate($image,mt_rand(80,220),mt_rand(80,220),mt_rand(80,220));?
???????? imagefttext($image, 16, mt_rand(-20,20), $i*$width/$length+4,mt_rand($height-15,25),$color,$fize,$str[$i]);
???? }
???? //設置干擾點
???? //如果這個變量$color 放在了for循環(huán)外面,那么所有的點都會是一個顏色
???? for($i=1;$i<200;$i++){
???????? $color2 = imagecolorallocate($image,mt_rand(40,255),mt_rand(40,255),mt_rand(40,255));
???????? imagesetpixel($image, mt_rand(0,$width), mt_rand(0,$height), $color2);
???? }
???? header('Content-Type:image/png');
???? imagepng($image);
???? imagedestroy($image);
???? return $str;
????}
????//要查看效果記得調(diào)用一下函數(shù)
????test_session($width=100,$height=30,$type=2,$length=4,$fize='D:\wamp64\www\project01\session.test.php\STXINGKA.TTF');//但是在html的img src路徑里調(diào)用這個函數(shù)時,這個調(diào)用可以不額外寫,因為在html引用這個函數(shù)本身就是一次調(diào)用
??>
????????單獨看效果,需要額外調(diào)用一下函數(shù)
2019-04-07
自己研究了一上午,找到了幾處錯誤借鑒一下,
是拼寫錯誤,有些地方的拼寫錯了,大家仔細看一下不難發(fā)現(xiàn)
我在往圖片上寫字(用imagefttext)函數(shù)時,里面顏色選項如果是調(diào)用前面設定的數(shù)組,就會失敗,但不會報錯,這點是我一段一段代碼排查出來的,原理我也不懂,希望有人指教一下
想要在查看這個頁面的代碼顯示效果,需要在下面再調(diào)用一下這個函數(shù)
改進之后的代碼和注釋我發(fā)下面了