PHP图像生成器没有任何典型原因导致失败
我有一个PHP文件,它生成一个指定大小的图像以供测试。我猜浏览器认为脚本的输出应该是text/html。到目前为止,我已经安装了php5-gd,并将我的php文件保存为UTF-8,没有BOM,但我仍然存在这个问题。使用readfile()将现有图像发送到浏览器时,图像会正常工作 我的PHP图像生成器:PHP图像生成器没有任何典型原因导致失败,php,apache,ubuntu,gd,mime-types,Php,Apache,Ubuntu,Gd,Mime Types,我有一个PHP文件,它生成一个指定大小的图像以供测试。我猜浏览器认为脚本的输出应该是text/html。到目前为止,我已经安装了php5-gd,并将我的php文件保存为UTF-8,没有BOM,但我仍然存在这个问题。使用readfile()将现有图像发送到浏览器时,图像会正常工作 我的PHP图像生成器: header('Content-Type: image/jpeg'); $im = null; $width = 400; $height = 300; if (isset($_GET['w'])
header('Content-Type: image/jpeg');
$im = null;
$width = 400;
$height = 300;
if (isset($_GET['w']) && isset($_GET['h'])) {
if (is_numeric($_GET['w']) && is_numeric($_GET['h'])) {
$width = $_GET['w'];
$height = $_GET['h'];
}
}
$im = imagecreatetruecolor($width,$height);
$bg = imagecolorallocate($im, 255, 255, 255);
$orange = imagecolorallocate($im, 220, 210, 60);
imagestring($im, 3, 5, 5, 'TEST IMAGE', $orange);
imagejpeg($im);
imagedestroy($im);
PHP.net中的示例代码也不起作用:
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');
// Output the image
imagejpeg($im);
// Free up memory
imagedestroy($im);
正在工作的readfile()方法:
更新:PEBKAC错误。
在安装gd时,我看到Apache服务器在安装过程中重新启动,所以我没有想到自己会重新启动它。事实证明,我需要再次重新启动Apache才能使安装正常工作。只需复制并粘贴代码,即可正常工作,如函数的PHP页面所述: 注意:JPEG支持仅在PHP编译时可用 GD-1.8或更高版本 确保您的PHP至少有GD-1.8 下图显示了您的代码:
谢谢!这帮了我的忙。我尝试在命令行和浏览器中显示gd_info数组,发现浏览器没有显示任何内容,而终端显示了数组。在那之后,我重新启动了Apache,我的问题得到了解决。
header('Content-Type: image/jpeg');
readfile('./site_top/1.jpg');