本文介绍了PHP:使用ImagePng创建图像并在单个文件中使用base64_encode进行转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用ImagePng()创建了一个图像。我不希望它将图像保存到文件系统,但希望将其输出到与base64编码内联图像相同的页面上,例如
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
print '<p><img src="data:image/png;base64,'.base64_encode(ImagePng($png)).'" alt="image 1" width="96" height="48"/></p>';这不起作用。
是这可能在一个PHP文件中完成吗?
Is this possible in a single PHP file at all?
提前致谢!
推荐答案这里的技巧是使用输出缓冲来捕获 imagepng()的输出,该输出将输出发送到浏览器或文件。它不会将其返回存储在变量(或base64编码)中:
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):
// Enable output buffering ob_start(); imagepng($png); // Capture the output $imagedata = ob_get_contents(); // Clear the output buffer ob_end_clean(); print '<p><img src="data:image/png;base64,'.base64_encode($imagedata).'" alt="image 1" width="96" height="48"/></p>';这是改编自 imagepng() docs。
This is adapted from a user example in the imagepng() docs.
更多推荐
PHP:使用ImagePng创建图像并在单个文件中使用base64
发布评论