I have a file that manipulates images before they are output to the browser. Here is the code:

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


?>

As of a few days ago, whenever I try to load any images through this file, I get The image {path-to-file} cannot be displayed because it contains errors.

I've searched for answers to this issue and it seems just about everybody who gets this error is doing so because they're outputting stuff before header(), which is not the problem in my case. I know something must have changed server-side because this was working fine for a long time, and then suddenly it just stopped working. I tested the script on another server and it worked fine.

I ran a GD support test script on the server with these results:

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

The main problem I have is that I really don't know enough about server requirements for PHP GD functionality, so what I need is some direction as to what I should look at as possible culprits in this case. What could possibly have caused this to break?

有帮助吗?

解决方案

Before sending the header with the file type try clearing the buffer:

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

header("Content-type: image/jpeg");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top