动态调整大小的图像PHP的内容长度

时间:2012-03-28 08:06:36

标签: php mysql image image-processing http-headers

我编写了一个PHP页面来动态调整存储为MySQL数据库中BLOB的图像。我正在执行此操作而不存储任何临时文件。有没有办法确定要在标题中发送的内容长度?

我使用imagecopyresampled()创建了图像,并使用imagepng()(或类似物)输出图像数据。

$origImage = imagecreatefromstring($image['data']);
$newImage = imagecreatetruecolor($width, $height);

// preserve alpha
imagealphablending($newImage, false);
imagesavealpha($newImage, true);

$resizeSuccess = imagecopyresampled($newImage, $origImage, 0, 0, 0, 0, $width, $height, $image['width'], $image['height']);

header('Content-Type: image/png');
imagepng($newImage, null, $pngCompression, PNG_NO_FILTER);
imagedestroy($newImage);

1 个答案:

答案 0 :(得分:4)

请尝试以下操作:

,而不是imagepng来电
ob_start(function($c) {
    header("Content-Length: ".strlen($c));
    return $c;
});
imagepng($newImage, null, $pngCompression, PNG_NO_FILTER);
obj_end_flush();

此外,服务器应自动为您处理Content-Length