PDA

View Full Version : Serving an image in PHP. How?


WA
12-05-2002, 01:17 PM
How does one serve an image using PHP? For example, the IMG tag may reference a php script like so:

<img src="image.php">

with image.php dynamically serving up an image. I was wondering how this is accomplished. I know it's through the use of header() somehow.

Thanks for any info.

firepages
12-05-2002, 01:46 PM
contents of image.php


<?
$img='some.gif';
header("Content-Type: image/gif");
readfile($img);
?>



you can actually lose the header if you wish and it will still work.

WA
12-05-2002, 10:11 PM
Thanks firepages. And there I was trying to use echo() instead of readfile() all this time. :)

WA
12-06-2002, 12:11 AM
BTW, are there other ways of serving an image in PHP, one that's more portable perhaps? From my understanding, readfile() may run into some problems if the filename is a URL and fopen wrappers are disabled. For example for PHP banner scripts that dynamically serve up an image, what's the most common method employed, which usually supports both uploading a banner, and using a complete URL to the image?

firepages
12-06-2002, 01:51 AM
include & fopen etc all also rely on fopen wrappers being available, you can fsockopen the image and read & display but thats a bit much IMO.

the most common method for banner stuff is to use say

<script src="image_gen.php"></script>

image_gen.php would generate document.write('<img src= etc') code etc

then of course you get the dudes with javascript disabled :)