【发布时间】:2011-04-11 08:08:21
【问题描述】:
在PHP manual 的base64_encode() 中,我看到了以下用于输出图像的脚本。
<?php
$imgfile = "test.gif";
$handle = fopen($filename, "r");
$imgbinary = fread(fopen($imgfile, "r"), filesize($imgfile));
echo '<img src="data:image/gif;base64,' . base64_encode($imgbinary) . '" />';
?>
但是如何输出一个用GD动态创建的图像?
我试过了:
$im = imagecreatetruecolor(400, 400);
imagefilledrectangle($im, 0, 0, 200, 200, 0xFF0000);
imagefilledrectangle($im, 200, 0, 400, 200, 0x0000FF);
imagefilledrectangle($im, 0, 200, 200, 400, 0xFFFF00);
imagefilledrectangle($im, 200, 200, 400, 400, 0x00FF00);
echo '<img src="data:image/png;base64,'.base64_encode(imagepng($im)).'" />';
为什么这不起作用?
它似乎在 IE 中有效,但在 Firefox 中无效。我怎样才能使它跨浏览器?
【问题讨论】:
-
不,火狐。嗯,它在 IE 中工作......
-
您是否真的需要使用数据:URI?因为如果你不这样做,最好不要使用它们。他们有很多缺点。例如,IE