【问题标题】:PHP - GD fails on certain files only why?PHP - GD 在某些文件上失败只是为什么?
【发布时间】:2016-10-22 10:02:52
【问题描述】:

我正在使用以下示例代码来调整给定图像的大小,但是某些图像(只有少数几张照片,但次数很多)失败并返回黑色 jpeg 函数中没有任何错误

但是,如果我在 Photoshop 中打开该图像并再次将其保存为原样为 JPEG(相同的文件类型),那么它似乎可以正常工作。它正确调整了新保存的图像的大小。

我想知道原来的有什么问题吗?因为原始文件似乎格式正确,甚至可以在 Windows 照片查看器中打开。

请注意

没有问题
  1. 文件不存在

  2. 文件类型...等(扩展名为 jpeg)

  3. 抱歉,由于隐私原因,我无法在此处发布示例照片。

  4. 已安装最新的 GD 库,并且可以正常处理其他图像

  5. 其实我发现了它的“$image = imagecreatefromjpeg($filename);”如果失败,它会为失败的图像返回 false,而当我执行“var_dump($image);”时它返回“resource(5) of type (gd)”的其他图像;

进一步调试发现我出现以下错误

Warning: imagecreatefromjpeg(): gd-jpeg, libjpeg: recoverable error: Invalid SOS parameters for sequential JPEG

对此有何解决方案或理由?

我用来调整大小的代码如下。

<?php

$thumb=resizeImage('20160612_123658_web.jpg',500,500);
// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');
// Output
imagejpeg($thumb);

/**
* Resize an image and keep the proportions
* @author Allison Beckwith <allison@planetargon.com>
* @param string $filename
* @param integer $max_width
* @param integer $max_height
* @return image
*/
function resizeImage($filename, $max_width, $max_height)
{
    list($orig_width, $orig_height) = getimagesize($filename);

    $width = $orig_width;
    $height = $orig_height;

    # taller
    if ($height > $max_height) {
        $width = ($max_height / $height) * $width;
        $height = $max_height;
    }

    # wider
    if ($width > $max_width) {
        $height = ($max_width / $width) * $height;
        $width = $max_width;
    }

    $image_p = imagecreatetruecolor($width, $height);

    $image = imagecreatefromjpeg($filename);

    imagecopyresampled($image_p, $image, 0, 0, 0, 0,
                                     $width, $height, $orig_width, $orig_height);

    return $image_p;
}

【问题讨论】:

标签: php image image-processing gd


【解决方案1】:

在运行 imagemagick 的 convert.exe 时,我用手机 (S5) 拍摄的照片也遇到了类似的问题,但不是系统地。

在我使用 IrfanView 无损 JPEG 插件(快捷方式“J”),选项“无(可用于优化和清理)”清理有问题的图像后,问题停止了。然后使用 convert.exe 并没有导致任何警告。

所以这可能是图像元数据的不合规问题。对我来说,它并没有导致引用的黑色图像问题。修复源 JPEG 文件似乎是前进的方向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-04
    • 2018-02-20
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    相关资源
    最近更新 更多