我编写了一个 PHP 页面来动态调整以 BLOB 形式存储在 MySQL 数据库中的图像的大小。我正在即时执行此操作,而不存储任何临时文件。有没有办法确定要在 header 中发送的内容长度?
我使用 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);
最佳答案
尝试以下操作,而不是使用 imagepng
调用:
ob_start(function($c) {
header("Content-Length: ".strlen($c));
return $c;
});
imagepng($newImage, null, $pngCompression, PNG_NO_FILTER);
obj_end_flush();
此外,服务器应该自动为您处理Content-Length
。
关于php - 动态调整图像大小的 PHP 的内容长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9903433/