【问题标题】:My PHP cannot create a true color image我的 PHP 无法创建真彩色图像
【发布时间】:2016-08-17 15:43:41
【问题描述】:

我有一个用于创建真彩色图像的脚本。

但更换服务器后,新的 PHP 安装不显示图像。

示例如下:http://www.europasprak.com/engine/system/utils/printBarImage.php?color=%239999FF&width=3&height=10

源码为:

<?PHP

require_once("website.php");

$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);
}

  // 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");

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

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

?>

我的 PHP 版本 5.6.20 信息是这样说的:

GD Support  enabled
GD Version  bundled (2.1.0 compatible)
FreeType Support    enabled
FreeType Linkage    with freetype
FreeType Version    2.5.2
GIF Read Support    enabled
GIF Create Support  enabled
JPEG Support    enabled
libJPEG Version 6b
PNG Support enabled
libPNG Version  1.2.50
WBMP Support    enabled
XBM Support enabled

更新:我注释掉了图像输出并添加了一个现有的外部图像进行调试:

//    imagejpeg($image, NULL, 100);
$img=imagecreatefromjpeg("http://www.google.com/logos/doodles/2016/faten-hamamas-85th-birthday-4804306106580992-hp2x.jpg");
imagejpeg($img, NULL, 100);

问题依旧。

所以它可能与我的 GD 设置无关,而与内容类型有关。

我删除了所有 BOM 字符:

tail --bytes=+4 lib/image.php
tail --bytes=+4 system/utils/printBarImage.php

我尝试了不同的字符集:

iconv -f ISO-8859-1 -t utf-8 ... -o ...

编辑:我将我的 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.7.1
GIF Read Support    enabled
GIF Create Support  enabled
JPEG Support    enabled
libJPEG Version unknown
PNG Support enabled
libPNG Version  1.6.28
WBMP Support    enabled
XPM Support enabled
libXpm Version  30411
XBM Support enabled

使用 error_log() 我可以看到 $image = imagecreatetruecolor($width, $height); 函数调用创建了资源 ID #678,$color = imagecolorallocate($image, $r, $g, $b); 函数调用返回颜色代码 6737049。

这个问题还是一样。

我还尝试使用另一个函数代替imagejpeg($image, NULL, 100);,所以我先尝试了imagepng($image);,然后尝试了imagegif($image);,但问题还是一样。

如果我注释掉 header("Content-type:image/jpeg");,那么输出看起来像:����JFIF``�� 和源代码:

����JFIF``��<CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 100
��C��C��
��  
���}!1AQa"q2���#B��R��$3br� 
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������������������������������������������    
���w!1AQaq"2�B����  #3R�br�
$4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������?�@����(��
(��?�

我选择的浏览器是 Chrome。

现在,如果我在 Firefox 中打开图像,它会显示:The image ... cannot be displayed because it contains errors.

编辑:我添加了一些错误报告,日志显示如下:

PHP Parse error:  imagepng(): gd-png error: cannot allocate libpng main struct

【问题讨论】:

  • 你遇到了什么错误?
  • 日志仍然是空的。
  • 您的最后一个代码示例在 PHP 5 中对我来说很好。
  • 在我更改服务器之前,用于以前安装的代码。我认为这是安装或配置问题。不知何故,它无法创建图像。

标签: php php-gd


【解决方案1】:

我将 PHP 升级到 5.6.30 并解决了这个问题。

【讨论】:

    猜你喜欢
    • 2010-12-09
    • 2016-11-25
    • 2012-05-21
    • 2015-07-09
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    相关资源
    最近更新 更多