PHP:使用ImagePng创建图像并在单个文件中使用base64

编程入门 行业动态 更新时间:2024-10-23 16:18:09
本文介绍了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

本文发布于:2023-11-01 02:22:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1547977.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并在   图像   文件   PHP   ImagePng

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!