我有一个文件,在将图像输出到浏览器之前对其进行操作。下面是代码:
<?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 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功能的服务器需求,所以我需要的是一些方向,我应该看看在这种情况下可能的罪魁祸首。是什么导致它断裂的?
在发送带有文件类型的头文件之前尝试清空缓冲区:
// clean the output buffer
//http://www.php.net/manual/en/function.ob-clean.php#75694
ob_clean();
header("Content-type: image/jpeg");