如何将字节数组中的图像与编码器中的其他内容一起包含在编码器中


How to include the image from byte array with other content in codeigniter?

>我正在尝试使用字节数组中的图像制作用户的配置文件视图。我已经成功渲染了图像,但是由于将整个文件作为图像作为图像的header('Content-Type: image/png'),因此在包含图像和其他配置文件内容时遇到问题。如何解决这个问题?

$myArray = implode('', array_map(function($e) {
    return pack("C*", $e);
}, $image));
if      (substr($myArray, 0, 4) == "'x89PNG") header('Content-Type: image/png');
else if (substr($myArray, 0, 2) == "'xFF'xD8") header('Content-Type: image/jpeg');
else if (substr($myArray, 0, 4) == "GIF8") header('Content-Type: image/gif');
echo $myArray;

您不能将原始图像数据作为 HTML 文档的一部分提供。

您可以通过多种方式提供图像:

  1. Base64 对其进行编码并将其嵌入到 img src 中。有关示例,请参阅此线程。
  2. 将图像保存到文件中,并像提供任何其他图像一样提供它。

#2 的优点是允许浏览器缓存图像。如果图像很大,我会尝试找出一种方法将其保存到文件中。