首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在PHP和GD中获取图片资源的字节大小?

如何在PHP和GD中获取图片资源的字节大小?
EN

Stack Overflow用户
提问于 2010-10-26 20:03:52
回答 5查看 16.1K关注 0票数 7

我正在用php gd调整图像的大小。结果是我想要上传到亚马逊S3的图片资源。如果我先将图像存储在磁盘上,效果会很好,但我想直接从内存中上传它们。如果我只知道图像的字节大小,这是可能的。

有没有办法获取gd图像资源的大小(以字节为单位)?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-10-26 20:06:51

您可以使用PHP的memory i/o stream将图像保存到其中,然后获得以字节为单位的大小。

您要做的是:

代码语言:javascript
复制
$img = imagecreatetruecolor(100,100);
// do your processing here
// now save file to memory
imagejpeg($img, 'php://memory/temp.jpeg'); 
$size = filesize('php://memory/temp.jpeg');

现在你应该知道大小了

我不知道任何(Gd)方法来获取图像资源的大小。

票数 14
EN

Stack Overflow用户

发布于 2012-06-25 03:43:24

我不能用imagepng在php://内存上写东西,所以我使用ob_start(),ob_get_content() end ob_end_clean()

代码语言:javascript
复制
$image = imagecreatefrompng('./image.png'); //load image
// do your processing here
//...
//...
//...
ob_start(); //Turn on output buffering
imagejpeg($image); //Generate your image

$output = ob_get_contents(); // get the image as a string in a variable

ob_end_clean(); //Turn off output buffering and clean it
echo strlen($output); //size in bytes
票数 11
EN

Stack Overflow用户

发布于 2014-07-10 13:54:58

这也是可行的:

代码语言:javascript
复制
$img = imagecreatetruecolor(100,100);

// ... processing

ob_start();              // start the buffer
imagejpeg($img);         // output image to buffer
$size = ob_get_length(); // get size of buffer (in bytes)
ob_end_clean();          // trash the buffer

现在$size将以字节为单位显示您的大小。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4023441

复制
相关文章

相似问题

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