Print sizes of all images in a directory

This script gets a list of everything in the current directory which looks like an image file and prints its size to standard output using the Image::Magick Perl module. This is for use in adding sizes to a web page.

#!/home/ben/software/install/bin/perl
use warnings;
use strict;
use Image::Magick;
use Getopt::Long;
use autodie;
my $dir;
my $output_file;
my $r = GetOptions ("dir:s" => \$dir,
                    "output:s" => \$output_file,
                ); 
if (! $dir) {
    $dir = ".";
}
my @images;
if (@ARGV) {
    @images = @ARGV;
}
else {
    @images = <$dir/*>;
}
my $output = \*STDOUT;
if ($output_file) {
    open $output, ">:encoding(utf8)", $output_file;
}

for my $image (@images) {
    if ($image !~ /\.(?:jpe?g|png|gif)/i) {
        next;
    }
    my $im = Image::Magick->new ();
    my $status = $im->Read ($image);
    if ($status) {
        warn "Problem $status";
        next;
    }
    my ($height, $width) = $im->Get (qw/height width/);
    my $name = $image;
    $name =~ s~.*/~~;
    print <<EOF;
<img src='$name' height='$height' width='$width'>
EOF
}

Copyright © Ben Bullock 2009-2023. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer