PHP:使用ImagePng创建图像并在单个文件中使用base64_encode进行转换? [英] PHP: create image with ImagePng and convert with base64_encode in a single file?

查看:1145
本文介绍了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_encode进行转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆