问题描述
php新手,自己写了一下轻量级别的mvc框架,非常简陋,今天添加验证码类的时候发现图片一直没有办法显示出来,于是怀疑验证码类本身有点问题,但是通过在验证码类中调用自己测试,发现可以正常的显示验证码。所以排除了验证码类自己的问题,在尝试百度以后并木有发现相应的解决方法
环境描述
homestead虚拟机:
php7.1.2
开启GD等各种必须的扩展
使用Phpstrom进行开发,所以不存在BOM的问题
测试过程
验证码类如下所示,当时写这个类的时候测试结果是可以正常使用的,包括这一次在此类中调用本身,也是可以正常的使用的.
namespace framework\tools;
/*
* Captcha Tool Class
*/
class Captcha
{
private $width = 100; //Picture width
private $height = 30; //Picture height
private $number = 4; //Captcha words number
private $font_file = './STHUPO.TTF'; //TTF file path
private $font_size = 20; //Font Size
public function __set($p,$v)
{
if(property_exists($this, $p)){
$this -> $p = $v;
}
}
public function __get($p)
{
if(property_exists($this, $p)){
return $this -> $p;
}
}
//Make Captcha
public function makeImage()
{
//1. Make canvas
$image = imagecreatetruecolor($this->width, $this->height);
//2. Assign color
$color = imagecolorallocate($image, mt_rand(100,255), mt_rand(100,255), mt_rand(100,255));
imagefill($image, 0, 0, $color);
//3. Make words
$code = $this->makeCode();
//Store Captcha answer in session
session_start();
$_SESSION['code'] = $code;
for($i=0;$i
imagettftext($image, $this->font_size, mt_rand(-30,30), ($this->width/$this->number)*$i+5, 20, mt_rand(0,100), $this->font_file, $code[$i]);
}
//Make 100 point
for($i=0;$i<100;$i++){
imagesetpixel($image, mt_rand(0,$this->width), mt_rand(0,$this->height), mt_rand(0,100));
}
//Make 10 line
for($i=0;$i<10;$i++){
$color = imagecolorallocate($image, mt_rand(100,150), mt_rand(100,150), mt_rand(100,150));
imageline($image, mt_rand(0,$this->width), mt_rand(0,$this->height), mt_rand(0,$this->width), mt_rand(0,$this->height), $color);
}
//3. Export to browser
header("Content-Type:image/png");
imagepng($image);
//4. Destory image
imagedestroy($image);
}
//Make code
public function makeCode()
{
//A-Z a-z
$upper = range('A','Z');
$lower = range('a','z');
//3-9
$number = range(3,9);
//merge
$code = array_merge($lower,$upper,$number);
shuffle($code);
$str = '';
for($i=0;$inumber;$i++){
$str .= $code[$i];
}
//echo '
';
//var_dump($str);
return $str;
}
}
然后就是调用这个类
public function makeCaptchaAction()
{
$captcha = new Captcha();
$captcha->font_file = './application/public/fonts/STHUPO.TTF';
$captcha->makeImage();
}
在浏览器端查看效果如下
我写的这个劣质框架的文件分布为:
如果您感觉麻烦,会占用您的时间,我愿意付费请您进行解答
谢谢