指点成金-最美分享吧

登录

php 图片流 转为base64

佚名 举报

篇首语:本文由小编为大家整理,主要介绍了php 图片流 转为base64相关的知识,希望对你有一定的参考价值。

我只会用这种方法生成图片
//以上代码忽略
imagepng($code)
这样会直接在浏览器输出图片,后面加路径则会在指定位置输出图片

那么问题来了,我要把图片转换为base64编码,就必须先要imagepng($code,路径)
然后再从路径读图片转为base64编码,有没有办法直接用 $code这个参数直接转换为base64编码的图片?据查这个$code参数就是图片流,我就是想不通过写出文件的步骤直接转为base64编码,求指教。

参考技术A 使用php对图片进行base64解码输出
$img = "test.jpg";
$base64_img = base64EncodeImage($img);
echo "";
function base64EncodeImage ($image_file)
$base64_image = "";
$image_info = getimagesize($image_file);
$image_data = fread(fopen($image_file, "r"), filesize($image_file));
$base64_image = "data:" . $image_info["mime"] . ";base64," . chunk_split(base64_encode($image_data));
return $base64_image;

?>

以上是关于php 图片流 转为base64的主要内容,如果未能解决你的问题,请参考以下文章