Forum Moderators: coopster
What I finally pieced together is very straightforward: the seeds of it were in several places, but here’s a working model all in one place.
(By the way, this has been tested with Fireefox, IE7, and Chrome. My web site runs under PHP 5 on an Apache server.)
I decided to keep my images in a folder called ‘help’ that is at a peer level with ‘public_html’. In ‘help’ I have another folder called ‘sample’
Here’s the original image tag:
<img src=”sample/myPicutre.jpg”>
Here’s the modified tag:
<img src=”index.php?helpimage=sample/myPicture.jpg”>
Here’s how my modified index.php file looks
extract($_GET); // will contain the ‘helpimage’ variable
if(isset($helpimage)) // if this is a call for an image
{
$f=pathinfo($helpimage); // and if its an actual image file (hacker-resistance)
if($f['extension']=="jpg")
{
chdir("../help"); // jump up one level to the help folder
readfile($helpimage); // and send the image back to the browser.
return; // that’s it
}
}
… whatever else index.php is supposed to do
Lets say image.php
and put your image processing code there
extract($_GET); // will contain the ‘helpimage’ variable
if(isset($helpimage)) // if this is a call for an image
{
$f=pathinfo($helpimage); // and if its an actual image file (hacker-resistance)
if($f['extension']=="jpg")
{
chdir("../help"); // jump up one level to the help folder
readfile($helpimage); // and send the image back to the browser.
return; // that’s it
}
}
but as this file is sending back JPG content type, use this header on top.
header("Content-type: image/jpg");
and in your index.php you can call the image as
<img src=”image.php?helpimage=sample/myPicture.jpg”>
[edited by: Anyango at 4:53 am (utc) on Sep. 11, 2008]
And the header stuff is NOT necessary. A lot of the forum posts describe that, but a simple readfile seems to be enough. (I imagine Apache is smart enough to know that the request cam from within an image tag and responds appropriately.) In any case, the example works fine without the header() stuff in the browsers I tested and under my Apache server.