2011-03-11 54 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'); ?>" />