2011-03-11 55 views
1

可能重复:
Want to render an image without saving it to disk using PHP GD libs
The image cannot be displayed because it contains errors.PHP - 从IMG SRC调用一个函数

我想调用一个函数,使一个图像trought的IMG SRC标签。可能吗?

我的意思是:不是呼叫<img src="path/file.php" />的我想要做像出头<img src="function()" />

+0

从技术上讲,可以使用'数据:'URI的,但它是一个可怕的想法。用什么@Sjoerd建议 – 2011-03-11 16:05:38

+1

看到一个可能的解决方案在http://stackoverflow.com/questions/3385982/the-image-cannot-be-displayed-because-it-contains-errors/3386050#3386050 – Gordon 2011-03-11 16:07:38

回答

3

PHP是服务器端;它可以生成可以作为图像放置的base64编码图像结果,也可以指向将生成图像的php脚本。但是,关于客户端,它不会工作。

所以,你可以做到以下几点:

// the browser will make a call to your generator to render an image back 
echo '<img src="myimagegenerator.php" />'; 

// src will be something like "data:image/png;base64,..." 
echo '<img src="'.generateImage().'" />'; 
+1

@戈登:语义,但完成。 ;-) – 2011-03-11 16:12:17

0

但是,您可以使用URL“thispage.php makeimage = 1?”并调用该函数如果$_GET['makeimage']包含1,则输出图像。

+0

-1因为它**是**可能的数据URI – Gordon 2011-03-11 16:06:14

1

在HTML5中,你可以把base64编码的图像源的图像标签。你只需要一个函数来返回它。

function getImage($file){ 
     return 'data:image/gif;base64,' . base64_encode(file_get_contents($file)); 
} 

你的img标签:

<img src="<? echo getImage('path-to-image.gif'); ?>" />