创建的图像不显示在浏览器中

Created image doesn't display in the browser

我已经在我的 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&width=30&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&width=30&height=100的public测试环境中显示乱码。

更新:评论解决方案...我在脚本文件中添加了 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);
}

?>

这里有点冒失,但这就是我想象中正在发生的事情。

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

<?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']);

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

具体(带空格注释)

?>$
$

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

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


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

的东西

Warning: Cannot modify header information - headers already sent by (output started at ImageUtils.php:<last line>) in ImageUtils.php on line <line with header(...)>

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

下载您的 "image" 流后,您的 "image" 流前面似乎有额外的数据。

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

我的怀疑在三个地方:

1。你的 <?php

前面可能有多余的空格

<?php ?> 清除任何不必要的空格

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

检查包含的文件的输出

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

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