Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Random Image in HTML

by cei (Monk)
on Apr 24, 2000 at 23:19 UTC ( [id://8772]=CUFP: print w/replies, xml ) Need Help??

Takes a random file from a directory and spits it back out in an IMG tag.
#!/usr/bin/perl ###################################################################### +########## # This script randomly selects a file from a directory of graphics and + returns # that image to an HTML page. The proper method for calling this CGI w +ould be # from an HTML page, using <img src="randomimage.cgi"> # # Copyright 1998 Click Active Media # Code by Chuck Ivy (chuck@clickmedia.com) ###################################################################### +########## $directoryname = "rndimg"; opendir(FILES,$directoryname); @URL = readdir(FILES); &chooseURL; while (($random_URL eq ".") || ($random_URL eq "..")) { &chooseURL; } print "Expires: Wednesday, 27-Dec-95 05:29:10 GMT", "\n"; print "Pragma: no-cache", "\n"; print "Location: $directoryname/$random_URL", "\n\n"; closedir(FILES); sub chooseURL { srand (time | $$); $number_of_URL = $#URL; $random = int (rand ($number_of_URL)); $random_URL = $URL[$random]; }

Replies are listed 'Best First'.
RE: Random Image in HTML
by turnstep (Parson) on Apr 25, 2000 at 02:26 UTC
    My quick version:
    if (opendir(PIZZA, "$dirname")) { @images = grep { -f "$dirname/$_" && m/\.gif|jpg|png/ } readdir(PIZZ +A); srand(); $image = $images[rand($#images+1)]; closedir(PIZZA); if (open(IMAGE, "$dirname/$image")) { $size = -s IMAGE; printf "Content-length: %d\n", -s IMAGE; printf "Content-type: image/%s\n\n", substr($image,-3); while (<IMAGE>) { print; } close(IMAGE); } closedir(PIZZA); } exit;
      $image = $images[rand($#images+1)];
      is unnecessarily obtuse. I much prefer:
      $image = $images[rand @images];
      Your version returns the actual image, while the original does a Location to it. Which is more efficient (and in what way). My guess, without actually knowing/understanding the issues involved, is that Returning the location is more efficient because you don't have the single-line effect of <PIZZA> (I'm assuming that the webserver can do efficient largeblock file activities). How much webserver overhead does a Location reference build in comparison?

        The second way (opening the image itself) is probably quicker because the browser only has to open a single connection. If it receives a location directive, then it has to send another request to the site. Further, I doubt that the "single-line effect" of <PIZZA> is much of an issue - even a large gif would get parsed so fast it would be hard to measure, and I doubt the webserver would be able to send it faster.

        P.S. Thanks, merlyn, you are right about the obtuseness. I'd change the original post but then your response wouldn't make much sense. :)

      Hi... I was trying to run this snippet on a page, and all I'm seeing is a broken image icon. All of the images are there and correctly permitted, if the randomly generated file is put in the page, it runs beautifully, but that's not what I am looking for. so I'm curious about the directory. I'm using a "http://your.host.here/path/to/images"-- is this incorrect? I would think it was necessary. does it need to live in the ~user/www/cgi-bin/ directory? I can tell I'm missing something really simple here, and it's fairly frustrating... thanks.
        Ah-ha! The hyperlink led me to talk to a friend and I found that I just needed the script to live in the "cage." Thanks!
RE: Random Image in HTML
by turnstep (Parson) on Apr 25, 2000 at 01:37 UTC
    Some notes:
    • 'opendir' should check its result, perhaps with a "|| die"
    • 'srand' should not be in the subroutine, since it could be called more than once in the same script.
    • The whole subroutine could probably be collapsed into an inner loop.
    • If the directory is empty, the script will enter an infinite loop!
    • Outputing a 'Content-size' to the browser would be nice.
    • This does not actually return the image to the HTML page, but a location. Strange, but workable for most browsers.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://8772]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2025-04-19 02:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.