可能重复:
How to get image resource size in bytes with PHP and GD?
是否可以使用php获取对象$ image的文件大小(而不是图像大小尺寸)?我想将其添加到我的“Content-Length:”标题中。
$image = imagecreatefromjpeg($reqFilename);
答案 0 :(得分:3)
我认为这应该有效:
$img = imagecreatefromjpeg($reqFilename);
// capture output
ob_start();
// send image to the output buffer
imagejpeg($img);
// get the size of the o.b. and set your header
$size = ob_get_length();
header("Content-Length: " . $size);
// send it to the screen
ob_end_flush();
答案 1 :(得分:2)
您可以使用filesize():
// returns the size in bytes of the file
$size = filesize($reqFilename);
如果调整大小的图像是存储在磁盘上的图像,如果您在调用imagecreatefromjpeg()
之后调整图像大小,那么上面的内容当然会起作用,那么你应该使用@One Trick Ponys解决方案并执行类似这样的操作:
// load original image
$image = imagecreatefromjpeg($filename);
// resize image
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// get size of resized image
ob_start();
// put output for image in buffer
imagejpeg($new_image);
// get size of output
$size = ob_get_length();
// set correct header
header("Content-Length: " . $size);
// flush the buffer, actually send the output to the browser
ob_end_flush();
// destroy resources
imagedestroy($new_image);
imagedestroy($image);