PDA

View Full Version : Random inline image script?



Astonished Head
01-05-2005, 10:47 AM
Does anyone have a random image script that they use to display inline images?

I'm looking for one that I know works in the Westhost environment, because I haven't been able to get any of the .pl or .cgi scripts available on the web to work for me (from Matt's Script Archive, Jumbo CGI, etc.).

Many thanks,

--AH

wildjokerdesign
01-05-2005, 11:56 AM
What type are you looking for. I have used Matt's I am pretty sure in the past on a site. I have an old one that displays a random image useing SSI. I don't use it now but it always worked well. I have no idea where it came from or if it was something I cobbled together from a bunch of different scripts. I normally leave copy info and such in a script so it may have been from a bunch of different ones.


#!/usr/bin/perl
$imagedir = "/var/www/html/images/gallery/preview/";
$imageurl = "/images/gallery/preview/";

# ################################################
# Get Random Image and print in page
# ################################################

&getImages();
srand(time ^ $$);
my $i = rand(@GALLERY);
$randomimage = qq!<img src=$imageurl$GALLERY[$i] width= 200 height=200 alt="Random Gallery Image">!;
print "Content-type: text/html\n\n";
print $randomimage;
exit;

# ################################################
# Opens $imgedir and puts images into @Gallery
# ################################################
sub getImages {
opendir(DIR,"$imagedir") || die ; # Get list of gifs in directory
@gifs=grep(/.gif$/,readdir(DIR)); # and save info into an array.
close(DIR);
$numgifs=@gifs;
opendir(DIR,"$imagedir") || die ; # Get list of jpgs in user's home directory
@jpegs=grep(/.jpg$/,readdir(DIR)); # and save info into an array.
close(DIR);
$numjpegs=@jpegs;

for($j=0;$j<$numgifs;)
{
push @GALLERY, $gifs[$j];
++$j
}
for($v=0;$v<$numjpegs;)
{
push @GALLERY, $jpegs[$v];
++$v
}
}



1;

If you wanted to try and use it with an image tag then you it should work if you change the following code:


$randomimage = qq!<img src=$imageurl$GALLERY[$i] width= 200 height=200 alt="Random Gallery Image">!;
print "Content-type: text/html\n\n";
print $randomimage;

to this:


$randomimage = $imageurl$GALLERY[$i];
print "Location: $randomimage\n\n";


Mine was useing images that where all 200 X 200 so if for some reason that is not the case with you then in then you would need to remove that info from $randomimage or change it.

The call to the script using an image tag would not care about width and height unless you had included it in the image tag.

Hope that helps.

wildjokerdesign
01-05-2005, 11:59 AM
Oh did you remember to upload your script in ascii to the cgi-bin and then chage the permmisions to 755?

Astonished Head
01-05-2005, 12:18 PM
Thanks for the script!

>Oh did you remember to upload your script in ascii
>to the cgi-bin and then chage the permmisions to 755?

Yes on the ASCII, and oops, ****! on the permissions. I had forgotten that was the last thing I needed to check out before going to the forum.

I'm using WS-FTP, and you're supposed to be able to right-click on a file and change permissions, but I don't see it on the right-click menu (am I looking for CHMOD? I've got no idea).

Do you know what I need to do to confirm or change the permissions via WS-FTP? Its Help is no help.

Many thanks,

--AH

wildjokerdesign
01-05-2005, 12:34 PM
I use the pro version but don't think that part is different. Yes you select the file then right click in the drop down menu there should be an option called chmod(unix). Click and it opens a pop up you can then input 755. You should be able to see the permissions by useing DirInfo. It then downloads the info to your computer and opens it in your notepad. You should see this at the begining of the line the file is on - -rwxr-xr-x.

Astonished Head
01-05-2005, 04:48 PM
Hmm. Nope. No chmod here. I'm using v. 3.00 rel. 96.02.19, so I'll get a fresher version.

Astonished Head
01-06-2005, 10:30 AM
OK, I finally got Matt's script working...sort of.

I can see the images. But it seems to get "stuck" on one image, and won't rotate off of it.

This happens in Firefox, but not MSIE or Safari.

Which is both puzzling and annoying. Any ideas?

Thanx,

--AH

wildjokerdesign
01-06-2005, 10:40 AM
I wonder if it has to do with a cacheing issue. Matt's is useing the image tag to call the script am I right? I wonder if the Firefox browser thinks it is the same image and thus is showing the image it has saved on your local computer. You can try clearing your cache or hit cntrl and the refresh button. If that seems to be the case then it may help to switch to the first script I posted since it prints a different image url each time. I am not 100% sure about this but may be worth a try.

wildjokerdesign
01-06-2005, 11:39 AM
I see what you mean. I installed the script and did a bit of testing. On FireFox and FireBird it seems to get stuck. I cleared the cache and that seemed to work. ctrl refresh does not seem to work. It did seem that if I closed the browser and then opened it again and visited the page that it displayed a new image. This was not real consistant but then I only have about 4 images I am trying it on and it could just be the random thing hitting the same image.

Don't have time right now but if I can get to it I'll try some variations and see if it helps.

wildjokerdesign
01-06-2005, 12:53 PM
Ok this seems to work in all browsers:

In rand_image.pl find


print "Location: $basedir$files[$num]\n\n";
and replace with


$randomimage = qq!<img src=$basedir$files[$num]>!;
print "Content-type: text/html\n\n";
print $randomimage;

Then in your web page where you want the random image to show up place this SSI call

<!--#include virtual="/cgi-bin/rand_image.pl" -->

Unless your site settings are diffrent then the one I tested this on the page does not have to have the .shtml exstention it can be .html