【问题标题】:Created image doesn't display in the browser创建的图像不显示在浏览器中
【发布时间】:2018-04-28 20:01:23
【问题描述】:

我已经在我的PHP Version 5.6.32 服务器上安装了 GD。

GD Support  enabled
GD headers Version  2.2.4
GD library Version  2.2.4
FreeType Support    enabled
FreeType Linkage    with freetype
FreeType Version    2.8.0
GIF Read Support    enabled
GIF Create Support  enabled
JPEG Support    enabled
libJPEG Version unknown
PNG Support enabled
libPNG Version  1.6.29
WBMP Support    enabled
XPM Support enabled
libXpm Version  30411
XBM Support enabled

这是创建图像的函数:

  // Print a bar image
  static function printBarImage($color, $width, $height) {
    // Create a blank image
    $image = imagecreatetruecolor($width, $height);

    if (strlen($color) == 6) {
      $color = "#" . $color;
    }

    $r = intval(substr($color, 1, 2), 16);
    $g = intval(substr($color, 3, 2), 16);
    $b = intval(substr($color, 5, 2), 16);
    $color = imagecolorallocate($image, $r, $g, $b);

    // Fill up the image background
    imagefilledrectangle($image, 0, 0, $width, $height, $color);

    // Header indicating the image type
    header("Content-type:image/jpeg"); // <<== This is the line 467

    // Create the image in the best jpeg quality
    imagejpeg($image, NULL, 100);

    // Destroy the image
    imagedestroy($image);
  }

上面这个函数是由脚本调用的:

<?PHP

require_once("website.php");

ob_end_flush(); // <<== Added for debugging purposes

$color = LibEnv::getEnvHttpGET("color");
$width = LibEnv::getEnvHttpGET("width");
$height = LibEnv::getEnvHttpGET("height");

$color = urldecode($color);

if ($color && $width > 0 && $height> 0) {
  LibImage::printBarImage($color, $width, $height);
}

?>

但它不会显示任何图像。

你可以在http://www.thalasoft.com/engine/system/utils/printBarImage.php?color=%2392299FF&amp;width=30&amp;height=100看到它的实际应用

我的phpinfo 页面显示default_charsetUTF-8

但文件在字符集中:

$ uchardet system/utils/printBarImage.php 
ascii/unknown

文件不包含 BOM 字符:

$ head -c 3 system/utils/printBarImage.php | hexdump -C
00000000  3c 3f 50                                          |<?P|
00000003

函数imagepngimagejpeg 也会出现同样的问题。

我知道如果我将函数调用为imagepng($image, "/usr/bin/learnintouch/engine/image.jpeg");,则创建图像,然后它会在文件系统上创建/usr/bin/learnintouch/engine/image.jpeg 文件,我可以在另一个浏览器选项卡中打开它并查看图像。

即使保存了图像,以后也无法读取,没有图像显示:

//imagejpeg($image, "/usr/bin/learnintouch/engine/image.jpeg", 100);
readfile("/usr/bin/learnintouch/engine/image.jpeg");

按照 Phil 的建议,我添加了一个 ob_end_flush(); 调用,它在我的开发环境中给出了以下错误(源代码中上面也标记了行号):

Error message: Cannot modify header information - headers already sent by (output started at /usr/bin/learnintouch/engine/system/utils/printBarImage.php:5)
Filename: /usr/bin/learnintouch/engine/lib/image.php
Line: 467

http://www.thalasoft.com/engine/system/utils/printBarImage.php?color=%2392299FF&amp;width=30&amp;height=100的公共测试环境中显示乱码。

更新:评论解决方案...我将ob_end_clean(); 函数调用添加到脚本文件中,然后图像显示正常。脚本文件现在显示:

<?PHP

require_once("website.php");

// Prevent any possible previous header from being sent before the following image header that must be the first
ob_end_clean();

$color = LibEnv::getEnvHttpGET("color");
$width = LibEnv::getEnvHttpGET("width");
$height = LibEnv::getEnvHttpGET("height");

$color = urldecode($color);

if ($color && $width > 0 && $height> 0) {
  LibImage::printBarImage($color, $width, $height);
}

?>

【问题讨论】:

  • 您的输出开头似乎有一些无效字符 ~ “不是 JPEG 文件:以 0x0a 0xff 开头”。似乎是换行符。调用 printBarImage 函数的 PHP 脚本是什么样的?
  • 请记住,图像在保存并在另一个浏览器选项卡中打开时可以正常显示。
  • 对,所以不需要的字符来自您的 printBarImage 函数之外的某个地方
  • 如果我从网络上的某个页面下载 jpeg 图像,并尝试在此功能中显示它,它不会。我怀疑我的 PHP 服务器的字符集与我的 PHP 源代码文件之一不同...
  • 我认为这不太可能。很可能您在&lt;?php 标签之前或?&gt; 之后的某处显示了一些空格(尽管您已经排除了BOM,所以这很好)。我强烈建议您在所有脚本中使用omit the closing PHP tag

标签: php gd content-type


【解决方案1】:

这里有点随意,但这就是我想象的正在发生的事情。

鉴于您的 printBarImage 函数是 static,我猜它是类的一部分。我也猜想这个类在它自己的文件中,它看起来像这样

<?php
// ImageUtils.php
class ImageUtils {
    static function printBarImage($color, $width, $height) {
        // ...
    }
}
?>

我想你的printBarImage.php PHP 文件看起来像

<?php
require_once 'ImageUtils.php';
ImageUtils::printBarImage($_GET['color'], $_GET['width'], $_GET['height']);

问题很可能是您的类 .php 文件有一些尾随空格。

特别是(带有空白注释)

?>$
$

因此,您的 printBarImage.php 脚本会在顶部、二进制图像数据之前输出一个换行符。

解决方案是在所有.php 文件的末尾添加omit the final closing PHP tag


我还猜测您的 PHP 环境启用了输出缓冲。我不推荐这个。如果你是disable it,你可能会看到类似的东西

警告:无法修改标头信息 - 标头已由 ImageUtils.php 中 header(...)>

中的 ImageUtils.php 中的(输出开始于 ImageUtils.php:)发送

即使我对空白字符的位置有误,您仍应尝试禁用输出缓冲以帮助查明问题。

【讨论】:

  • 我查看了代码库,并没有看到其中使用了任何ob_... 函数。
  • 请注意,相同的代码库可以很好地部署在另一台服务器上。当然,这并不排除在此环境中错误添加的空格。
【解决方案2】:

下载“图像”流后,您的“图像”流前面似乎有一个额外的数据。

看起来你的脚本在图像之前发送了一些东西。

我的怀疑在于三个地方:

1。您的 &lt;?php 前面可能有额外的空格

<?php ?> 清除所有不必要的空格

2。你的include("website.php"); 可能输出了一些东西

检查您包含的文件的输出

3。 ob_end_flush() 正在发射缓冲区中的任何内容

如果你想丢弃任何缓冲区,你应该使用ob_end_clean()

【讨论】:

  • 使用ob_end_clean() 函数可以显示图像。确实,在包含的包中一定有一些输出偷偷溜出。
猜你喜欢
  • 2021-11-25
  • 2021-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
  • 2016-02-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多