【发布时间】:2016-10-22 10:02:52
【问题描述】:
我正在使用以下示例代码来调整给定图像的大小,但是某些图像(只有少数几张照片,但次数很多)失败并返回黑色 jpeg 函数中没有任何错误。
但是,如果我在 Photoshop 中打开该图像并再次将其保存为原样为 JPEG(相同的文件类型),那么它似乎可以正常工作。它正确调整了新保存的图像的大小。
我想知道原来的有什么问题吗?因为原始文件似乎格式正确,甚至可以在 Windows 照片查看器中打开。
请注意
没有问题文件不存在
文件类型...等(扩展名为 jpeg)
抱歉,由于隐私原因,我无法在此处发布示例照片。
已安装最新的 GD 库,并且可以正常处理其他图像
其实我发现了它的“$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;
}
【问题讨论】:
-
“顺序 JPEG 的 SOS 参数无效”。似乎这个错误在网络上很流行,它会影响各种程序。
-
错误消息会产生很好的搜索查询 ~ github.com/owncloud/core/issues/21873。基本上,
ini_set ('gd.jpeg_ignore_warning', 1); -
嗯,我有一种可能发生的感觉。看起来您需要先修复 JPEG 图像。说有“没有理由” 是错误的。就 libjpeg 而言,您的图像无效
-
您可以尝试使用其他图像处理库,例如Imagick
标签: php image image-processing gd