#!/usr/bin/perl # # make_alt # Generate alternate formats using NetPBM # # Hugo Haas, 4 July 2002 # # $Id: make_alt 2259 2003-09-14 09:20:39Z hugo $ use strict; use Image::Info qw(image_info dim); use Getopt::Long; my $thumb_suffix = "-th"; my $thumb_height = 128; my $small_suffix = "-sm"; my $small_size = 640; my $med_suffix = "-med"; my $med_size = 1024; my $do_med = 0; my $do_small = 0; GetOptions('m' => \$do_med, 's' => \$do_small, 'a' => sub { $do_med = $do_small = 1; } ); sub run() { my ($command)= (@_); print STDERR "Running: $command\n"; my $ret = system($command); return($ret); } sub gen_alt() { my ($name, $suffix, $convopt, $size) = @_; my $dest = $name; $dest =~ s/(\.[^.]+)$/${suffix}$1/; scale: my $ret = &run("pnmscale $convopt $size $name.pnm > $dest.pnm"); if ($ret != 0) { print STDERR "Error: rerunning command...\n"; goto scale; } &run("pnmtojpeg --optimize $dest.pnm > $dest"); unlink("$dest.pnm"); &run("jhead -te $name $dest"); } my $file; foreach $file (@ARGV) { if ( ! -r $file ) { print STDERR "Can't read $file! skipping it...\n"; next; } if ($file =~ m/${thumb_suffix}|${small_suffix}|${med_suffix}/) { print STDERR "Not master: skipping it...\n"; next; } print STDERR "Processing $file:\n"; # Prepare generation: convert to PNM &run("jpegtopnm $file > $file.pnm"); # Generate thumbnail &gen_alt($file, $thumb_suffix, '-h', $thumb_height); # Generate alternate sizes my $info = image_info($file); my($w, $h) = dim($info); my $convopt; if ($w > $h) { $convopt = '-w'; } else { $convopt = '-h'; } &gen_alt($file, $small_suffix, $convopt, $small_size) if $do_small; &gen_alt($file, $med_suffix, $convopt, $med_size) if $do_med; # Remove temporary PNM file unlink("$file.pnm"); }