我一直致力于加载 PNG 文件的图像处理功能。虽然它在我的本地服务器(Windows、PHP 5.3.6)和远程服务器(FreeBSD、PHP 5.3.6)上运行良好,但在使用 imagecreatefrompng 时,PHP 返回“500 内部服务器错误”。通常,我会查阅/var/log/php.log 中的日志 - 但当我尝试加载页面时,日志中没有添加任何内容。如果我注释掉包含 imagecreatefrompng 的行,脚本将加载 - 尽管由于缺少图像资源而将许多错误输出到日志中。
我试过添加:
ini_set('display_errors', 1);
error_reporting(E_ALL);
代码,没有区别。
phpinfo的输出显示GD已加载,gd_info的输出为:
array(12) {
["GD Version"]=>
string(27) "bundled (2.0.34 compatible)"
["FreeType Support"]=>
bool(true)
["FreeType Linkage"]=>
string(13) "with freetype"
["T1Lib Support"]=>
bool(true)
["GIF Read Support"]=>
bool(true)
["GIF Create Support"]=>
bool(true)
["JPEG Support"]=>
bool(true)
["PNG Support"]=>
bool(true)
["WBMP Support"]=>
bool(true)
["XPM Support"]=>
bool(true)
["XBM Support"]=>
bool(true)
["JIS-mapped Japanese Font Support"]=>
bool(false)
}
在这一点上,我完全迷路了。没有核对我的 php 安装并重试,我不知道该怎么做。我计划在端口更新后升级到 5.4,但考虑到我无法确定问题所在 - 我不知道这是否能解决问题。
这是相关代码的片段:
/* load image */
$imgname = '/images/'.$doctype.'_'.side($_GET['side']).".png";
$image_path = trim(shell_exec('pwd')).$imgname;
if(!file_exists($image_path)){
trigger_error('Image file not found!');
exit;
}
$im = imagecreatefrompng($image_path);
我已经尽力确保我使用的是图像的绝对路径,仔细检查图像是否存在 (file_exists()),如果不存在则报错。无论我做什么,只要 imagecreatefrompng 未被注释,我就会收到无声的 500 错误。
编辑:我添加了片段:
var_dump(is_readable($image_path));
var_dump(file_exists($image_path));
我的代码导致:
bool(true) bool(true)
所以看起来图像文件可以被 PHP 读取。我还验证了该图像是一个实际的 PNG 文件。还有其他想法吗?
最佳答案
你检查过文件权限了吗?
if(!file_exists($path) || !is_readable($path))....
有时它让我着迷。
而且,我不是那个,但是......你在那之前检查它是否真的是一个 png 文件??
关于PHP GD 静默失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9593326/