kalifornia Posted November 24, 2004 Share Posted November 24, 2004 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. Quote Link to comment Share on other sites More sharing options...
ThumpAZ Posted November 24, 2004 Share Posted November 24, 2004 (edited) 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 November 24, 2004 by TCH-Glenn Quote Link to comment Share on other sites More sharing options...
TCH-Thomas Posted November 25, 2004 Share Posted November 25, 2004 Welcome to your extended family Kalifornia Quote Link to comment Share on other sites More sharing options...
TCH-Bruce Posted November 25, 2004 Share Posted November 25, 2004 Welcome to the family! Quote Link to comment Share on other sites More sharing options...
TCH-Don Posted November 25, 2004 Share Posted November 25, 2004 Welcome to the Family Kalifornia and your new home! We really are like family here. So if you need anything, just ask your new family! We love to help : Quote Link to comment Share on other sites More sharing options...
mattisl Posted November 26, 2004 Share Posted November 26, 2004 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 Quote Link to comment Share on other sites More sharing options...
Virtual Imager Posted November 26, 2004 Share Posted November 26, 2004 Cool! Quote Link to comment Share on other sites More sharing options...
kalifornia Posted November 27, 2004 Author Share Posted November 27, 2004 Wow, thanks for all the great responses, I'll give it a try! Quote Link to comment Share on other sites More sharing options...
CRO8 Posted December 6, 2004 Share Posted December 6, 2004 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! Quote Link to comment Share on other sites More sharing options...
LisaJill Posted December 6, 2004 Share Posted December 6, 2004 You might want to try this script. It allows for several extensions and is very short and easy to implement. Quote Link to comment Share on other sites More sharing options...
CRO8 Posted December 6, 2004 Share Posted December 6, 2004 thanks lisa Ill give it a shot tonight. thanks! Quote Link to comment Share on other sites More sharing options...
LisaJill Posted December 6, 2004 Share Posted December 6, 2004 Post with your results. (do you like how I totally avoided troubleshooting your script? I thought it was pretty slick. ) Quote Link to comment Share on other sites More sharing options...
section31 Posted December 7, 2004 Share Posted December 7, 2004 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. 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. Quote Link to comment Share on other sites More sharing options...
CRO8 Posted December 7, 2004 Share Posted December 7, 2004 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] Quote Link to comment Share on other sites More sharing options...
TheMovieman Posted August 7, 2007 Share Posted August 7, 2007 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? Quote Link to comment Share on other sites More sharing options...
TCH-Bruce Posted August 7, 2007 Share Posted August 7, 2007 Have you uploaded the script in ASCII mode vs. Binary and have you set the permissions to 755 rwxr-xr-x ? Quote Link to comment Share on other sites More sharing options...
TCH-Don Posted August 7, 2007 Share Posted August 7, 2007 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 />'; ?> Quote Link to comment Share on other sites More sharing options...
TheMovieman Posted August 7, 2007 Share Posted August 7, 2007 (edited) 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 August 7, 2007 by TheMovieman Quote Link to comment Share on other sites More sharing options...
TCH-Don Posted August 7, 2007 Share Posted August 7, 2007 It looks good, in fact the site is looking very good 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>'; Quote Link to comment Share on other sites More sharing options...
TheMovieman Posted August 7, 2007 Share Posted August 7, 2007 (edited) Whoops, thanks for pointing that out Don. And thanks, finally got a style/format I like and will stick with (the site has gone through many changes over the year). Edited August 7, 2007 by TheMovieman Quote Link to comment Share on other sites More sharing options...
TCH-Don Posted August 7, 2007 Share Posted August 7, 2007 For some reason, the breaks aren't showing up so it's tough to read... I missed that What did you mean the breaks? Quote Link to comment Share on other sites More sharing options...
TheMovieman Posted August 7, 2007 Share Posted August 7, 2007 The first part (the array) is all together (in the code box). I tried editing before and I could only get a part of it to separate. I should've said paragraph breaks or something, lol. If that makes any sense... Quote Link to comment Share on other sites More sharing options...
TCH-Don Posted August 8, 2007 Share Posted August 8, 2007 Ok you mean the code displayed in your post not on your site. I get it. Quote Link to comment Share on other sites More sharing options...
TheMovieman Posted August 8, 2007 Share Posted August 8, 2007 Haha, yes. Sorry Quote Link to comment Share on other sites More sharing options...
Pendragon Posted August 13, 2007 Share Posted August 13, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.