PHP 图像生成器因任何典型原因而失败

标签 php apache ubuntu gd mime-types

我有一个 PHP 文件,它生成一个指定大小的图像进行测试。我猜浏览器认为脚本的输出是 text/html。到目前为止,我已经安装了 php5-gd 并将我的 php 文件保存为没有 BOM 的 UTF-8,但我仍然遇到问题。使用 readfile() 将现有图像发送到浏览器可以正常工作。

我的 PHP 图像生成器:

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() 方法:
header('Content-Type: image/jpeg');
readfile('./site_top/1.jpg');

更新:PEBKAC 错误。
安装gd的时候看到过程中Apache服务器重启了,所以没想到自己重启。事实证明,我需要第二次重新启动 Apache 才能使安装正常工作。

最佳答案

正如 imagejpeg() 的 PHP 页面上所述,只需复制并粘贴您的代码即可。功能:

Note: JPEG support is only available if PHP was compiled against GD-1.8 or later.



确保你的 PHP 至少有 GD-1.8。

下面的代码显示的图像:

Working Image

关于PHP 图像生成器因任何典型原因而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25220692/

相关文章:

PHP: Signup > USERNAME with only Greek Characters and only one white space

linux - 如何创建自定义 Ubuntu 镜像

java - 服务器 Tomcat v8.5 无法启动 hello world Jersey Web 服务

spring - Pentaho j_spring_security_check 混合内容。 HTTPS 到 HTTP

node.js - 找不到 Node 16.14.2 的模块 'worker_threads'

ubuntu - VirtualBox 上的内核 panic Ubuntu Xenial

php - 在 PHP 中解析逗号分隔文件然后添加到数据库

php - mt_rand() 总是给我相同的数字

php - Magento:创建发票时出现 500 内部服务器错误

wordpress - 使用node-http-proxy时隐藏WordPress URL中的端口号