PHP:使用 ImagePng 创建图像并在单个文件中使用 base64_encode 进行转换?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9370847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: create image with ImagePng and convert with base64_encode in a single file?
提问by Romaeus Percival
I have created an image with ImagePng(). I dont want it to save the image to the file system but want to output it on the same page as base64 encode inline Image, like
我用 ImagePng() 创建了一个图像。我不希望它将图像保存到文件系统,但希望在与 base64 编码内联图像相同的页面上输出它,例如
print '<p><img src="data:image/png;base64,'.base64_encode(ImagePng($png)).'" alt="image 1" width="96" height="48"/></p>';
which does not work.
这不起作用。
Is this possible in a single PHP file at all?
这可能在单个 PHP 文件中吗?
Thanks in advance!
提前致谢!
回答by Michael Berkowski
The trick here will be to use output buffering to capture the output from imagepng()
, which either sends output to the browser or a file. It doesn't return it to be stored in a variable (or base64 encoded):
这里的技巧是使用输出缓冲来捕获来自 的输出imagepng()
,它将输出发送到浏览器或文件。它不会返回它以存储在变量(或 base64 编码)中:
// Enable output buffering
ob_start();
imagepng($png);
// Capture the output and clear the output buffer
$imagedata = ob_get_clean();
print '<p><img src="data:image/png;base64,'.base64_encode($imagedata).'" alt="image 1" width="96" height="48"/></p>';
This is adapted from a user example in the imagepng()
docs.
这适于从在用户示例的imagepng()
文档。
回答by SyntaxLAMP
I had trouble using the ob_get_contents() when using PHP with AJAX, so I tried this:
在使用 PHP 和 AJAX 时,我在使用 ob_get_contents() 时遇到了问题,所以我尝试了这个:
$id = generateID(); //Whereas this generates a random ID number
$file="testimage".$id.".png";
imagepng($image, $file);
imagedestroy($image);
echo(base64_encode(file_get_contents($file)));
unlink($file);
This saves a temporary image file on the server and then it is removed after it is encoded and echoed out.
这会在服务器上保存一个临时图像文件,然后在对其进行编码和回显后将其删除。
回答by SOFe
If you do not wish to store to an explicit file, and you are already using ob_start()
for something else (so you cannot use ob_start for this case without a lot of refactoring), you can define your own stream wrapper that store a stream output to a variable.
如果您不想存储到显式文件,并且您已经在ob_start()
用于其他用途(因此您不能在没有大量重构的情况下将 ob_start 用于这种情况),您可以定义自己的流包装器,将流输出存储到多变的。
You use stream_wrapper_register
to register a new stream wrapper, and implement its stream_write
method to write it to a variable whose value you can retrieve later. Then you pass this stream (actually you just need to pass the URI for this stream) to imagepng
. imagepng
wanting to close your stream won't bother you, as long as your stream wrapper doesn't destroy the data when it is closed (stream_close
method called).
您用于stream_wrapper_register
注册一个新的流包装器,并实现其stream_write
方法以将其写入一个您可以稍后检索其值的变量。然后你将这个流(实际上你只需要传递这个流的 URI)到imagepng
. imagepng
想要关闭您的流不会打扰您,只要您的流包装器在关闭时不破坏数据(stream_close
调用的方法)。