Image Rotation in PHP
Have a lot of images that you want to rotate on each page refresh? Here’s your solution.
I wrote this because it’s always getting bleedin’ asked in HG&P, and the code in the General Discussion sticky is horrid. However, you don’t have to be a member of OcUK to enjoy randomly-generated imagey goodness.
Directions
Create a new directory on your server, and upload the images you’d like in your rotation to that directory.
Open Notepad, or your favourite text editor.
Paste the following code:
$files = glob('{*.PNG,*.png,*.JPG,*.jpg,*.GIF,*.gif}', GLOB_BRACE); readfile($files[array_rand($files)]);Save the file as
rotate.php, and upload it to the directory that you just uploaded your images to.Make sure it has a
.phpextension, and not.php.txt.Edit your signature on the forums, and paste the link to your rotate.php between image tags, an example being:
[img]http://yourdomain.com/sigs/rotate.php[/img]Optional step: Some forums block
.phpextensions on images (phpBB does this by default, I believe). If you run the Apache webserver withmod_rewriteinstalled (there’s a 90% chance that you do), you can use this little trick to fool the software into thinking your image is a regular one.Open Notepad or your favourite text editor.
Paste the following code:
RewriteEngine On RewriteRule ^.*$ rotate.phpSave the file as
.htaccessand upload it to the same directory as your images are in. If you’re using Notepad, it might complain that this is an invalid filename; if you have this problem, you can usually just use your FTP client to rename the file to.htaccess.Use the following as your image link, obviously replacing
yourdomain.com/sigs/with the correct URL:[img]http://yourdomain.com/sigs/rotate.png[/img]
All done! Enjoy.
And here’s an example of what it looks like in action: