Authored by Vince Russell on 06 September 2005 at 07:03PM
Current rating: 
Filed under:
PHP/ASP Coding
Using ASP or PHP pages in image tags

The ability to use an ASP or PHP file as the source of an image can be extremely useful! It's something that I use on my live Halo 2 ranking and allows me to programmatically determine the image I want to display.
You could use it to display certain banners on your website, or perhaps change an image on your site to coincide with the time of day (yeah, like that'll be useful!), or maybe you want to return a different client logo based on a login? All pretty useful stuff!
So how do you do it? I thought you'd never ask!
What do we actually need to do?
- We want to be able to use tags of the form
<img src="return_an_image.asp" />to display our dynamically determined image. - Normally, webpages return content of type
text/html, however in this case we want to return an image so we'll need to change the content type of the page to something likeimage/jpegorimage/gif. - I'm assuming that the file we want to return is stored as a file, so we need to read this file and return it as binary content.
The PHP solution
Doing this in PHP is very simple, this is all the code you need...
<?php
// Set the name of the image we want to return (how you set this is up to you)
$img_name = '/home/httpd/vhosts/testing/images/brilliant.jpg';
// Change the content type of the page
header('Content-Type: image/jpeg');
// Read the file and write it to the output buffer
@readfile($img_name);
// end the execution of the script
exit;
?>
You can find more about readfile from the www.php.net site.
The ASP solution
This is a little more involved than the PHP solution but still just as effective.
<%
dim buf
dim strFilename
' Set the name of the image we want to return (how you set this is up to you)
strFilename = server.mappath("/images/brilliant.jpg")
' Read the binary file in the buffer (ReadBinaryFile source code is given below)
buf = ReadBinaryFile(strFilename)
'Change the content type of the page
Response.ContentType = "image/jpeg"
' Write out the file to the output buffer and end the response
Response.BinaryWrite(buf)
Response.End()
%>
If you also wished to return the size in the header, you could add this line before the BinaryWrite call above
Response.Addheader "Content-Length", LenB(buf)
In the ASP method above, we make a call to the function ReadBinaryFile, this function can be downloaded along with the above examples.
Summary
So that's it! Not as difficult as you may have thought I hope and the amount of things you can do with it is just brilliant! Well, I think so, perhaps I should rethink a special dynamically "time of day" image!
Perhaps not.
Download the sample PHP and ASP files
Authored by Vince Russell on 06 September 2005 at 07:03PM
Current rating: 
Filed under:
PHP/ASP Coding
Have you heard?
Excellent
Poor
Rubbish
Cack
Comments about "Using ASP or PHP pages in image tags"
Vince Russell said
I've just been working on something along these lines and i've found that you can actually use a redirect to another image within the ASP page, so...
<%
response.redirect("image/brilliant.jpg")
%>
..would work in the same way as the stuff above (except you've got the extra "hop")
07 September 2005 at 05:45PM