Jump to content

Display Random Images


kalifornia
 Share

Recommended Posts

I apologize in advance if this topic has been covered, I didn't see anything when I did a search...

 

I'm looking for a script recommendation for displaying a random image everytime a page is loaded. I've seen some out there but ever since I found out some scripts may be banned I thought it would be best to ask first.

 

Thanks.

Link to comment
Share on other sites

What you need is a simple javascript and you can find it at www*hotscripts.com. However, IE in XP's SP2 has gotten annoying with it's blocking of scripts. I don't think this one gets blocked, though.

 

EDIT: was bored and did a quick search on my own, came up with way more than you ever wanted, probably...

www*hotscripts.com/search/1332314.html

Edited by TCH-Glenn
Link to comment
Share on other sites

If you don't like Javascript you could use this PHP solution:

 

1. Create a folder called rand_images and put all the images in it.

 

2. Name the images 0.gif, 1.gif, 2.gif, 3.gif, 4.gif etc.

 

3. Create a file called rand_images.php and put this code into it:

 

><?
// This script returns an image
header("Content-type: image/gif");

$image_count = 5;  // Set this variable to the total amount of
  	 // images in the rand_images folder

// Load a random image
$image = fopen("rand_images/".rand(0,$image_count-1).".gif","r");

// Return the image
fpassthru($image);
?>

 

4. Now when you want to display a random image, just use this html code:

><img src="rand_images.php">

 

----------

 

This should work :)

Link to comment
Share on other sites

  • 2 weeks later...

hey guys- I did a search for "random images" and "php" and found this thread. I tried to implement the code but it doesn’t seem to be working. I have 8 jpgs I think that php code I’m using has "gif" in it . . . but when I change to .jpg in php code - no image shows up . .

 

Testing Page

http://www.hotfrogdesign.com/new_index.htm

 

Anyway, here is all code. Any ideas why its not rotating?

 

File located images/ rand_images.php

 

><?
// This script returns an image
header("Content-type: image/gif");

$image_count = 8;  // Set this variable to the total amount of
   // images in the rand_images folder

// Load a random image
$image = fopen("rand_images/".rand(0,$image_count-1).".jpg","r");

// Return the image
fpassthru($image);
?>

 

Folder located domain/rand_images

 

0.jpg

1.jpg

2.jpg

3.jpg

4.jpg

5.jpg

6.jpg

7.jpg

 

 

thanks TCH!

Link to comment
Share on other sites

hey guys- I did a search for "random images" and "php" and found this thread. I tried to implement the code but it doesn’t seem to be working. I have 8 jpgs I think that php code I’m using has "gif" in it . . . but when I change to .jpg in php code - no image shows up . .

 

Testing Page

http://www.hotfrogdesign.com/new_index.htm

 

Anyway, here is all code. Any ideas why its not rotating?

 

File located images/ rand_images.php

 

><?
// This script returns an image
header("Content-type: image/gif");

$image_count = 8;  // Set this variable to the total amount of
   // images in the rand_images folder

// Load a random image
$image = fopen("rand_images/".rand(0,$image_count-1).".jpg","r");

// Return the image
fpassthru($image);
?>

 

Folder located domain/rand_images

 

0.jpg

1.jpg

2.jpg

3.jpg

4.jpg

5.jpg

6.jpg

7.jpg

 

 

thanks TCH!

Look at your Content-Type header. 71.gif

 

Change it to image/jpeg

 

here, try this, I made slightly easier to run.

 

><?php

  $numImages = 3;        // Number of images starting with 1
  $imageType = "jpeg";   // jpeg || gif
  $fileExt   = ".jpg";   // File Extension

  $randPic = rand( 1, $numImages );

  // open the file in a binary mode
  $file = $randPic . $fileExt;
  $fp = fopen($file, 'rb');

  // send the right headers
  header("Content-Type: image/{$imageType}");
  header("Content-Length: " . filesize($file));

  // dump the picture and stop the script
  fpassthru($fp);
  exit();
?>

Use at your own risk.

Link to comment
Share on other sites

hey thanks all. pretty sneaky Lisa . . . I made section 31's change and it still only shows the orange 6.jpg.

 

below I updated the code in bold

 

><?
// This script returns an image
header("Content-type: image/[B]jpeg[/B]");

$image_count = 7;  // Set this variable to the total amount of
   // images in the rand_images folder

// Load a random image
$image = fopen("rand_images/".rand(0,$image_count-1).".jpg","r");

// Return the image
fpassthru($image);
?>

 

>0.jpg
1.jpg
2.jpg
3.jpg
4.jpg
5.jpg
6.jpg
[B]eliminated 7.jpg[/B]

Link to comment
Share on other sites

  • 2 years later...

Rather than start a new topic, I thought I'd ask it here.

 

I'm looking for a random image generator with a corresponding link. I found one here: http://www.scriptarchive.com/ssi_image.html, but I can't get it to work. It looked simple enough but when I tried it, I got this message:

 

"Warning: main(http://www.moviemansguide.com/external/ssi_rand_image.pl'>http://www.moviemansguide.com/external/ssi_rand_image.pl) [function.main]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in /home2/**edited**/public_html/index2.php on line 51

 

Warning: main(http://www.moviemansguide.com/external/ssi_rand_image.pl) [function.main]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in /home2/**edited**/public_html/index2.php on line 51

 

Warning: main() [function.include]: Failed opening 'http://www.moviemansguide.com/external/ssi_rand_image.pl' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home2/**edited**/public_html/index2.php on line 51"

 

 

 

I was wondering if there were other scripts out there that are easier to implement?

Link to comment
Share on other sites

This is a simple one that I created for my site

it lets you display a random image and a link.

Just add more images and links to the array as needed.

 

><?php
srand((float) microtime() * 10000000);

 $image[1]['pic']='/bob/297.jpg';
 $image[1]['link']='/bob/index.html';

 $image[2]['pic']='/jay/05.jpg';
 $image[2]['link']='/jay/index.html';

 $image[3]['pic']='/andrea/cat19.jpg';
 $image[3]['link']='/andrea/index.html';


 $rn = array_rand($image);


echo 'Featured Member<br />';
echo '<a href="'.$image[$rn]['link'].'" target="_self">';
echo '<img src="'.$image[$rn]['pic'].'"  ><br />';
echo 'Click image to see more.</a><br />';
?>

Link to comment
Share on other sites

Thanks so much Don! That's exactly what I was looking for. Something simple and easy to adjust.

 

I've made some (minor) changes if someone wanted an alt tag as well:

 

><?php
srand((float) microtime() * 10000000);

 $image[1]['pic']='http://www.moviemansguide.com/images/randomimages/dvdcover_number23.jpg';
 $image[1]['link']='http://www.moviemansguide.com/reviews/DVD/read.php?id=number23';
 $image[1]['alt']='The Number 23 (DVD)';


 $image[2]['pic']='http://www.moviemansguide.com/images/randomimages/dvdcover_zodiac.jpg';
 $image[2]['link']='http://www.moviemansguide.com/reviews/DVD/read.php?id=zodiac';
 $image[2]['alt']='Zodiac (DVD)';


 $image[3]['pic']='http://www.moviemansguide.com/images/randomimages/dvdcover_hills2.jpg';
 $image[3]['link']='http://www.moviemansguide.com/reviews/DVD/read.php?id=hills-season2';
 $image[3]['alt']='The Hills (Season 2)';



 $rn = array_rand($image);


echo '<a href="'.$image[$rn]['link'].'" target="_self">';
echo '<center><img src="'.$image[$rn]['pic'].'" alt="'.$image[$rn]['alt'].'"  ></center>';
?>

 

For some reason, the breaks aren't showing up so it's tough to read...

Edited by TheMovieman
Link to comment
Share on other sites

It looks good, in fact the site is looking very good :thumbup1:

 

But don't forget to close to the image tag with </a>

change

echo '<center><img src="'.$image[$rn]['pic'].'" alt="'.$image[$rn]['alt'].'" ></center>';

to

echo '<center><img src="'.$image[$rn]['pic'].'" alt="'.$image[$rn]['alt'].'" ></a></center>';

Link to comment
Share on other sites

Here's one I wrote a few years ago as an SSI.

Comments added so you can see what it's doing

 

#!/usr/bin/perl

 

$bannerfile = "$thepath/cgi-bin/banner.txt"; # file that holds the list

open THEFILE, "<$bannerfile" || die "Unable to open file: $!";

 

(@banners) = <THEFILE>;

close THEFILE;

$totbanners = @banners;

srand;

$picked = int(rand($totbanners));

@bannerdata = split(/\|/,$banners[$picked]);

$bannerdata[1]++;

$banners[$picked] = "$bannerdata[0]\|$bannerdata[1]\|$bannerdata[2]";

print "Content-type: text/html\n\n"; # you may need to remove this, seems to depend on what host it's on

if ($bannerdata[0] eq 'link') { print "\n$bannerdata[2]\n"; }

if ($bannerdata[0] eq 'file') {

open THEFILE, "$bannerdata[2]";

(@data) = <THEFILE>;

close THEFILE;

print "<center>";

foreach $datastring (@data) { print "$datastring\n" }

}

# this updates the data to include how many times that item has been picked.

open THEFILE, ">$bannerfile";

print THEFILE @banners;

close THEFILE;

 

banner.txt contains...

link|13|Have you checked out our <a href="/board/" target="board">Discussion Forums</a> yet?

 

one entry per line, and you can see you can include links, the contents of a file, or just insert random html code.

the link, 13, and Have you text are seperated by the pipe symbol.

so as an example for an image, you want to use the link command, since you are only sending text.

 

link|0|<img src="myimage.jpg">

Remember here that 0 is the number of times this image has been picked, since it's new, we start with 0

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...