如何使用PHP和GD获取以字节为单位的图像资源大小? [英] How to get image resource size in bytes with PHP and GD?
问题描述
我正在使用php gd调整图像大小。结果是我想要上传到Amazon S3的图像资源。如果我先将图像存储在磁盘上但我想直接从内存上传它们,它的效果很好。如果我只知道图像的字节大小,那是可能的。
I'm resizing images with php gd. The result is image resources that i want to upload to Amazon S3. It works great if i store the images on disk first but i would like to upload them directly from memory. That is possible if i just know the bytesize of the image.
有没有办法获得gd图像资源的大小(以字节为单位)?
Is there some way of getting the size (in bytes) of an gd image resource?
推荐答案
您可以使用PHP的内存i / o流保存图像并随后获取大小(以字节为单位)。
You could use PHP's memory i/o stream to save the image to and subsequently get the size in bytes.
您所做的是:
$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)方法来获取图像资源的大小。
I don't know of any (gd)method to get the size of an image resource.
这篇关于如何使用PHP和GD获取以字节为单位的图像资源大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!