« Jetlag | HomePage | Variations of the OODA Loop 6, Bibliography »

Sunday, June 04, 20061149409500

Perl Code for Posting Images of China

From the Forbidden City to the ER, the beautiful Fragrant Hills to the mucous Pacific Ocean, I photoblogged the Chinese People's Republic in series whose most popular posts was on breakfast cereals (*sigh*).

The most time-consuming and tedious part of the posts was actually creating the web page. By then I had already selected, cropped, and resized the images to display on tdaxp, and the prospect of creating all the img and href tags was tiring. So I worked smarter, not harder, and wrote a perl script to do the work for me.

The process reminded me of similar work at Animation Factory, whose online UI I wrote.

The code for the script, which uses the Image::Size module, is below. (Code for my other recent series, Redefining the Gap, is also available).


use Image::Size 'html_imgsize';

$link = "http://foo.bar.com/images"; # online folder goes here
$folder = "C:/foo/bar/images"; # local folder goes here
$r_n = {};
$r_h = {};
$m_n = {};
$m_h = {};

getimgs($folder);

foreach $key (sort keys %m_n) {
$rn = $r_n{$key};
$mn = $m_n{$key};
$mh = $m_h{$key};

#print $key . " name " . $r_n{$key} . " reg height " . $r_h{$key} . " med height " .$m_h{$key} . "n";

print "<a href="$link/$rn"&rt;<img src="$link/$mn" $mh border="0" / &rt;</a&rt;nn";
}

sub getimgs {
my $folder = shift(@_);

opendir(IMGS,$folder) || die "Couldn't open $folder: $!";
@files = readdir(IMGS);
closedir(IMGS);

foreach $file (@files) {
if ($file =~ m/.jpg/i) {
if ($file =~ m/_md.jpg/i) {
$base = $file;
$base =~ s/_md.jpg//i;

$m_n{$base} = $file;
$m_h{$base} = html_imgsize("$folder/$file");

#print "MD file is $file base $basen";

} else {
$base = $file;
$base =~ s/.jpg//i;

$r_n{$base} = $file;
#$r_h{$base} = html_imgsize("$folder/$file");

#print "Reg file is $file base $basen";

}

}

}

}