Make HTML image tags from a list of graphics files

This script uses the Perl module Image::Magick to create HTML <img> tags from a list of images. You call it like this:

get-sizes.pl *.jpg
and it outputs img tags like this:
<img src='bare-valve-250.jpg' height='202' width='250'>
<img src='broken-rubber-250.jpg' height='615' width='771'>
<img src='new-rubber-fitted-250.jpg' height='176' width='250'>
<img src='perished-rubber-250.jpg' height='199' width='250'>
<img src='soundtrack.jpg' height='301' width='457'>

Here is the script, formatted:

#!/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
}

(download)


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