首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP:如何获得php中动态生成的图像的字节大小?

问PHP:如何获得php中动态生成的图像的字节大小?
EN

Stack Overflow用户
提问于 2013-01-31 23:11:04
回答 2查看 4.1K关注 0票数 4

可能重复: 如何使用PHP和GD获得以字节为单位的图像资源大小?

是否可以使用php获取对象$image的文件大小(而不是图像大小)?我想把这个添加到我的“内容长度:”标题中。

代码语言:javascript
复制
$image = imagecreatefromjpeg($reqFilename);
EN

回答 2

Stack Overflow用户

发布于 2013-01-31 23:19:26

我认为这应该是可行的:

代码语言:javascript
复制
$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();
票数 3
EN

Stack Overflow用户

发布于 2013-01-31 23:13:07

您可以使用文件大小()来完成以下工作:

代码语言:javascript
复制
 // returns the size in bytes of the file
 $size = filesize($reqFilename);

当然,只有当调整大小的映像是存储在磁盘上的映像时,如果在调用imagecreatefromjpeg()后要调整映像的大小,那么就应该使用@One技巧Ponys解决方案,并执行如下操作:

代码语言:javascript
复制
  // 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);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14636857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档