我用php gd调整图像大小.结果是我想要上传到Amazon S3的图像资源.如果我先将图像存储在磁盘上但我想直接从内存上传它们,它的效果很好.如果我只知道图像的字节大小,那是可能的.
有没有办法获得gd图像资源的大小(以字节为单位)?
Den*_*ink 13
您可以使用PHP的内存i/o流来保存图像,然后以字节为单位获取大小.
你做的是:
$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');
Run Code Online (Sandbox Code Playgroud)
现在你应该知道尺寸了
我不知道任何(gd)方法来获取图像资源的大小.
小智 9
我无法在php:// memory with imagepng上写,所以我使用ob_start(),ob_get_content()结束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
Run Code Online (Sandbox Code Playgroud)
这也有效:
$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
Run Code Online (Sandbox Code Playgroud)
现在$size将以字节为单位.