php:从二进制数据重新创建和显示图像 [英] php: recreate and display an image from binary data

查看:20
本文介绍了php:从二进制数据重新创建和显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从二进制数据重新创建图像(如果需要,处理它们)并在同一个脚本中显示它们?类似的东西

Is it possible to recreate images from binary data (process them if needed) and display them, all in the same script? Something like

// get and display image 1:
$imagedata1 = file_get_contents('assets/test.png');
$imagedata1 = process_using_gd_or_something($imagedata1);

echo "<img src={$imagedata1} >"; // <-- IS THIS (OR EQUIVALENT) POSSIBLE?

// get and display image 2:
//etc...

我想避免在处理后将图像存储到磁盘并从那里获取它们,或使用外部脚本...

I want to avoid storing the images to to disk after processing and getting them from there, or using an external script...

推荐答案

您可以使用图像 src 属性中的 数据 URI 来做到这一点.

You can do this using a data URI in the image src attribute.

格式为:data:[][;charset=""][;base64],

这个例子直接来自维基百科关于数据 URI 的页面:

<?php
function data_uri($file, $mime) 
{  
  $contents = file_get_contents($file);
  $base64   = base64_encode($contents); 
  return ('data:' . $mime . ';base64,' . $base64);
}
?>

<img src="<?php echo data_uri('elephant.png','image/png'); ?>" alt="An elephant" />

这篇关于php:从二进制数据重新创建和显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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