php:如何创建图像二进制字符串而不将其保存到文件中?

php:如何创建图像二进制字符串而不将其保存到文件中?,php,jpeg,Php,Jpeg,我有一个图像变量 $im = imagecreatetruecolor(400, 300); 是否仍然可以获取jpeg格式的此图像的二进制字符串,而不将其保存到文件中? 谢谢 作为函数并添加imagedestroy() 函数图像jpeg\u tostring($im,$quality=75){ ob_start();//标准输出-->缓冲区 imagejpeg($im,NULL,$quality);//输出。。。 $imgString=ob_get_contents();//将标准输出存储在$

我有一个图像变量

$im = imagecreatetruecolor(400, 300);
是否仍然可以获取jpeg格式的此图像的二进制字符串,而不将其保存到文件中?
谢谢

作为函数并添加
imagedestroy()

函数图像jpeg\u tostring($im,$quality=75){
ob_start();//标准输出-->缓冲区
imagejpeg($im,NULL,$quality);//输出。。。
$imgString=ob_get_contents();//将标准输出存储在$imgString中
ob_end_clean();//清除缓冲区
imagedestroy($im);//销毁img
返回$imgString;
}
是的,这是可能的(即使没有输出缓冲)。它看起来没有文档化,但是您可以传递流资源而不是文件名

<?php

$stream = fopen("php://memory", "w+");
$i = imagecreatetruecolor(200, 200);
imagepng($i, $stream);
rewind($stream);
$png = stream_get_contents($stream);

很好的解决方案!比重定向脚本的整个输出要干净得多。这应该是公认的答案。
<?php

$stream = fopen("php://memory", "w+");
$i = imagecreatetruecolor(200, 200);
imagepng($i, $stream);
rewind($stream);
$png = stream_get_contents($stream);