php:从二进制数据重新创建和显示图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2070603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:54:25  来源:igfitidea点击:

php: recreate and display an image from binary data

phpimagebinaryfiles

提问by Cambiata

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...

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

回答by Ben James

You can do this using a data URIin the image srcattribute.

您可以使用图像属性中的数据 URI来执行此操作src

The format is: data:[<MIME-type>][;charset="<encoding>"][;base64],<data>

格式为: data:[<MIME-type>][;charset="<encoding>"][;base64],<data>

This example is straight from the Wikipedia page on data URIs:

这个例子直接来自维基百科关于数据 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" />

回答by Pekka

This is actually possible using inline images (called data URIs).

这实际上可以使用内联图像(称为数据 URI)。

Your image tag would look something like this:

你的图像标签看起来像这样:

<img src="data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub/
/ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcpp
V0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7" 
width="16" height="14" alt="embedded folder icon">

Why they are mostly not a good idea:

为什么它们大多不是一个好主意:

  • Page load will be slowed downbecause the image needs to be fetched beforethe full HTML structure can be loaded and thus, rendered. Even more so if you are performing additional operations on the image. Your site will very likely feel much slower than if it were an external image.

  • Inline images need to be base64 encoded, adding 33% to their size.

  • 页面加载会减慢,因为在加载完整的 HTML 结构并呈现之前需要获取图像。如果您对图像执行其他操作,则更是如此。与外部图像相比,您的网站很可能会感觉慢得多。

  • 内嵌图像需要进行 base64 编码,将其大小增加 33%

If you are talking about a reasonable high-traffic, public site, I would recommend you store your image externally, and cache them. If it's just for a small project, inline images may work for you.

如果您谈论的是一个合理的高流量公共站点,我建议您将图像存储在外部并缓存它们。如果它只是一个小项目,内嵌图像可能适合你。

回答by Krab

Other possibility for you is to create a script producing the image data to the output and direct the link to it.

您的另一种可能性是创建一个脚本,将图像数据生成到输出并将链接指向它。

image.php

图片.php

$imagedata1 = file_get_contents('assets/test.png');
$imagedata1 = process_using_gd_or_something($imagedata1);

header('Content-type: image/png');
echo $imagedata1;

other_pages.php:

other_pages.php:

echo "<img src='image.php?some_params'>";

EDIT: Sorry, I missed the notice of not wanting an external script, but this solution is more efficient than encoding the image to base64.

编辑:抱歉,我错过了不想要外部脚本的通知,但此解决方案比将图像编码为 base64 更有效。

回答by eildiz

Try this...

尝试这个...

$img=base64_encode($row['PICTURE']);

<img alt="105x105" class="img-responsive" src="data:image/jpg;charset=utf8;base64,<?php echo $img ?>"/>

回答by riyad

In case you just want the image, without any html around it you can use the following:

如果您只想要图像,而周围没有任何 html,则可以使用以下内容:

$filename = 'assets/test.png';
$original_image = file_get_contents($filename);
$processed_image = process_the_image_somehow($original_image);

header('Content-type: '.mime_content_type($filename));
header('Content-Length: '.strlen($processed_image));
echo $processed_image;

You must not forget the Content-Length header, otherwise it won't work. You may also want to replace mime_content_type() as it is deprecated according to the docs.

您一定不要忘记 Content-Length 标头,否则它将无法工作。您可能还想替换 mime_content_type() ,因为它根据文档已被弃用。