t p5框架验证码问题,本人新手一枚,希望前辈们指点迷津一下,谢谢

intmainfunc1 2019-12-16 09:45:07
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='/application/user/GetImage.php' width='580' height='550'>"
然后在GetImage.php中定义输出验证码的函数,显示结果是一个图片不显示的问号占位图。

希望有人能够指点一下。谢谢。
...全文
125 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ipman 2019-12-26
  • 打赏
  • 举报
回复
可能路径问题,在前端图片路径加上 __ROOT__ 方便不同虚拟主机移植。
水光清浅 2019-12-23
  • 打赏
  • 举报
回复
网站链接 : http://wangweihong.com 第一个例子是用TP5写的. 之前好像是因为直接视图渲染的时候, 框架会多加一个自己的头部信息, 导致乱码

21,890

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧