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?
here is my test code but no effect
img.html
<img src="visitImg.php" />
visitImg.php
<?
header('Content-Type: image/jpeg');
echo "<img src=\"btn_search_eng.jpg\" />";
?>
"<img src=\"btn_search_eng.jpg\" />"
is not valid image data. You have to actually read the contents of btn_search_eng.jpg
and echo them.
See here for the various ways to pass-through files in PHP.
UPDATE
what you can do without using include
as said below in the comments:
try this:
<?
$img="example.gif";
header ('content-type: image/gif');
readfile($img);
?>
The above code is from this site
Original Answer
DON'T try this:
<?
header('Content-Type: image/jpeg');
include 'btn_search_eng.jpg'; // SECURITY issue for uploaded files!
?>
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.
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.
Directly from the 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;
?>
As an explanation, you need to pass the image data as output from the script, not html data. You can use other functions like fopen
, readfile
, etc etc etc
Use readfile:
<?php
header('Content-Type: image/jpeg');
readfile('btn_search_eng.jpg');
?>