【发布时间】:2012-02-15 16:16:11
【问题描述】:
我有一个类有几个可以渲染图像的函数。
// render.php
class Render {
public function Render($some_arguments) {
...
header("Content-Type: image/png");
$im = @imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
return "data:image/png;base64,".base64_encode($im);
}
}
然后我的 php 文件包含 html 代码以及我想在 < img> 标签内输出图像的位置:
// index.php
include("render.php");
$render = new Render();
echo "<htlm><head></head><body>";
echo "<img src=\"".$render->Render(1)."\" />";
echo "</body></html>";
当我在浏览器中运行 index.php 时,我只是得到一个空白屏幕。
我不能使用函数调用作为图像源吗?我知道我可以使用 php 文件作为源,比如< img src="render_image.php" />,但是我不能以面向对象的方式发送任何参数(我知道我可以使用 $_GET 来检索参数),但我想用不错的面向对象编写的代码。
那么,有没有什么方法可以使用函数调用作为 html 标签的来源?
【问题讨论】:
-
您应该得到无法正常工作的垃圾,而不是空白屏幕。提出 error_reporting 或查看 error.log。
->Render()函数也会输出 img 数据,而不是返回它。首先处理它而不是对象接口。