PDA

View Full Version : Dynamic Random Image



Mentor
09-06-2005, 05:08 PM
Ok, some people may have been wondering how my image in my sig changed. When you cant put a randomize script in your sig.

Well its actly quite simple as the randomiser is actly in the image.

so isnetd of banner.gif
you have banner.php

It then genirates the headers to tell the computer loading it, that it is an image, and not a php script.

the script below only works with .gif 's athogh you could cahnge this.

to ad more images, simply ad to the array, and chnage the currant image in the array to the images you want in the randomize.

hope this helps some one


<?php

// tells it its a gif image
Header("Content-type: image/gif");

// dates back a bit (so dosnt go in cache, so peeps actaly see chnages)
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

// ye be modifed always (so dosnt go in cache, so peeps actaly see chnages)
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

// HTTP/1.1 - no (so dosnt go in cache, so peeps actaly see chnages)
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

// HTTP/1.0 - no (so dosnt go in cache, so peeps actaly see chnages)
header("Pragma: no-cache");

//image arrays
// just ad decres it to remove images, also chnage image1.gif etc to images you wnat to dimainc image to randomize "/
$stuff = array (
0 => "image1.gif",
1 => "image2.gif",
2 => "image3.gif",
);
$display=rand(0, sizeof($stuff)-1);

//include it and done
include($stuff[$display]);

?>



Exstendered

Ok you have a dinamic image, but you want to use it on a forum. BUT the forum only supports valide image types, gif, jpg, png etc?

Well with a bit of help from .htaccss files its possible to run file types as other file types.

Heres an exsample of a finshed part
http://www.thybag.co.uk/php/speacal/myimage.png

The file type in PNG. yet refresh it, and it chnages like the php exsample

OK how do i do this. Well its axtly quite simple if your host supports .htaccess

As a htaccess effects all files below it, its best to creat a new directy for this, then put the script and the images in it.
Next choose what image typed youd like the php genirator to be, for my exsample i choose png.

creat a new file, called .htaccess

now htaccess can over ride both the apachi handlers and the MIME Types, as well as adding new ones, if you wanted a .pie files for exsample

Anyway to ad the MIME Type, use this format


AddType application/x-httpd-php .png


The bit that says add MIME type is


AddType


The middle is what type to add


application/x-httpd-php

Above ive used the php file type

and the last bit is what exsention to apply it to


.png


This can be used for ANY exstention, and aply any file type.

Say if i was just renaime a .html document this would be all i need. But as im doing this for a php document, the apachi server needs to genrate the php on it, or there would be an error

we add a apchi handler in the same way, exspt with one diffence


AddHandler application/x-httpd-php .png
[code]
that is we use the ad handler command insted of the addtype command.

so in your .htaccess file you should have

[code]
AddType application/x-httpd-php .png
AddHandler application/x-httpd-php .png


Now save this. and then chnage the exstention on the php file with the random banner script on it, to in this exsampe .png .

Now due to the .htaccess all .png file in this directory will be treated as php, and are therefore the script will genriate succefuly and give you the random image.


The only flaw in this is some versions of IE (helpfuly) decide to ignore the information there given, and attempt to treat the file on its own mime file type database, wich can cause errors.

Anderman
09-06-2005, 05:23 PM
Ah cool :D

I thaught it was done by a Java image rotator script

Mentor
09-06-2005, 05:27 PM
Ah cool :D

I thaught it was done by a Java image rotator script
javascript.

Old thybag did, then i upgraded to php, but i coulnt use that in a sig, or on forum, so i then made the image itself dinaimic, as ye can see above :D

:JaMhEd
09-06-2005, 05:28 PM
lol, never noticed that before. nice...

Kroziun
28-06-2005, 07:00 PM
Good one - I too wondered how it was done. :)

-JT-
03-07-2005, 09:19 AM
Ah cool :D

I thaught it was done by a Java image rotator script


i wish people would stop calling javascript JAVA, two different things!!

Anderman
03-07-2005, 09:26 AM
I know now,
I didnt know when i made that post

:-Mystical.Dave-
16-08-2005, 02:28 AM
I was wondering, can you show like a tutorial with pictures, because I dunno how to do this.. so confusing..

Ringz
16-08-2005, 09:45 AM
nice tutorial

Lysine
16-08-2005, 10:10 AM
nice Tutorial
+rep if i can :rolleyes:

Dan Williamson
16-08-2005, 10:21 AM
Nice Tut Mentor +Rep

Want to hide these adverts? Register an account for free!