PHP HTML图像输出 [英] PHP HTML image output
本文介绍了PHP HTML图像输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 PHP manual
中,用于
In the PHP manual
for base64_encode()
I saw the following script for outputting an image.
<?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
?
But how can you output an image dynamically created with 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 中使用.如何使其跨浏览器?
It seems to work in IE but not Firefox. How can I make it cross-browser?
推荐答案
好,抱歉,我想得太快了:)
Ok, sorry, I was thinking too fast :)
imagepng()
将直接向浏览器输出原始数据流 ,因此必须使用ob_start()
和其他输出缓冲句柄来获取它.
imagepng()
will output raw data stream directly to the browser, so you must use ob_start()
and other output buffering handles to obtain it.
您在这里:
ob_start();
imagepng($yourGdImageHandle);
$output = ob_get_contents();
ob_end_clean();
也就是说-您需要为base64_encode()
函数使用$output
变量.
That is - you need to use $output
variable for you base64_encode()
function.
这篇关于PHP HTML图像输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文