Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Display all images in a given directory

by cjf (Parson)
on Feb 02, 2002 at 03:46 UTC ( [id://142864]=sourcecode: print w/replies, xml ) Need Help??
Category: CGI Programming
Author/Contact Info /msg cjf
Description: Simple little script that reads through a directory, grabs all files with png/jpg/gif extensions and prints them to the browser.
#!/usr/bin/perl -wT

use strict;
use CGI;
use Image::Size;

my $q = new CGI;

my $imageDir = "./";
my @images;

opendir DIR, "$imageDir" or die "Can't open $imageDir $!";
    @images = grep { /\.(?:png|gif|jpg)$/i } readdir DIR;
closedir DIR;

print $q->header("text/html"),
      $q->start_html("Images in $imageDir"),
      $q->p("Here are all the images in $imageDir");

foreach my $image (@images) {
    my ($width, $height) = imgsize("$image");
    print $q->p(
            $q->a({-href=>$image},
              $q->img({-src=>$image,
                       -width=>$width,
                       -height=>$height})
            )
    );
}

print $q->end_html;

Update: Applied gav^'s and dws's suggestions listed below.

Update: Included jarich's code to use CGI.pm to print out the lines in the foreach loop.


Replies are listed 'Best First'.
Re: Display all images in a given directory
by gav^ (Curate) on Feb 02, 2002 at 03:55 UTC
    Some random points:
    • Why assign "0" to $count instead of 0?
    • grep is your friend: my @files = grep { /\.(?:png|gif|jpg)$/ } readdir DIR;
    • Why use CGI's HTML code, but then don't use it to write the images?

    gav^

      Thanks for the pointers :)

      I like your grep method far better. As for using the one here document I did that because I thought there were problems with quotes if I use the CGI.pm print $q->p("") method. In hindsight I'm sure there is a way around this (do I even need the quotes in the cgi print method?).

      Update jarich pointed out a nice way to use CGI.pm to to print out the paragraphs. I've included that in the code now.

Re: Display all images in a given directory
by dws (Chancellor) on Feb 02, 2002 at 03:56 UTC
    It's generally a good idea to run code before posting it. Your <href> has a flaw in it.

    For style points, use Image::Size to get sizes for HEIGHT and WIDTH tags. That'll make life easier on the browser side.

    You might want to make that regex case independent, in case you end up with a .GIF.

    Since you're already using CGI.pm methods/subroutines to generate some of your tags, you might consider going all the way.

      Hi,

      I think you must have viewed this just as I was fixing the '-' typo in the href tag (mozilla seems to be having issues with cutting and pasting, so I typed the code out directly into the text field and missed the typo the first time around.)

      I like your suggestion about the Image::Size module, I hadn't heard of it before but I'll definately check it out now. I'll also add on the /i to ignore case as you suggested. Thanks again for the comments :)

      Update: the script now uses the Image::Size module as suggested by dws. Thanks again to everyone for the comments :).

Re: Display all images in a given directory
by merlyn (Sage) on Feb 02, 2002 at 14:54 UTC
Re: Display all images in a given directory
by rjray (Chaplain) on Feb 12, 2002 at 03:14 UTC

    It's nice to see Image::Size getting some play :-)

    Rather than rely on the file name's extension, you could just loop over all the files through the imgsize call. Those that don't map to a known image type will return an error of the form "Data stream is not a known image file format" in the third element of the returned array. If it is an image, the 3-letter acronym for the type will be in the 3rd element. You could then actually note which files are images, but not displayable by browsers (such as TIF, BMP, etc.).

    OR, if you aren't interested in files that are not GIF, JPG or PNG, you could eliminate the use of $width and $height completely, by using the attr_imgsize call:

    use Image::Size 'attr_imgsize'; ... $q->img({-src => $image, attr_imgsize($image))

    --rjray

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-10-13 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.