php新手請教一個關于驗證碼錯誤的問題。
備注:xampp搭配thinkphp框架,Mac平臺,phpstormIDE。
敘述情況:
就是一個顯示驗證碼的簡單的功能。
假如直接在phptorm內部撰寫這個輸出驗證碼的函式(沒有使用類的結構),貼出代碼:
<?php
/**
* Created by PhpStorm.
* User: templzg
* Date: 2019-12-09
* Time: 22:50
*/
$x=250;$y=62;
$count=5;
$TheRandomRootString='AVCDEFGHIJKLMNOPQ123456789';
$TheCreatedString='';
$LengthOfStr=strlen($TheRandomRootString)-1;
for ($i=0;$i<=$count;++$i)
{
//這里的意思是,$TheRandomRootString可以看作是一個字符陣列,在取下標的時候,這個下標的值為0~$LengthOfStr的隨機值。
$TheCreatedString.=$TheRandomRootString[mt_rand(0,$LengthOfStr)];
}
$thecode=$TheCreatedString;
//echo $thecode;
//die();
$im=imagecreate($x,$y);
//設定驗證碼文字的顏色(其實應該還有字體)
imagecolorallocate($im,mt_rand(50,200),mt_rand(0,155),mt_rand(0,155));
$fontcolor=imagecolorallocate($im,225,225,225);
$fontfile='hanti.ttf';
//在影像中繪制驗證碼
for ($i=0,$len=strlen($thecode);$i<$len;++$i)
{
imagettftext($im,30,mt_rand(0,20)-mt_rand(0,25),32+$i*40,mt_rand(30,50),$fontcolor,$fontfile,$thecode[$i]);
}
//添加八條干擾線,顏色、位置隨機
for ($i=0;$i<8;++$i)
{
$linecolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($im,mt_rand(0,$x),0,mt_rand(0,$x),$y,$linecolor);
}
//添加250個噪點
for ($i=0;$i<250;++$i)
{
imagesetpixel($im,mt_rand(0,$x),mt_rand(0,$y),$fontcolor);
}
//清除快取
ob_clean();
//設定訊息頭
header('Content-Type:image/png');
//輸出圖片
imagepng($im);
imagedestroy($im);
---------------------------------------------------------------- 分割線
是可以正常顯示出驗證碼的:

但是具體到用tp5框架來顯示的話,會出現問題,即出現亂碼

出現這個亂碼情況的目前情況是:
在application目錄創建了一個user目錄->controller目錄->GetCode.php一路下來,代碼貼出:
<?php
/**
* Created by PhpStorm.
* User: templzg
* Date: 2019-12-07
* Time: 16:49
*/
namespace app\user\controller;
use think\controller;
class GetCode extends controller
{
//生成影像
public static function show($x=150,$y=62)
{
//清除快取
ob_clean();
//設定訊息頭
header('Content-Type:image/png');
$thecode=self::creat();
// var_dump($thecode);
//創建影像資源,隨機生成背景顏色
$im=imagecreate($x,$y);
//設定驗證碼文字的顏色(其實應該還有字體)
imagecolorallocate($im,mt_rand(50,200),mt_rand(0,155),mt_rand(0,155));
$fontcolor=imagecolorallocate($im,255,255,255);
$fontfile='../thinkphp/library/fonts/hanti.ttf';
//在影像中繪制驗證碼
for ($i=0,$len=strlen($thecode);$i<$len;++$i)
{
imagettftext($im,30,mt_rand(0,20)-mt_rand(0,25),32+$i*40,mt_rand(30,50),$fontcolor,$fontfile,$thecode[$i]);
}
//添加八條干擾線,顏色、位置隨機
for ($i=0;$i<8;++$i)
{
$linecolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($im,mt_rand(0,$x),0,mt_rand(0,$x),$y,$linecolor);
}
//添加250個噪點
for ($i=0;$i<250;++$i)
{
imagesetpixel($im,mt_rand(0,$x),mt_rand(0,$y),$fontcolor);
}
//輸出圖片
imagepng($im);
imagedestroy($im);
}
//這里的count的值為指定生成的驗證碼的字符數量
private static function creat($count=5)
{
$TheRandomRootString='AVCDEFGHI123456';
$TheCreatedString='';
$LengthOfStr=strlen($TheRandomRootString)-1;
for ($i=0;$i<$count;++$i)
{
//這里的意思是,$TheRandomRootString可以看作是一個字符陣列,在取下標的時候,這個下標的值為0~$LengthOfStr的隨機值。
$TheCreatedString.=$TheRandomRootString[mt_rand(0,$LengthOfStr)];
}
return $TheCreatedString;
}
}
//
目前是配置了虛擬主機名的:
www.tp5.com
訪問show()操作(函式)的url路徑是:
http://www.tp5.com/user/GetCode/show
此外GD庫也開啟的

---------------------------------------------------------------------------------------------------------分割線
除此之外在使用模版檔案進行渲染搭配img標簽試過幾種:



以及scr中采用{:url('GetCode','show')}都無法正常顯示--均是亂碼。
---------------------------------------分割線
另外,采用
"<img src='https://bbs.csdn.net/application/user/GetImage.php' width='580' height='550'>"
然后在GetImage.php中定義輸出驗證碼的函式,顯示結果是一個圖片不顯示的問號占位圖。
希望有人能夠指點一下。謝謝。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/121942.html
標籤:Apache
上一篇:Ext技術怎么樣
