我有一个文件在输出到浏览器之前操纵图像。这是代码:
<?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功能的服务器要求知之甚少,所以我需要的是在这种情况下我应该看哪些可能的罪魁祸首。什么可能导致这种情况破裂?
答案 0 :(得分:3)
在发送带文件类型的标头之前,请尝试清除缓冲区:
// clean the output buffer
//http://www.php.net/manual/en/function.ob-clean.php#75694
ob_clean();
header("Content-type: image/jpeg");