PHP in a image file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

PHP in a image file

Post by vigge89 »

I want to somehow create a php script, like image randomizer or something, and then save the script as php file, but with png extension, and then when the image should be outputted, it runs the script as a PHP script, like somehow telling the server to execute the file as a php file, and not as an image. So it would just be a PHP file, but with .png extension. The file should then be able to be runned from a different page.
Is this possible?
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

very much so, check out my avatar, its a random image :D.

step 1:
make a httaccess with this code:

Code: Select all

AddType application/x-httpd-php .png
save it as ".htaccess"


step 2:
put the code below in notepad and save it as "*.png", * = file name that u want.
make sure you define a correct path to a images folder for $folder!!!!!

Code: Select all

<?php
Header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); 
Header("Expires: Thu, 11 May 1983 08:52:00 GMT"); 
Header("Pragma: no-cache"); 
$folder = "."; 
/* no need to edit the settings below */ 
if($handle = opendir($folder)) 
{ 
while($files = readdir($handle)) 
{ 
if ($files != "." 
&& $files != "..") 
{
$g[] = $files;
} 
}
}
$t = array_rand($g);
header("Location: $g[$t]");
?>
step 3:
upload both files to your server and go to http://www.yoursite.com/imagefile.png.

hope that helps.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

but if I have other png-files, will they also be processed with PHP, and they will have no output?

I currently do not have any, but i just wanna know ;)
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

ALL files in $folder's path will be processed, i suggest you only keep .png files in there.

but if you want to only process .png gile then use this:

Code: Select all

<?php
Header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); 
Header("Expires: Thu, 11 May 1983 08:52:00 GMT"); 
Header("Pragma: no-cache"); 
$folder = "."; 
/* no need to edit the settings below */ 
if($handle = opendir($folder)) 
{ 
while($files = readdir($handle)) 
{ 
if ($files != "." 
&& $files != ".."
&& strstr($files, ".png")) 
{ 
$g[] = $files; 
} 
} 
} 
$t = array_rand($g); 
header("Location: $g[$t]");
?>
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

well, i just want to process ONE png file as PHP, and that one should be the script. All the others should just be normal Png-images
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

I'd suggest using mod_rewrite for Apache.
http://httpd.apache.org/docs/misc/rewriteguide.html
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

well, if there isn't another way to do it, i'll just stick to using no ther png's.

Oh, btw, is there possible to just assign
"AddType application/x-httpd-php .png"
in httaccess to only one directory?
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Post by Linkjames »

The htaccess should only effect the folder you place it in. Just use a seperate folder for the .png you want to run as .php
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

ok, that's what i wanted to know, now i just have to wait for my 3rd host-change in this 2 last months :/
Last edited by vigge89 on Sat Nov 15, 2003 11:33 am, edited 1 time in total.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

put your .png file (that is actually php) in the main folder (/new) and then make an imgs folder, and put the ones u want to be processed as images, in a new folder (/new/imgs/).
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

LiLpunkSkateR wrote:put your .png file (that is actually php) in the main folder (/new) and then make an imgs folder, and put the ones u want to be processed as images, in a new folder (/new/imgs/).
I know, that's what i currently have
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

Well, when run image.png, i sometimes get re-directed to the .htacces file, i've got 2 gif-files in the same dir and folder set to ".".
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

oh, got it to work, just added
&& $files != ".htaccess"
in the script :D, thanks all
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

hmm, when using the script in signature in IPB, it only displays one img (it doesn't dandomly pick one, just the same all the time).

Why won't it work?

maybe it's the header:location?
have IPB disabled it or something
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

does it work here?
testing right now

Edit: no it doesn't work here or on IP boards either, why?
Well, it works here, but the image seems to get cached, so i have to reload the pge lots of times before another one shows up...
Post Reply