2011-05-17 82 views
1

一個PHP圖像我想用:中如何產生SRC

src="my_generated_image_from_php.jpg" 

代替

src="my_php_image.php"

我該怎麼辦呢?

my_php_image.php像:

<?php 
    ...... 
    Header('Content-type: image/png'); 
    imagepng($im2); 
    imagedestroy($im2); 
    ......  
?> 
+1

把它們放在同一個目錄下? – Ben 2011-05-17 02:30:26

+0

你的意思是如何讓一個php文件顯示爲.jpg?你將不得不使用mod_rewrite – tlunter 2011-05-17 02:35:28

回答

0

你需要與二進制圖像一起發送圖像頭在你的my_php_image.php文件像這樣:

header("Content-Type: image/jpeg"); 
echo file_get_contents("my_generated_image_from_php.jpeg"); 

顯然,你」我們需要確保圖像的相對路徑正確才能正常工作。否則,只需使用根目錄中的絕對路徑。

如果你這樣做,你將能夠使用第二個選項作爲圖像源。

0

以下是Drupal如何做到這一點。您的網址的圖像看起來像index.php?q=some/path/to/the/image.jpg,但如果你的Web服務器樹立正確的這些簡單的規則:

一旦你做到了這一點作爲BraedenP說
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule^index.php [L] 

現在PHP可以提取與

$request_path = strtok($_SERVER['REQUEST_URI'], '?') 
$base_path_len = strlen(rtrim(dirname($_SERVER['SCRIPT_NAME']), '\/')); 
// Unescape and strip $base_path prefix, leaving q without a leading slash. 
$path = substr(urldecode($request_path), $base_path_len + 1); 

請求路徑你只需要正確的HTTP頭,然後離開你。

1

你可能會想URL重寫(mod_rewrite),這是用來服務於不同的URL的東西。

my_generated_image_from_php.jpeg(隱藏名爲「php」的文件的實際擴展名)可能會得到與my_php_image.php?image=my_generated_image_from_php.jpeg相同的內容。

當涉及到MIME類型和腳本響應的頭部時,如果您可以包含它,這將是一件好事,但這不是實現您所期望的。