Web Hosting Talk







View Full Version : image.php : How?


Studio64
08-28-2002, 11:39 PM
I've heard of this before but, never seen an example that I can recall.

Essentially I'm talking about this

<img src="image.php">


Can something like this be done? Using a php file to return an image to be displayed to the screen.

Someone please correct me if I'm wrong or on the wrong track on this...
Or if it's something that can be done point me to an example.



Sidenote: Similar question different topic

Any example of?

<form action="<?form_check()?>">
<blah blah form crap>
<input type="submit">
</form>


Am I completly wrong on this as well?

Rich2k
08-29-2002, 04:40 AM
Yes it can. All you need to do is get the image source in a string (whether from a database or if you are using it for bandwidth protection) and do the following (example if for a JPEG)

<?php
Header( "Content-type: image/jpeg");
echo $data;
?>

There is a problem with that though because when someone tries to save any of those images they will all be called 'image.jpg' because the script is called image.php. If you are using Apache you can be slightly clever by removing the file extension (and forcing it as PHP using the <File> directive in .htaccess) and putting the query string to make it look like a directory.

e.g. <img src="image/453.jpg">

Where you are actually calling image.php to find id number 453 in the database but as far as web browsers are concerned it's 453.jpg

tanknd
08-29-2002, 09:31 AM
This is a very cool idea... how does one "put" an imagfe into a string if the image is not being stored as a blob in the DB? Thanks.

Rich2k
08-29-2002, 09:59 AM
Open the image as a file and read it in as a string. If the image is local to the server:

<?php
$thumb_file_size = filesize($photolocation);
$open = fopen($photolocation, "r");
$data = fread($open, $thumb_file_size);
fclose($open);
?>


Your image is now in the string $data

You may also want to include some file exist checking above that though.