1

我有一个文件在将图像输出到浏览器之前对其进行操作。这是代码:

<?php
require_once($_SERVER["DOCUMENT_ROOT"].'/_content/_constants.php');
require_once($_SERVER["DOCUMENT_ROOT"].'/_content/_functions.php');

    function hex2rgb($hex) {
       $hex = str_replace("#", "", $hex);

       if(strlen($hex) == 3) {
          $r = hexdec(substr($hex,0,1).substr($hex,0,1));
          $g = hexdec(substr($hex,1,1).substr($hex,1,1));
          $b = hexdec(substr($hex,2,1).substr($hex,2,1));
       } else {
          $r = hexdec(substr($hex,0,2));
          $g = hexdec(substr($hex,2,2));
          $b = hexdec(substr($hex,4,2));
       }
       $rgb = array($r, $g, $b);
       //return implode(",", $rgb); // returns the rgb values separated by commas
       return $rgb; // returns an array with the rgb values
    }

    /*-- Get the hex color code from the URL query --*/
    $rgb = hex2rgb((isset($_GET["color1"]) && $_GET["color1"] != '' ? $_GET["color1"] : "C9AA14"));

    /*-- Get the image file --*/
    $im = imagecreatefrompng (BASE."/_content/images/".$_GET["img"]);

    /*-- Preserve transparency --*/
    imageAlphaBlending($im, false);
    imageSaveAlpha($im, true);


    //$index = imagecolorclosest( $im,  0,0,0 ); // get original color
        //imagecolorset($im,imagecolorat($im,8,8), $rgb2[0],$rgb2[1],$rgb2[2] ); // SET COLOR OF ICON SYMBOL

    /*-- Apply the colorizing filter --*/
    imagefilter($im, IMG_FILTER_COLORIZE, $rgb[0],$rgb[1],$rgb[2], 0);

    /*-----------------------------------------------
    * If the image has a symbol/foreground 
    * on it, such as the audio/video icons, 
    * this sets the color of that symbol
    *
    * Not needed for single color images
    */
    //imagefilltoborder($im, 18, 21, imagecolorat($im,23,10), imagecolorallocate($im,0,0,0));  


    header("Content-type: image/png");
    imagePng($im);


?>

截至几天前,每当我尝试通过此文件加载任何图像时,我都会得到The image {path-to-file} cannot be displayed because it contains errors.

我已经搜索了这个问题的答案,似乎几乎每个收到此错误的人都在这样做,因为他们之前输出过东西header(),这在我的情况下不是问题。我知道服务器端肯定发生了一些变化,因为它工作了很长时间,然后突然停止工作。我在另一台服务器上测试了脚本,它运行良好。

我在服务器上运行了一个 GD 支持测试脚本,结果如​​下:

GD is supported by your server!
GD Version          Yes
FreeType Support    Yes
FreeType Linkage    Yes
T1Lib Support       No
GIF Read Support    Yes
GIF Create Support  Yes
JPEG Support        Yes
PNG Support         Yes
WBMP Support        Yes
XPM Support         No
XBM Support         Yes
JIS-mapped Japanese Font Support    No

我遇到的主要问题是我对 PHP GD 功能的服务器要求了解得不够多,所以我需要的是一些方向,即在这种情况下我应该将哪些问题视为可能的罪魁祸首。是什么可能导致它破裂?

4

1 回答 1

3

在发送带有文件类型的标头之前,请尝试清除缓冲区:

// clean the output buffer
//http://www.php.net/manual/en/function.ob-clean.php#75694
ob_clean();

header("Content-type: image/jpeg");
于 2013-05-09T17:42:33.200 回答