我在使用 php header 时遇到了困难,我试图在浏览器中创建任何类型的图像而不将其保存在文件中,但无论我做什么,我都无法让它工作。
例如,如果我使用 php 手册中的这个基本示例:
$im = imagecreate(100, 100);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
它会设置正确的标题,甚至输出找不到图像时得到的图标。
我还使用启用了 GD 的 xampp
,这是我的 phpinfo
GD 部分:
GD Support enabled
GD Version bundled (2.0.34 compatible)
FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.4.3
GIF Read Support enabled
GIF Create Support enabled
JPEG Support enabled
libJPEG Version 6b
PNG Support enabled
libPNG Version 1.2.46
WBMP Support enabled
XBM Support enabled
我的代码是错误的还是我需要配置一些东西?
最佳答案
您不仅应该创建图像,还应该以某种方式填充它:
error_reporting(E_ALL);
ini_set('display_errors', 1);
$im = imagecreate(100, 100) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
关于php header ,内容类型 png 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9500836/