Skip to Main Content
Painting the Web
book

Painting the Web

by Shelley Powers
May 2008
Beginner content levelBeginner
656 pages
17h 41m
English
O'Reilly Media, Inc.
Content preview from Painting the Web

A Bit of Code

A final way to integrate photos into your web pages is using a bit of code embedded into your web pages in such a way that photos are displayed randomly each time a person accesses the page. I've used this for years, both to create changeable sidebar photo displays and also to modify banners and backgrounds for my stylesheets.

Who knows who originated the first PHP-based sidebar photo randomizer. It's been around a long time, though, and is quite popular because so many weblogs and other tools are PHP-based.

If your web pages are PHP-based, all you need do to use a randomizer is organize the photos into one directory local to the site, and save the PHP in Example 4-2 to a file located wherever you want the photo to display.

Gallery with the Ajaxian theme

Figure 4-36. Gallery with the Ajaxian theme

Example 4-2. PHP script to randomly display photos

<?php $dir = "/home/someloc/www/images/"; $url = "http://somesite.com/images/"; $exts = array('jpg'); //collect list of images in current directory $imgs = array( ); if($handle = opendir($dir)) { while(false !== ($image = readdir($handle))) foreach($exts as $ext) if(strstr($image, '.' . $ext)) $imgs[] = $image; closedir($handle); } //generate a random number srand((double)microtime( ) * 1000000); //change the number after the % to the number of images //you have $ct = count($imgs); $rn = (rand( )%$ct); $imgname = trim($imgs[$rn]); printf("<img src='$url%s' alt='' ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Reinventing the Organization for GenAI and LLMs

Reinventing the Organization for GenAI and LLMs

Ethan Mollick
Mastering PHP 7

Mastering PHP 7

Branko Ajzele

Publisher Resources

ISBN: 9780596515096Supplemental ContentErrata Page