2011-04-12 83 views
10

我想通过使用一个img标签打印一个图像,其中src是一个php文件,该脚本还将在服务器scirpt中处理一些操作。任何人都有一个示例代码?一个php文件作为img src

这里是我的测试代码,但没有效果

img.html

<img src="visitImg.php" /> 

visitImg.php

<? 

header('Content-Type: image/jpeg'); 

echo "<img src=\"btn_search_eng.jpg\" />"; 

?> 

回答

5

UPDATE

什么作为评论下面说你可以不用使用include

试试这个:

<? 
$img="example.gif"; 
header ('content-type: image/gif'); 
readfile($img); 
?> 

上面的代码是从this网站

原来的答案
不要试试这个:

<? 

header('Content-Type: image/jpeg'); 

include 'btn_search_eng.jpg'; // SECURITY issue for uploaded files! 

?> 
+0

甚至更​​好:'ReadFile的( 'btn_search_eng.jpg')'它它刷新直接到输出。 – 2011-04-12 04:05:48

+1

我想知道是谁投了票。包含图像文件。真? – mario 2011-04-12 04:07:08

+0

-1,因为在图像上清楚地包含''将不起作用。 'include'用于包含PHP代码。此外,这是使用short_tags,这是不推荐的,并且不会在许多服务器上工作。 – 2011-04-12 04:09:07

1

如果你想在你的脚本的顶部使用header('Content-Type: image/jpeg');,那么你的脚本的输出最好是JPEG图像!在您当前的示例中,您正在指定图像内容类型,然后提供HTML输出。

1

你回应的是HTML,而不是生成JPEG图像所需的二进制数据。要做到这一点,您需要读取外部文件或使用PHP的图像处理函数生成文件。

6

直接从php fpassthru docs

<?php 

// open the file in a binary mode 
$name = './img/ok.png'; 
$fp = fopen($name, 'rb'); 

// send the right headers 
header("Content-Type: image/png"); 
header("Content-Length: " . filesize($name)); 

// dump the picture and stop the script 
fpassthru($fp); 
exit; 
?> 

作为一个解释,你需要从脚本传递图像数据作为输出, HTML数据。您可以使用其他功能,如fopenreadfileetc etc etc

11

使用readfile

<?php 
header('Content-Type: image/jpeg'); 
readfile('btn_search_eng.jpg'); 
?>