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 ""-- 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!