一个php文件作为img src

hkv*_*ega 10 php image-processing

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

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

img.html

<img src="visitImg.php" />
Run Code Online (Sandbox Code Playgroud)

visitImg.php

<?

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

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

?>
Run Code Online (Sandbox Code Playgroud)

Mar*_*itz 12

使用readfile:

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


zzz*_*Bov 7

直接来自php fpassthru文档:

<?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;
?>
Run Code Online (Sandbox Code Playgroud)

作为解释,您需要将图像数据作为脚本的输出传递,而不是 html数据.您可以使用其他功能,如fopen,readfile,等等等等


Cor*_*Xii 5

"<img src=\"btn_search_eng.jpg\" />"是无效的图像数据.你有实际阅读内容btn_search_eng.jpg和回声他们.

请参阅此处了解在PHP中传递文件各种方法.


Nea*_*eal 5

UPDATE

你可以做什么而不用include如下面评论中所述:

试试这个:

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

上面的代码来自这个网站

原始答案
不要试试这个:

<?

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

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

?>
Run Code Online (Sandbox Code Playgroud)