If I generate an image directly in a php script to be sent back to JS, is it mandatory to have this interoom step of saving the image to the file system ?
如果我直接在php脚本中生成图像以发送回JS,是否必须将此图像保存到文件系统?
I create an image using imagecreate() then imagecopyresized() then want to send the result as base64 wrapped in json (Later I will want an array of strings rather than just one).
我使用imagecreate()然后imagecopyresized()创建一个图像,然后想要将结果作为包装在json中的base64发送(稍后我将需要一个字符串数组而不是一个字符串)。
I couldn't find anything that base64_encode() would take in apart from the bytes from fread(). I find having to save to the file system wasteful and think there must be a better way!
除了来自fread()的字节之外,我找不到base64_encode()会占用的任何内容。我发现必须保存到文件系统浪费,并认为必须有更好的方法!
Thanks
2 个解决方案
#1
4
You don't have to save the file at all. Simply use output buffering:
您根本不必保存文件。只需使用输出缓冲:
ob_start();
imagepng($im);
$pngData = ob_get_contents();
ob_end_clean();
echo base64_encode($pngData);
#2
0
Just want to add; There's a function ob_get_clean()
which executes both ob_get_contents()
and ob_end_clean()
只想添加;有一个函数ob_get_clean(),它执行ob_get_contents()和ob_end_clean()
#1
4
You don't have to save the file at all. Simply use output buffering:
您根本不必保存文件。只需使用输出缓冲:
ob_start();
imagepng($im);
$pngData = ob_get_contents();
ob_end_clean();
echo base64_encode($pngData);
#2
0
Just want to add; There's a function ob_get_clean()
which executes both ob_get_contents()
and ob_end_clean()
只想添加;有一个函数ob_get_clean(),它执行ob_get_contents()和ob_end_clean()