一个 php 文件作为 img src

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5630266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:02:08  来源:igfitidea点击:

A php file as img src

phpimage-processing

提问by hkvega

i want to print a image by using a img tag which src is a php file, the script will also process some action at server scirpt. Anyone have an example code?

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

here is my test code but no effect

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

img.html

img.html

<img src="visitImg.php" />

visitImg.php

访问Img.php

<?

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

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

?>

回答by Marc Abramowitz

Use readfile:

使用读取文件

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

回答by zzzzBov

Directly from the php fpassthru docs:

直接来自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;
?>

As an explanation, you need to pass the image dataas output from the script, nothtml data. You can use other functions like fopen, readfile, etc etc etc

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

回答by Core Xii

"<img src=\"btn_search_eng.jpg\" />"is not valid image data. You have to actually read the contentsof btn_search_eng.jpgand echo them.

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

See here for the various ways to pass-through files in PHP.

有关在 PHP 中传递文件各种方法,请参见此处。

回答by Naftali aka Neal

UPDATE

更新

what you can do without using includeas said below in the comments:

include如下评论中所述,您可以在不使用的情况下做什么:

try this:

尝试这个:

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

The above code is from thissite

上面的代码是从网站

Original Answer
DON'Ttry this:

原始答案
不要尝试这个:

<?

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

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

?>

回答by Ryan

If you are going to use header('Content-Type: image/jpeg');at the top of your script, then the output of your script had better be a JPEG image! In your current example, you are specifying an image content type and then providing HTML output.

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

回答by Jimmy Sawczuk

What you're echoing is HTML, not the binary data needed to generate a JPEG image. To get that, you'll need to either read an external file or generate a file using PHP's image manipulation functions.

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