【问题标题】:Handling corrupt/truncated files with PHP gd-png使用 PHP gd-png 处理损坏/截断的文件
【发布时间】:2017-03-07 19:58:11
【问题描述】:

我正在编写一个脚本,该脚本必须处理大量图像,我需要一种优雅的方式来处理集中损坏/截断的图像。

目前我的脚本适用于所有有效图像,但在遇到截断文件时会崩溃。它一定是偷偷溜进了文件夹,但我还是想处理这个案子。

它是一个 *.png.tmp 文件,当将它提供给 imagecreatefrompng() 时,它会导致致命错误并停止我的脚本。

Fatal error: imagecreatefrompng(): gd-png: fatal libpng error: Read Error: truncated data...

getimagesize()exif_imagetype() 都将其识别为 PNG。

由于我无法将其作为异常处理,有没有办法在不使我的脚本崩溃并且不依赖文件名中的扩展名的情况下检查文件是否有效?

我可以忍受跳过图像,但我不知道如何在遇到致命错误之前将其识别为问题图像。

【问题讨论】:

标签: php gd


【解决方案1】:

您是否尝试使用@运算符来抑制错误,并检查图像是否实际创建?

foreach ($imagePaths as $imagePath)
{
    $image = @imagecreatefrompng($imagePath);
    if (!$image)
    {
        //maybe delete bad image?
        unlink($imagePath);
        continue;//do nothing in this iteration anymore
    }
    //do your magic here
}

【讨论】:

    猜你喜欢
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多