每当我尝试使用PHP提供图像时,它都会声明图像已损坏/损坏,并在Google中发出以下警告:
解释为文档但使用MIME类型映像/png传输的资源
然而,情况如下:
img解码的图像放在base64 src中,它们就会非常好地工作。我尝试过的例子:
$imgpath = 'assets/img/dropdown-arrow.png';
$type = pathinfo($imgpath, PATHINFO_EXTENSION);
$data = file_get_contents($imgpath);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
header('Content-Type: image/png;'); // also tried with charset=UTF-8 and such
echo base64_decode($base64);
exit();示例2(在其他服务器上工作的示例):
// Set the content type header - in this case image/png
header('Content-Type: image/png; charset=UTF-8');
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($img, 0, 0, 0);
// removing the black from the placeholder
imagecolortransparent($img, $background);
// turning off alpha blending (to ensure alpha channel information
// is preserved, rather than removed (blending with the rest of the
// image in the form of black))
imagealphablending($img, false);
// turning on alpha channel information saving (to ensure the full range
// of transparency is preserved)
imagesavealpha($img, true);
// Output the image
imagepng($img);--所以,又一次,我已经把我的框架剪掉了,从index.php的第一行中直接运行。
因此,出于某种奇怪的原因,当我将content-type设置为image/png;时,一切都在走下坡路。
有人能知道为什么会发生这种事吗?这是我的代码遗漏的东西吗?这是我无法用代码(服务器端)修复的东西吗?我是不是错过了一些非常明显的东西?
基本服务器信息:
我使用PHP在Apache2.4.5上运行。(虽然切换到PHP没有改变什么)在PHP 7.2.3上
响应头:
连接:保持活动
Content-Type:图像/png
日期:2018年7月13日,星期五19:51:37
保持活力: timeout=5,max=99
服务器: Apache/2.4.25 (Debian)
Transfer-Encoding:块
更新/修复
显然,有人(不是我,真的!)在MVC中包含的一个小文件中,在<?php标记之前放置一个空格。删除那个空间解决了每一个问题。找到它花了相当长的时间,所以,对于任何阅读的人来说,教训都是:确保您的代码格式整洁,并且始终将<?php标记放在最开始的位置。
发布于 2018-07-13 20:19:11
您可以直接输出它,不需要执行base64 64_encode/decode
$imgpath = 'assets/img/dropdown-arrow.png';
$data = file_get_contents($imgpath);
header('Content-Type: image/png;');
echo $data;
exit();发布于 2018-07-13 20:22:41
在示例2中,标头表示您的数据是utf8编码的。为什么?您的数据是二进制文件,没有utf8编码。
https://stackoverflow.com/questions/51332046
复制相似问题