random image code

  • butlerboy99
  • Beginner
  • Beginner
  • User avatar
  • Posts: 60
  • Loc: WA

Post 3+ Months Ago

I'm not adept at code yet, but I want a code that will summon a random image... can anyone help me out??
  • Vladdrac
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 2136
  • Loc: Louisville, Ky

Post 3+ Months Ago

here is a link to a great php tutorial, that will probably help you. php week 4

Go to the home page http://www.scriptschool.com to learn a whole lot about java and php[/url]
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

If you don't have php available or just want a cut and past script you could search google for something like javascript random images and you'll find lots of examples of different scripts. For example, this one:

http://javascript.internet.com/miscella ... image.html

Unlike PHP these all have the drawback that they are dependent on the user having javascript enabled to work.
  • b0ksah
  • Newbie
  • Newbie
  • b0ksah
  • Posts: 10
  • Loc: Aarhus, Denmark

Post 3+ Months Ago

Use a txt file here to get random img url's from

imgtext.txt do I call it here could look like this (REMEMBER NO WHITESPACE BEFORE THE TEXT OR AFTER... they will be counted as a row and you'll get img scr=""):
Code: [ Select ]
http://www.bax.dk/WoW/SplashDimz02.jpg
http://www.bax.dk/WoW/SplashDimz03.jpg
http://www.bax.dk/WoW/SplashDimz04.jpg
http://www.bax.dk/WoW/SplashDimz05.jpg
http://www.bax.dk/WoW/SplashDimz06.jpg
http://www.bax.dk/WoW/SplashDimz07.jpg
http://www.bax.dk/WoW/SplashDimz08.jpg
http://www.bax.dk/WoW/WoWBlood.jpg
http://www.bax.dk/WoW/WoWpolls02.jpg
  1. http://www.bax.dk/WoW/SplashDimz02.jpg
  2. http://www.bax.dk/WoW/SplashDimz03.jpg
  3. http://www.bax.dk/WoW/SplashDimz04.jpg
  4. http://www.bax.dk/WoW/SplashDimz05.jpg
  5. http://www.bax.dk/WoW/SplashDimz06.jpg
  6. http://www.bax.dk/WoW/SplashDimz07.jpg
  7. http://www.bax.dk/WoW/SplashDimz08.jpg
  8. http://www.bax.dk/WoW/WoWBlood.jpg
  9. http://www.bax.dk/WoW/WoWpolls02.jpg


The PHP code to deal with this txt looks like this:

Code: [ Select ]
<?php
$txtfile = 'imgtext.txt'; // here we set the name of the txt file we wanna open
$fp = fopen($txtfile, "r"); // here we open it
$content = fread($fp, filesize($txtfile)); // get size
$imgarr = explode($lim,$content); // transform txt to array
fclose($fp);

  srand((double)microtime()*1000000); //Start the randomness

  $index = (rand(1, sizeof($imgarr)) - 1); //Pick random thingie


  $rthingie = $namearr[$index]; //Return the thingie
?>
  1. <?php
  2. $txtfile = 'imgtext.txt'; // here we set the name of the txt file we wanna open
  3. $fp = fopen($txtfile, "r"); // here we open it
  4. $content = fread($fp, filesize($txtfile)); // get size
  5. $imgarr = explode($lim,$content); // transform txt to array
  6. fclose($fp);
  7.   srand((double)microtime()*1000000); //Start the randomness
  8.   $index = (rand(1, sizeof($imgarr)) - 1); //Pick random thingie
  9.   $rthingie = $namearr[$index]; //Return the thingie
  10. ?>


You can now use it in you code

Code: [ Select ]
<img source="<?php $rthingie ?>">


there you go ;)
  • suzie
  • Guru
  • Guru
  • User avatar
  • Posts: 1134
  • Loc: England

Post 3+ Months Ago

You can see such a gallery of 3d art work at my link:

http://www.graphicsteck.ca/vb/index.php?

Cool place too.

:D
  • phpSelectah
  • Student
  • Student
  • User avatar
  • Posts: 97

Post 3+ Months Ago

This script reads images from a directory:

Code: [ Select ]
<?
//phpSelectah
//random image generator from a directory
//mrpic.php
//2/12/04


  $dir = array ("img");  //image paths relative to php file
  $allowed = array('jpg','jpeg','gif','png');  //declare allowable file extensions
  $pics = array();  //allocate space for the image array


    for($i=0;$i<count($dir);$i++){  //loop through available directories

        $dH = dir($dir[$i]) or next;  // Make directry handle object or else move to the next one

            while($file = $dH->read()){  //loop the directory, writing filename into variable $file
   
            list($name, $ext) = explode("." , $file);  //chunk file name by ., into filename + extension

             if(in_array( strtolower($ext) , $allowed)){  //check if extension is in allowable array

              $pics[] = $dir[$i] . "/" . $name . "." .$ext;  //add the file into the array

             }  // end if case

            }  // end nested while loop

        $dH = "";  //destroy directory object

    }  // end for loop

  $pic = $pics[mt_rand(0,count($pics) - 1)];  //get random image from array using marsienne twister
  
  if($fP = fopen( $pic , rb)){  //binary fopen for reading

   while($l = fgets($fP)){  //read image file pointer into variable $l

     $output .= $l;  // concatenate each $l into $output

     }  // end while loop
     
  $outputLen = strlen($output);  //determine length of our image for output header

  header("Content-Length: $outputLen");  //tell browser length of document
    header("Content-type: image/png");  //tell browser type of document
    print $output;  //give browser the images content

    fclose($fP);  //terminate file pointer

    }else{

  die(); // we failed so die

    }
?>
  1. <?
  2. //phpSelectah
  3. //random image generator from a directory
  4. //mrpic.php
  5. //2/12/04
  6.   $dir = array ("img");  //image paths relative to php file
  7.   $allowed = array('jpg','jpeg','gif','png');  //declare allowable file extensions
  8.   $pics = array();  //allocate space for the image array
  9.     for($i=0;$i<count($dir);$i++){  //loop through available directories
  10.         $dH = dir($dir[$i]) or next;  // Make directry handle object or else move to the next one
  11.             while($file = $dH->read()){  //loop the directory, writing filename into variable $file
  12.    
  13.             list($name, $ext) = explode("." , $file);  //chunk file name by ., into filename + extension
  14.              if(in_array( strtolower($ext) , $allowed)){  //check if extension is in allowable array
  15.               $pics[] = $dir[$i] . "/" . $name . "." .$ext;  //add the file into the array
  16.              }  // end if case
  17.             }  // end nested while loop
  18.         $dH = "";  //destroy directory object
  19.     }  // end for loop
  20.   $pic = $pics[mt_rand(0,count($pics) - 1)];  //get random image from array using marsienne twister
  21.   
  22.   if($fP = fopen( $pic , rb)){  //binary fopen for reading
  23.    while($l = fgets($fP)){  //read image file pointer into variable $l
  24.      $output .= $l;  // concatenate each $l into $output
  25.      }  // end while loop
  26.      
  27.   $outputLen = strlen($output);  //determine length of our image for output header
  28.   header("Content-Length: $outputLen");  //tell browser length of document
  29.     header("Content-type: image/png");  //tell browser type of document
  30.     print $output;  //give browser the images content
  31.     fclose($fP);  //terminate file pointer
  32.     }else{
  33.   die(); // we failed so die
  34.     }
  35. ?>


You can increase the directories that this script looks in by adding another directory name into the dir array :)


Then call this image using <img src="http://www.somewhere.com/mrpic.php">

:D Hth ......
  • Johan007
  • Guru
  • Guru
  • User avatar
  • Posts: 1080
  • Loc: Aldershot, UK

Post 3+ Months Ago

Here is some simple asp you could insert onto the page if your server can do it.

Code: [ Select ]
RANDOMIZE
LowestNumber = 1
HighestNumber = 3
RandomNumber = INT((HighestNumber-LowestNumber+1)*Rnd+LowestNumber)

SELECT CASE RandomNumber

CASE "1"
respons.write "img src=""/banners/monkeyboyreview_468_60.gif"" border=""0""></a>"

CASE "2"
respons.write "img src=""/banners/monkeyboyreview_468_60.gif"" border=""0""></a>"

CASE "3"
respons.write "img src=""/banners/monkeyboyreview_468_60.gif"" border=""0""></a>"

END SELECT
  1. RANDOMIZE
  2. LowestNumber = 1
  3. HighestNumber = 3
  4. RandomNumber = INT((HighestNumber-LowestNumber+1)*Rnd+LowestNumber)
  5. SELECT CASE RandomNumber
  6. CASE "1"
  7. respons.write "img src=""/banners/monkeyboyreview_468_60.gif"" border=""0""></a>"
  8. CASE "2"
  9. respons.write "img src=""/banners/monkeyboyreview_468_60.gif"" border=""0""></a>"
  10. CASE "3"
  11. respons.write "img src=""/banners/monkeyboyreview_468_60.gif"" border=""0""></a>"
  12. END SELECT
  • LazyJim
  • Student
  • Student
  • LazyJim
  • Posts: 92

Post 3+ Months Ago

nice script phpSelectah (not checked or tried it but nice idea!)
  • zudomon
  • Born
  • Born
  • zudomon
  • Posts: 4

Post 3+ Months Ago

Where do you put the dir at? I trying to figure that out :P

Quote:

You can increase the directories that this script looks in by adding another directory name into the dir array :)


Then call this image using <img src="http://www.somewhere.com/pic.php">

:D Hth ......
  • LNK
  • Newbie
  • Newbie
  • LNK
  • Posts: 8

Post 3+ Months Ago

I've used the one found here:

http://automaticlabs.com/products/rotator


It's really easy.
  • zudomon
  • Born
  • Born
  • zudomon
  • Posts: 4

Post 3+ Months Ago

Only problem is that it only want's one dir. I have more then on.
I'm, trying to get a pic from one of the dirs not just one.

LNK wrote:
I've used the one found here:

http://automaticlabs.com/products/rotator


It's really easy.
  • Valeria
  • Born
  • Born
  • Valeria
  • Posts: 2

Post 3+ Months Ago

What if you are not allowed to have Javascript enabled, and don't have enough room in your sig for that long-arse code? I've been trying to find a new image host for my sig on a few of the forums and have been unable to find a good one. And I have no knowledge of creating/editing codes for my own usage, so Javas and the like from scratch is out for me. *sweatdrops*
  • phpSelectah
  • Student
  • Student
  • User avatar
  • Posts: 97

Post 3+ Months Ago

zudomon wrote:
Where do you put the dir at? I trying to figure that out :P

Quote:

You can increase the directories that this script looks in by adding another directory name into the dir array :)


Then call this image using <img src="http://www.somewhere.com/pic.php">

:D Hth ......


In the line:

Code: [ Select ]

  $dir = array ("img");  //image paths relative to php file
  1.   $dir = array ("img");  //image paths relative to php file


You can add multiple directories, I.E. directories named 'images' and 'avatars' as:

Code: [ Select ]

  $dir = array ("img","images","avatars");  //image paths relative to php file
  1.   $dir = array ("img","images","avatars");  //image paths relative to php file



:D
  • darkermoon
  • Expert
  • Expert
  • User avatar
  • Posts: 542
  • Loc: Riverdale, MD

Post 3+ Months Ago

suzie wrote:
You can see such a gallery of 3d art work at my link:

http://www.graphicsteck.ca/vb/index.php?

Cool place too.

:D


reminds me of Copermine in a way.. coolness.
  • DJ Lite
  • Newbie
  • Newbie
  • DJ Lite
  • Posts: 7

Post 3+ Months Ago

hello, i believe that this is what you are
looking for, Code below url:
http://c.1asphost.com/GooldComputer1/weeps.htm
(Note: there is 4-5 Images hit refresh too
see them all)


Code: [ Select ]
<script language="JavaScript">
<!--
/*
Random Image Link Script
*/

function random_imglink(){
 var myimages=new Array()
 //specify random images below. You can have as many as you wish
 myimages[1]="http://img.photobucket.com/albums/v385/Weez3r/DJLite1copy.gif"
 myimages[2]="http://img.photobucket.com/albums/v385/Weez3r/Grungecopy.gif"
 myimages[3]="http://img.photobucket.com/albums/v385/Weez3r/15-th.gif"
 myimages[4]="http://img.photobucket.com/albums/v385/Weez3r/g.png"

 //specify corresponding links below
 var imagelinks=new Array()
 imagelinks[1]="www.url.com"
 imagelinks[2]="www.url.com"
 imagelinks[3]="www.url.com"
 imagelinks[4]="www.url.com"
 imagelinks[5]="www.url.com"

 var ry=Math.floor(Math.random()*myimages.length)

 if (ry==0)
   ry=1
   document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>')
}

 random_imglink()
//-->
</script>
  1. <script language="JavaScript">
  2. <!--
  3. /*
  4. Random Image Link Script
  5. */
  6. function random_imglink(){
  7.  var myimages=new Array()
  8.  //specify random images below. You can have as many as you wish
  9.  myimages[1]="http://img.photobucket.com/albums/v385/Weez3r/DJLite1copy.gif"
  10.  myimages[2]="http://img.photobucket.com/albums/v385/Weez3r/Grungecopy.gif"
  11.  myimages[3]="http://img.photobucket.com/albums/v385/Weez3r/15-th.gif"
  12.  myimages[4]="http://img.photobucket.com/albums/v385/Weez3r/g.png"
  13.  //specify corresponding links below
  14.  var imagelinks=new Array()
  15.  imagelinks[1]="www.url.com"
  16.  imagelinks[2]="www.url.com"
  17.  imagelinks[3]="www.url.com"
  18.  imagelinks[4]="www.url.com"
  19.  imagelinks[5]="www.url.com"
  20.  var ry=Math.floor(Math.random()*myimages.length)
  21.  if (ry==0)
  22.    ry=1
  23.    document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>')
  24. }
  25.  random_imglink()
  26. //-->
  27. </script>

Hope I helped
  • dementia
  • Born
  • Born
  • dementia
  • Posts: 3

Post 3+ Months Ago

Hi. This is my first "official" forum post. I tried both phpselector's and the rotator suggested by LNK and set them up in my site. It works as is but if I try to use them as a sig, it doesn't work. I don't know why. Any ideas?
  • ScienceOfSpock
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1893
  • Loc: Las Vegas

Post 3+ Months Ago

My guess is because the image tag looks for an actual image (ie .jpg, .gif, .png etc) and doesn't even bother with .php files.
  • meta
  • Born
  • Born
  • User avatar
  • Posts: 2
  • Loc: U.A.E

Post 3+ Months Ago

Here's another way in javascript: Cycling Images with a random Start!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Cycling Images with Random Start</title>
<script type="text/javascript" language="Javascript">
<!-- Hide script from old browsers

adImages = new Array("images/image1.gif","images/iamge2.gif","images/image3.gif")
imgCt = adImages.length
firstTime = true

function rotate() {
if (document.images) {
if (firstTime) {
thisAd = Math.floor((Math.random() * imgCt))
firstTime = false
}
else {
thisAd++
if (thisAd == imgCt) {
thisAd = 0
}
}
document.myPicture.src=adImages[thisAd]
setTimeout("rotate()", 3 * 1000)
}
}

// End hiding script from old browsers -->
</script>
</head>
<body bgcolor="#FFFFFF" onload="rotate()">
<center>
<img src="images/spacer.gif" width="400" height="75" name="myPicture"
alt="Random Images" />
</center>
</body>
</html>


explanations:

[1]. Set up the array and the variable that contains the number of items in the array. : image1, image2, and image3. and so on...

adImages = new Array
("images/image1.gif","images/iamge2.gif","images/image3.gif")

[2]. Set the variable "firstTime"(you can change this as you want) this variable will only be used once, so that the script doesn't produce a random image each time through the loop.

firstTime = true

[3]. This conditional check only rins once, and if the value of "firstTime" is true, sets the value of "thisAd" to random number, using the now-familiar " Math.random method". After the job is done, firstTime is set to "false".

:) chow!

you can check thispage:
//http://www.javascriptworld.com/scripts/chap05/script08.html
  • dementia
  • Born
  • Born
  • dementia
  • Posts: 3

Post 3+ Months Ago

ScienceOfSpock wrote:
My guess is because the image tag looks for an actual image (ie .jpg, .gif, .png etc) and doesn't even bother with .php files.


I don't think so. I used to have my sig hosted at uploadit.org and the extention for their random image generator is php as well and it worked fine.
  • chiuaua
  • Born
  • Born
  • chiuaua
  • Posts: 2

Post 3+ Months Ago

How could I do this one: I have hundreds of folders containing images in one folder. I would like to have a script, that will look in all of those folders (Every folder shouldn't be specified in the script, it would just have to be all ;)), and choose a random image and display it. Possible?
  • meman
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3432
  • Loc: London Town , Apples and pears and all that crap

Post 3+ Months Ago

chiuaua it would probably be easier to just copy all if the images to the same folder lol ;)...

any way, i know the orriginal question had been answered , but ill post the random image selector i made for some of my avatars.
It might help someone out.
Its pretty simple.
PHP Code: [ Select ]
<?php
 
 
 
////////
 
// for this to work you will need 25 .png images named
 
//  1 to 25. ie. 1.png 2.png 3.png and so on up to 25
 
////////
 
 
 
// select a random number between 1 and 25
 
$rand = rand(1, 25);
 
 
 
// select the image using the random number
 
$image = "$rand.png";
 
 
 
//create the image
 
$im = imagecreatefrompng($image);  
 
 
 
 
 
// Tell the browser to expect binary data
 
// and to create an image from what it recieves
 
header("Content-Type: image/png");
 
 
 
// send it then throw it away
 
Imagepng($im,'',100);
 
ImageDestroy ($im);
 
?>
  1. <?php
  2.  
  3.  
  4.  
  5. ////////
  6.  
  7. // for this to work you will need 25 .png images named
  8.  
  9. //  1 to 25. ie. 1.png 2.png 3.png and so on up to 25
  10.  
  11. ////////
  12.  
  13.  
  14.  
  15. // select a random number between 1 and 25
  16.  
  17. $rand = rand(1, 25);
  18.  
  19.  
  20.  
  21. // select the image using the random number
  22.  
  23. $image = "$rand.png";
  24.  
  25.  
  26.  
  27. //create the image
  28.  
  29. $im = imagecreatefrompng($image);  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. // Tell the browser to expect binary data
  36.  
  37. // and to create an image from what it recieves
  38.  
  39. header("Content-Type: image/png");
  40.  
  41.  
  42.  
  43. // send it then throw it away
  44.  
  45. Imagepng($im,'',100);
  46.  
  47. ImageDestroy ($im);
  48.  
  49. ?>
  • chiuaua
  • Born
  • Born
  • chiuaua
  • Posts: 2

Post 3+ Months Ago

No, I mustn't copy them into same folder, because I'm using gallery system (Folder represents an album).

Post Information

  • Total Posts in this topic: 22 posts
  • Users browsing this forum: No registered users and 85 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.