鍍金池/ 問答/PHP  HTML/ php驗(yàn)證碼類可以正常顯示圖片,但是在mvc框架中使用自動加載類的時候無法正常顯

php驗(yàn)證碼類可以正常顯示圖片,但是在mvc框架中使用自動加載類的時候無法正常顯示圖片,求解決思路?

問題描述

php新手,自己寫了一下輕量級別的mvc框架,非常簡陋,今天添加驗(yàn)證碼類的時候發(fā)現(xiàn)圖片一直沒有辦法顯示出來,于是懷疑驗(yàn)證碼類本身有點(diǎn)問題,但是通過在驗(yàn)證碼類中調(diào)用自己測試,發(fā)現(xiàn)可以正常的顯示驗(yàn)證碼。所以排除了驗(yàn)證碼類自己的問題,在嘗試百度以后并木有發(fā)現(xiàn)相應(yīng)的解決方法

環(huán)境描述

homestead虛擬機(jī):

  1. php7.1.2
  2. 開啟GD等各種必須的擴(kuò)展
  3. 使用Phpstrom進(jìn)行開發(fā),所以不存在BOM的問題

測試過程

驗(yàn)證碼類如下所示,當(dāng)時寫這個類的時候測試結(jié)果是可以正常使用的,包括這一次在此類中調(diào)用本身,也是可以正常的使用的.

<?php
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<strlen($code);$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;$i<$this->number;$i++){
            $str .= $code[$i];
        }
        //echo '<pre>';
        //var_dump($str);
        return $str;
    }
}

然后就是調(diào)用這個類

    public function makeCaptchaAction()
    {
        $captcha = new Captcha();
        $captcha->font_file = './application/public/fonts/STHUPO.TTF';
        $captcha->makeImage();
    }

在瀏覽器端查看效果如下

報錯如下

我寫的這個劣質(zhì)框架的文件分布為:

clipboard.png

如果您感覺麻煩,會占用您的時間,我愿意付費(fèi)請您進(jìn)行解答

謝謝

回答
編輯回答
硬扛

調(diào)試驗(yàn)證碼類,你可以先把header("Content-Type:image/png");這行注釋了,跑一遍看看有什么錯誤信息,瀏覽器看不到的話,看看錯誤日志。

2017年3月5日 12:34