【发布时间】:2012-10-22 07:52:23
【问题描述】:
我从 stackoverflow 中获取的一个简单 PHP 脚本生成一个具有透明背景的 PNG,在其上写入一些文本,然后直接将其输出到客户端浏览器:
$font = 25;
$string = 'My Text';
$im = @imagecreatetruecolor(300, 300);
imagesavealpha($im, true);
imagealphablending($im, false);
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
$red = imagecolorallocate($im, 255, 0, 0);
imagefill($im, 0, 0, $white);
$lime = imagecolorallocate($im, 204, 255, 51);
imagettftext($im, $font, 0, 0, 30, $red, "fonts/tt0588m_.ttf", $string);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
范围是获得一个简单的服务,该服务根据通过 URL 传递给它的参数来提供图像,例如 Google Charts(例如this QR code image)。
到目前为止,一切都很好,除了如果我点击上面代码生成的图像并想保存它,浏览器不会将其识别为 PNG 图像,而是 PHP 脚本 em>(另存为类型选择器仅具有此选项),与 Google Charts 示例相反,其中资源被明确标识为 PNG 文件。
如何实现浏览器正确的资源识别?
【问题讨论】:
-
您是否尝试过使用
Content-Disposition标头来更改建议的文件名? -
您尝试使用
imagettftext访问的字体在您运行它的服务器上可用吗? -
@Frits van Campen 我什么都没试过,我不知道该怎么做 :)
-
是的,字体存在,输出也存在。只是如果我单击“另存为...”,则图像不会被浏览器识别为PNG(显然)。
-
你确定这张图片之前没有任何输出吗?在浏览器中检查您的网络日志以获取响应标头