Ezilon.com - Target Your Audience, be Seen in Your Region

Random Picture

You are viewing this site as a guest. Join our community to get your questions answered and share knowledge. Active members may advertise and ask for a website critique.

They have: 1 posts

Joined: Jan 2005

Is it possible to load random jpeg files every time a page is visited. ie- every time the page is viewed, a random image is displayed instead of the usual image.

Cheers

Karl

Greg K's picture
Moderator

He has: 1,667 posts

Joined: Nov 2003

You can use a script to do this, either Client Side (via javascript) or Server Side. See http://209.68.38.102/limo/randpic.php for a script I wrote that lets me have changing icons in another forum I frequest.

The code for the PHP one is:

<?

  $randval = rand(1,4); // generate a number from 1 to 4
  $fname = "limo-0" . $randval . ".jpg";
  $fp = fopen ($fname, "r");
  while (!feof ($fp)) echo fgetc($fp);
  fclose($fp);
?>

'

I have this code in a directory (limo) and also have 4 images in there, limo-01.jpg, limo-02.jpg, limo-03.jpg and limo-04.jpg.

-Greg

[This space intentionally left blank]

Cool Geek Supplies: www.ThinkGeek.com

ValleyPages's picture

He has: 46 posts

Joined: Nov 2001

Greg K wrote: You can use a script to do this, either Client Side (via javascript) or Server Side. See http://209.68.38.102/limo/randpic.php for a script I wrote that lets

Greg, something wrong with that file.

andy206uk's picture
DeveloperModerator

He has: 1,754 posts

Joined: Jul 2002

or if you don't have php... here's a javascript version:

&lt;script language="JavaScript"&gt;
<!--

function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="image1.gif"
myimages[2]="image2.gif"
myimages[3]="image3.gif"
myimages[4]="image4.gif"
myimages[5]="image5.gif"
myimages[6]="image6.gif"

var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<img src="'+myimages[ry]+'" border=0>')
}
random_imglink()
//-->
&lt;/script&gt;

'

Simply place it in your HTML anywhere between the body tags and update the image links in the array where prompted.

Andyk

Blog of a Web Designer
Give a man a fish and you feed him for a day. Teach him to use the Net and he won't bother you for weeks.

CptAwesome's picture
Sponsor

He has: 370 posts

Joined: Dec 2004

You mean the following jibberish?
= ハル「.,\,T靫ネ%0ED?Jォフ-;"シ{i誄嫩-SA/モクW・Vヲモィ・姓簽蟹 綣#UムNオホーソ]5薈

What that script is doing is opening the actual image file, and writing all the data into the browser, and the browser should read it, BUT you should add

<?
  header("Content-Type: image/jpg");
  $randval = rand(1,4); // generate a number from 1 to 4
  $fname = "limo-0" . $randval . ".jpg";
  $fp = fopen ($fname, "r");
  while (!feof ($fp)) echo fgetc($fp);
  fclose($fp);
?>

'

<? echo implode('',explode('|','A|W|E|S|O|M|E')); ?>

http://www.free-online-web-tools.com