如果在类文件之外创建实例,验证码类将不会显示图像

发布于 2024-12-11 02:23:10 字数 2807 浏览 0 评论 0原文

我做了一个验证码类来生成验证码。由于某种原因,如果我在类文件本身中创建该类的实例,我只能显示图像。否则我什么也得不到。在 firebug 中,我看到该文件被包含,但尽管我使用了 header("Content-type: image/png");
,但它仍以 html 形式发送 class.captcha.php

<?php
class captcha
{

    public $length=4;
    public $width=150;
    public $height=50;
    public $allowedChar='1234569ACEFGHJKMNPQRSTXZ';
    public $font='Arcade.ttf';
    public $fontSize=25;
    public $fontRed=0;
    public $fontGreen=0;
    public $fontBlue=104;
    public $backgroundRed=204;
    public $backgroundGreen=204;
    public $backgroundBlue=255;
    public $noiseRed=0;
    public $noiseGreen=0;
    public $noiseBlue=104;
    public $noisePercent=5;

    /*
    ** @RETURN IMAGE raw image of captcha
    */
    public function create($name)
    {
        //string
        for ($i=0;$i<$this->length;$i++) 
        {$captcha.= $this->allowedChar[mt_rand(0, strlen($this->allowedChar))];}

        //image
        $img=imagecreatetruecolor($this->width, $this->height);
        //colors
        $bgColor=imagecolorallocate($img, $this->backgroundRed, $this->backgroundGreen, $this->backgroundBlue);
        $fontColor=imagecolorallocate($img, $this->fontRed, $this->fontGreen, $this->fontBlue);
        $noiseColor=imagecolorallocate($img, $this->noiseRed, $this->noiseGreen, $this->noiseBlue);
        imagefilledrectangle($img,0,0,$this->width+1,$this->height+1,$bgColor);
        //text
        for($i=0;$i<strlen($captcha);$i++)
        {
            $rotate=rand(0, 80)-40;
            $offX=$i*$this->fontSize+rand($this->fontSize, $this->fontSize*1.2);
            $offY=$this->fontSize+rand($this->fontSize/2, $this->fontSize);;
            $letter=$captcha[$i];
            imagettftext($img, $this->fontSize, $rotate, $offX, $offY, $fontColor, $this->font, $letter);
        }
        //add noise
        for($i=0;$i<$this->height*$this->width*($this->noisePercent/100);$i++)
        {
            //noise
            $x=rand(0, $this->width);
            $y=rand(0, $this->height);
            imagesetpixel($img, $x, $y, $noiseColor);
        }
        $_SESSION[$name]=md5($captcha);
        return $img;
    }
}
?>


captcha.contact.php

<?php
session_start(); 
include('class.captcha.php');
$c=new captcha;
header("Content-type: image/png"); 
imagepng($c->create('contactCaptcha'));
?>

链接到上面(如果有帮助)http:// /yamikowebs.com/_test/_php/class.captcha.php 错误是图像“http://yamikowebs.com/_test/_php/captcha.contact.php”无法显示,因为它包含错误。
如果我将 capture.contact.php 的内容放在 class.captcha.php 的末尾,它就没有问题。

I made a captcha class to generate a captcha. for some reason I can only get the image to display if I make an instance of the class in the class file itself. otherwise I get nothing. In firebug I see the file being included but its being sent as html despite I used header("Content-type: image/png");
class.captcha.php

<?php
class captcha
{

    public $length=4;
    public $width=150;
    public $height=50;
    public $allowedChar='1234569ACEFGHJKMNPQRSTXZ';
    public $font='Arcade.ttf';
    public $fontSize=25;
    public $fontRed=0;
    public $fontGreen=0;
    public $fontBlue=104;
    public $backgroundRed=204;
    public $backgroundGreen=204;
    public $backgroundBlue=255;
    public $noiseRed=0;
    public $noiseGreen=0;
    public $noiseBlue=104;
    public $noisePercent=5;

    /*
    ** @RETURN IMAGE raw image of captcha
    */
    public function create($name)
    {
        //string
        for ($i=0;$i<$this->length;$i++) 
        {$captcha.= $this->allowedChar[mt_rand(0, strlen($this->allowedChar))];}

        //image
        $img=imagecreatetruecolor($this->width, $this->height);
        //colors
        $bgColor=imagecolorallocate($img, $this->backgroundRed, $this->backgroundGreen, $this->backgroundBlue);
        $fontColor=imagecolorallocate($img, $this->fontRed, $this->fontGreen, $this->fontBlue);
        $noiseColor=imagecolorallocate($img, $this->noiseRed, $this->noiseGreen, $this->noiseBlue);
        imagefilledrectangle($img,0,0,$this->width+1,$this->height+1,$bgColor);
        //text
        for($i=0;$i<strlen($captcha);$i++)
        {
            $rotate=rand(0, 80)-40;
            $offX=$i*$this->fontSize+rand($this->fontSize, $this->fontSize*1.2);
            $offY=$this->fontSize+rand($this->fontSize/2, $this->fontSize);;
            $letter=$captcha[$i];
            imagettftext($img, $this->fontSize, $rotate, $offX, $offY, $fontColor, $this->font, $letter);
        }
        //add noise
        for($i=0;$i<$this->height*$this->width*($this->noisePercent/100);$i++)
        {
            //noise
            $x=rand(0, $this->width);
            $y=rand(0, $this->height);
            imagesetpixel($img, $x, $y, $noiseColor);
        }
        $_SESSION[$name]=md5($captcha);
        return $img;
    }
}
?>

captcha.contact.php

<?php
session_start(); 
include('class.captcha.php');
$c=new captcha;
header("Content-type: image/png"); 
imagepng($c->create('contactCaptcha'));
?>

link to above if it helps http://yamikowebs.com/_test/_php/class.captcha.php
the error is The image “http://yamikowebs.com/_test/_php/captcha.contact.php” cannot be displayed because it contains errors.
if i put the contents of capture.contact.php at the end of class.captcha.php it works no problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妖妓 2024-12-18 02:23:10

我认为解决方案可能是您正在使用 PHP 的结束标签:

?>

它们不是必需的,但有时是有害的。如果此标记后有任何标志,则您尝试发送的标头将不会发送,并且可能会出现通知(取决于 error_reporting 设置)。

只需删除结束标签 (?>) 就可以了。

另外一件事 - 如果脚本需要某些东西才能工作(例如您的类文件),请使用 require() 而不是 include()

I think the solution may be the fact you are using the closing tags of PHP:

?>

They are not required, but sometimes harmful. If there is any sign after such tag, the headers that you are trying to send will not be sent and the notice may appear (depending on the error_reporting setting).

Just remove closing tags (?>) and this should be ok.

One additional thing - if something is required for the script to work (such as your class file), use require() instead of include().

留一抹残留的笑 2024-12-18 02:23:10

contact.captcha.php

<?php
include('class.captcha.php');
$c=new captcha;
header("Content-type: image/png"); 
imagepng($c->create('contactCaptcha'));
?>

session_start() 发送标头信息,因此一旦删除它就开始正常工作。

contact.captcha.php

<?php
include('class.captcha.php');
$c=new captcha;
header("Content-type: image/png"); 
imagepng($c->create('contactCaptcha'));
?>

session_start() sends header information so once removed it started working fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文