php GD库低于一定大小的图像不显示

时间:2012-04-17 13:24:55

标签: php gd

我正在拍摄一张链接的图片,并使用imagecopyresampled()制作我自己的调整版本。

当试图显示GD库重新采样的图像时,那些低于一定大小(大约160px宽度)的图像仅显示为一堆乱码(例如:' JFIF > CREATOR:gd-jpeg v1。 0(使用IJG JPEG v62),默认质量 C')。

我自己检查了图像,并且在图片浏览器中打开时,它们被正确存储并正确显示。但是,将它们打印到浏览器不起作用。当我将像素大小更改为更大的像素大小时,它可以正常工作。

我在浏览器中显示图像

$handle = fopen($fileName, "rb");
$img = fread($handle, filesize($fileName));
fclose($handle);
$imgSize = strlen($img);
$image_type = exif_imagetype($fileName);
header('Content-Type: image/JPEG');
header('Content-Transfer-Encoding: binary');
header("Content-Length: $imgSize");
print $img;
return true;

现在我开始相信它与GD库有关。其他人对此有进一步的了解吗?

感谢。

编辑:

布拉德的一条建议向我展示了正确的方法。像往常一样,这是一个完全愚蠢的错误。我还不能回答我自己的问题,所以我想我会做一个编辑。

我忘记在最初的描述中包含的内容是前面的代码都在我的一个模型中,然后我调用了一个函数来在我的控制器中显示图像。

我最近切换到了Symfony2,并且在我的控制器中几乎无处不在使用Response返回。但是,我一直在重用我的旧显示图像代码,它是在没有Symfony的情况下编写的。我有一个“show image”类型的函数,我在我的控制器中调用,然后返回一个空响应。因此,Symfony的Response返回搞砸了编码。高级浏览器能够自动校正大图像,但不适用于小图像。

我的最终解决方案成了

在我的模特中

$handle = fopen($fileName, "rb");
$image= fread($handle, filesize($fileName));
fclose($handle);
$image_type = exif_imagetype($fileName);

在我的控制器中

return new Response($image, 200, array('Content-Type' => $image_type));

2 个答案:

答案 0 :(得分:1)

您看到的是原始图像数据,这意味着浏览器由于某种原因没有注册内容类型。

我怀疑发生的事情是,您测试的任何浏览器都与Content-Type标题区分大小写。

image/JPEG更改为image/jpeg,看看是否适合您。

至于解释为什么它适用于较大的图像... Content-Type可能永远不会被识别为已知类型。浏览器将查看他们收到的实际内容,以试图找出它的类型。它的内容嗅探功能可能有些东西看起来很小,并且说“不,可能不是图像”,然后继续前进。

答案 1 :(得分:1)

他们有很多方法来实现工作脚本..我使用file_get_contentsread_file

给你2个例子

示例1 使用file_get_contents

$fileName = "http://www.bestmastersineducation.com/teaching-to-tests/standardized-tests.jpg"; // Full path to image
$img = file_get_contents($fileName);
$imgSize = strlen($img);
header('Content-Type: image/jpeg');
header ('Content-Transfer-Encoding: binary' );
header ("Content-Length: $imgSize" );
echo $img;
return true;

参见DEMO:http://codepad.viper-7.com/GFqsLe

现在创建一个较小的图像样本

header ( 'Content-Type: image/jpeg' );
header ( 'Content-Transfer-Encoding: binary' );

$new_width = 100;
$new_height = 100;

$fileName = "http://www.bestmastersineducation.com/teaching-to-tests/standardized-tests.jpg"; // 
$img = file_get_contents ( $fileName );
$im = imagecreatefromstring ( $img );
if ($im !== false) {
    list ( $width, $height ) = getimagesize ( $fileName );
    $imageSample = imagecreatetruecolor ( $new_width, $new_height ); // new                                                    // Height
    imagecopyresampled ( $imageSample, $im, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
    imagejpeg ( $imageSample, null, 100 );
}

演示链接:http://codepad.viper-7.com/gXmoDW

示例2 使用readfile

$fileName = "a.jpg" ;   //Full path to image
$imgSize = filesize($fileName);
$imageType = exif_imagetype($fileName);
header('Content-Type:' . image_type_to_mime_type ($imageType));
header('Content-Transfer-Encoding: binary');
header("Content-Length: $imgSize");
readfile($fileName);

可选

如果您想强行下载,请改用

$fileName = "a.jpg" ; //Full path to image 
$imgSize = filesize($fileName);
$imageType = exif_imagetype($fileName);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fileName));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header("Content-Length: $imgSize");
readfile($fileName);

我希望这会有所帮助