首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP提供的图像总是损坏

PHP提供的图像总是损坏
EN

Stack Overflow用户
提问于 2018-07-13 19:56:25
回答 2查看 371关注 0票数 1

每当我尝试使用PHP提供图像时,它都会声明图像已损坏/损坏,并在Google中发出以下警告:

解释为文档但使用MIME类型映像/png传输的资源

然而,情况如下:

  1. 这个脚本在多台服务器上运行良好,只是在这个服务器上不运行。
  2. 我使用了多个图像和扩展
  3. 如果我把这些图像作为img解码的图像放在base64 src中,它们就会非常好地工作。
  4. 为了测试目的,我把它放在了我的index.php的第一行,没有工作
  5. 我的文件不是UTF-8 BOM
  6. 我个人看不到任何令人惊讶的标题

我尝试过的例子:

代码语言:javascript
复制
$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(在其他服务器上工作的示例):

代码语言:javascript
复制
// 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标记放在最开始的位置。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-13 20:19:11

您可以直接输出它,不需要执行base64 64_encode/decode

代码语言:javascript
复制
$imgpath = 'assets/img/dropdown-arrow.png';
$data    = file_get_contents($imgpath);
header('Content-Type: image/png;');
echo $data;
exit();
票数 3
EN

Stack Overflow用户

发布于 2018-07-13 20:22:41

在示例2中,标头表示您的数据是utf8编码的。为什么?您的数据是二进制文件,没有utf8编码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51332046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档