我正在用php gd调整图像的大小。结果是我想要上传到亚马逊S3的图片资源。如果我先将图像存储在磁盘上,效果会很好,但我想直接从内存中上传它们。如果我只知道图像的字节大小,这是可能的。
有没有办法获取gd图像资源的大小(以字节为单位)?
发布于 2010-10-26 20:06:51
您可以使用PHP的memory i/o stream将图像保存到其中,然后获得以字节为单位的大小。
您要做的是:
$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)方法来获取图像资源的大小。
发布于 2012-06-25 03:43:24
我不能用imagepng在php://内存上写东西,所以我使用ob_start(),ob_get_content() end ob_end_clean()
$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发布于 2014-07-10 13:54:58
这也是可行的:
$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将以字节为单位显示您的大小。
https://stackoverflow.com/questions/4023441
复制相似问题