Be the first to write a review
Free! - PHP and CSS: random images and CSS positioning
Until recently I was unaware of the power of CSS and Its ability to integrate with other languages. I always thought that styles were static and had to be coded into the style sheet.
It was with a visit to A List Apart when I stumbled across a PHP article about Random Images by Dan Benjamin. As I’m only a newcomer to PHP, I was interested enough to read on and found it to be a great article & script. Another blog style site Relatively Absolute is a perfect working example of the Image rotation script as it uses one for generating random header images much like the ones I’m going to be demonstrating here. In fact Absolutely Relative inspired me to write this article because I liked the transparency illusion so much.
Other Options
/*
If you’d like to enable additional image types other than
gif, jpg, and png, add a duplicate line to the section below
for the new image type.
Add the new file-type, single-quoted, inside brackets.
Add the mime-type to be sent to the browser, also single-quoted,
after the equal sign.
Example PDF Files entry:
$extList[‘pdf’] = ‘application/pdf’;
Example CSS Files entry:
$extList[‘css’] = ‘text/css’;
You can even serve up random HTML files:
$extList[‘html’] = ‘text/html’;
$extList[‘htm’] = ‘text/html’;
Just be sure your mime-type definition is correct!
*/
$extList = array();
$extList[‘gif’] = ‘image/gif’;
$extList[‘jpg’] = ‘image/jpeg’;
$extList[‘jpeg’] = ‘image/jpeg’;
$extList[‘png’] = ‘image/png’;
Additional File Formats in the Random Image script.
Looking at the array(); you can see we have 4 supported File types.
.gif
.jpg
.jpeg
.png
The fact that we can call random pdf’s, css style sheets and html documents, means you can really stir things up if you want to go into random overkill. You could even take this further yourself and add any other relevant File Types you want supported. With this in mind I trim the comments so my code block now supports all files mentioned above.
$extList = array();
$extList[‘gif’] = ‘image/gif’;
$extList[‘jpg’] = ‘image/jpeg’;
$extList[‘jpeg’] = ‘image/jpeg’;
$extList[‘png’] = ‘image/png’;
$extList[‘html’] = ‘text/html’;
$extList[‘htm’] = ‘text/html’;
$extList[‘css’] = ‘text/css’;
$extList[‘pdf’] = ‘application/pdf’;
The Array() with all supported File Types
That’s the PHP script set up and configured for our use. Save this as rotate.php in the folder where you have your images you want rotating.