Forum Moderators: coopster

Message Too Old, No Replies

Using PHP in an HTML image tag

images outside of web path

         

jrisken

3:26 am on Sep 11, 2008 (gmt 0)

10+ Year Member



I’ve looked in a pot-full of forums for the answer to this question:
How can I get the ‘src’ parameter of an HTML ‘img’ tag to refer to an image that is not in the web site’s path? I wanted to be able to do this so that images could be more secure and wouldn’t need to be replicated for multiple subdomains.

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

Anyango

4:49 am on Sep 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Create another file for Images,

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]

jrisken

5:10 am on Sep 11, 2008 (gmt 0)

10+ Year Member



One could certainly put the code in a separate file. I chose not to because I want as little as possible downstream. In fact, index.php is the only file visible to the world.

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.

Anyango

5:39 am on Sep 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's cool information that header stuff is not necessary, i never knew it would "always" work even without sending an image content-type header if you are echoing an image file.

Anyango

5:42 am on Sep 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And if everything is working then what's the question

jrisken

1:33 pm on Sep 11, 2008 (gmt 0)

10+ Year Member



Oh. No question. I spent about three hours trying to find all the pieces, and I wanted to share what I learned so that if someone else was looking they wouldn't have to spend three hours.