Random Picture

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

He has: 2,145 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

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.

He has: 1,758 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.

Andy

CptAwesome's picture

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);
?>
'

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.