【发布时间】: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&width=30&height=100看到它的实际应用
我的phpinfo 页面显示default_charset 为UTF-8。
但文件在字符集中:
$ uchardet system/utils/printBarImage.php
ascii/unknown
文件不包含 BOM 字符:
$ head -c 3 system/utils/printBarImage.php | hexdump -C
00000000 3c 3f 50 |<?P|
00000003
函数imagepng 或imagejpeg 也会出现同样的问题。
我知道如果我将函数调用为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的公共测试环境中显示乱码。
更新:评论解决方案...我将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 源代码文件之一不同...
-
我认为这不太可能。很可能您在
<?php标签之前或?>之后的某处显示了一些空格(尽管您已经排除了BOM,所以这很好)。我强烈建议您在所有脚本中使用omit the closing PHP tag
标签: php gd content-type