PHP GD图像未在Chrome中显示 [英] PHP GD image not displaying in Chrome

查看:90
本文介绍了PHP GD图像未在Chrome中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在一个项目中使用了Captcha.php,它在所有浏览器中均可打开,但我们无法在Google chrome版本22中查看。

We have used Captcha.php on one of our project, it opens in all browsers but we are not able to view in Google chrome Version 22.

我们的验证码脚本

session_start();
$captcha = new SimpleCaptcha();
$captcha->CreateImage();

class SimpleCaptcha 
{
    function CreateImage()
    {
        header("Content-Type: image/jpeg");

        $md5              = md5(rand(0,9999));
        $pass             = substr($md5, 10, 5);
        $_SESSION["pass"] = $pass;

        $image     = ImageCreatetruecolor(100, 20);
        $clr_white = ImageColorAllocate($image, 0, 0, 0);
        $clr_black = ImageColorAllocate($image, 255, 255, 255);

        imagefill($image, 0, 0, $clr_white);
        imagefontheight(15);
        imagefontwidth(15);
        imagestring($image, 5, 30, 3, $pass, $clr_black);

        return imagejpeg($image);
        imagedestroy($image);
    }
}

HTML实现

<img src="code/captcha.php" width="100" height="20" alt="Captcha Code"/>

我们无法在Google Chrome上查看它。所有浏览器都返回相同的图像。

We are not able to view it on Google Chrome. All the browser return the same image.

推荐答案

添加

ob_clean();

为我解决了。

完整示例:

$img = imagecreatefromjpeg('my_image.jpg');
ob_clean();
header('Content-Type: image/jpeg');
imagejpeg($img);
imagedestroy($img);

这篇关于PHP GD图像未在Chrome中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆