Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A php file as img src

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\" />";

?>
like image 914
hkvega Avatar asked Apr 12 '11 04:04

hkvega


3 Answers

Use readfile:

<?php
header('Content-Type: image/jpeg');
readfile('btn_search_eng.jpg');
?>
like image 103
Marc Abramowitz Avatar answered Nov 03 '22 00:11

Marc Abramowitz


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.

like image 40
Ryan Avatar answered Nov 03 '22 00:11

Ryan


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.

like image 31
Jimmy Sawczuk Avatar answered Nov 03 '22 00:11

Jimmy Sawczuk