imagecreatefrompng(和imagecreatefromstring)导致不可恢复的致命错误 [英] imagecreatefrompng (and imagecreatefromstring) causes to unrecoverable fatal error

查看:1294
本文介绍了imagecreatefrompng(和imagecreatefromstring)导致不可恢复的致命错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在不正确的png图像上使用php-gd函数时,出现致命PHP错误.这似乎是某种错误,因为相应于功能文档(例如imagecreatefrompng):

When I'm trying use php-gd functions on incorrect png images, I have Fatal PHP error. It seems to be some kind of bug, because accordingly to the functions documentation (imagecreatefrompng, for example):

* @return resource an image resource identifier on success, false on errors.

但是当我尝试使用不正确的图像进行操作时,我有:

But when I try to do it with my incorrect image, I have:

Fatal error: imagecreatefrompng(): gd-png: fatal libpng error: Read Error: truncated data in /var/www/common/models/Utils.php on line 61

Fatal error: imagecreatefrompng(): gd-png: fatal libpng error: Read Error: truncated data in /var/www/common/models/Utils.php on line 61

导致此错误的代码很简单:

The code that leads to this error is simple:

$handle = imagecreatefrompng($fname);

此字符串后无代码执行.

No code executes after this string.

尝试从同一字符串创建图像时,imagecreatefromstring的行为相同.

The same behaviour is for imagecreatefromstring when trying to create an image from the same string.

我无法修复"此图片,因为它是用户提供的,因此我需要处理这种情况.

I can't "fix" this picture, because it is user-provided, so I need to handle this cases.

我试图这样使用try...catch块:

echo 'start'."\n";
try {
    imagecreatefromstring($result);
} catch (\Throwable $e) {
    echo 'error'."\n";
    return null;
}
echo 'success'."\n";

但是脚本仅输出开始",然后死机并显示我上面发布的错误描述.

But script outputs only "start", and then dies and shows error description I have posted above.

Ubuntu 16.04.2,PHP 7.0,php7.0-gd扩展都是最新版本.

Ubuntu 16.04.2, PHP 7.0, php7.0-gd extension, both are latest version.

所以我不能用try ... catch块来处理它,我也不知道该如何处理或修复它.有什么想法吗?

So I can't handle it with try...catch block and I don't know how to handle or fix it at all. Any ideas?

UPD:与环境似乎真的很奇怪,因为当我在Windows(使用PHP 7.0)下运行相同的代码时,会产生正确的警告"错误.

UPD: It seems to be really weird bug with environment, because when I run same code under Windows (with PHP 7.0) it produces correct "Warning" error.

UPD2::似乎是新鲜的错误 https: //bugs.php.net/bug.php?id=73986

UPD2: It seems to be that fresh bug https://bugs.php.net/bug.php?id=73986

推荐答案

参加聚会有点晚,但我也遇到了这个问题,不能等到我的ubuntu LTS中包含该错误修复程序 我发现的唯一干净的解决方法实际上是使用Imagick :: valid()检查图像是否有效.

A bit late to the party, but i ran into this problem as well, and cannot wait for the bug fix to be included in my ubuntu LTS The only clean workaround i found is actually to use Imagick::valid() check the image is valid.

function imageIsValid($path)
{
    try
    {
        $imagick = new \Imagick($path);

        return $imagick->valid();
    }
    catch (\Exception $e)
    {
        return false;
    }
}

当然,您的服务器/主机需要安装php imagick扩展程序...

Of course, your server/hosting needs to have php imagick extension installed...

这篇关于imagecreatefrompng(和imagecreatefromstring)导致不可恢复的致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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