Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

RE: Random Image in HTML

by turnstep (Parson)
on Apr 25, 2000 at 02:26 UTC ( #8830=note: print w/replies, xml ) Need Help??


in reply to Random Image in HTML

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;

Replies are listed 'Best First'.
RE: RE: Random Image in HTML
by merlyn (Sage) on Apr 26, 2000 at 01:17 UTC
    $image = $images[rand($#images+1)];
    is unnecessarily obtuse. I much prefer:
    $image = $images[rand @images];
RE: RE: Random Image in HTML
by swiftone (Curate) on May 17, 2000 at 23:18 UTC
    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. :)

RE: RE: Random Image in HTML
by brick (Sexton) on May 27, 2000 at 03:07 UTC
    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!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2023-11-29 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?