#!/usr/bin/perl
-# Works with either Perl4 or Perl5, Perl4 is much faster.
#
# imgsizer -- correct image sizes in WWW pages
+# by Eric S. Raymond <esr@thyrsus.com>
+#
+# Fix up IMG tags in given documents to contain correct sizes. No longer
+# requires the identify(1) program from the ImageMagick suite if you have
+# a decent file(1) and rdjpgcom(1). To fix up sizes for non-local images
+# it requires the httpget(1) script.
+#
+# Works with either Perl4 or Perl5, Perl4 is much faster.
+#
+# Copy, use, and redistribute freely, but don't take my name off it and
+# clearly mark an altered version. Fixes and enhancements cheerfully
+# accepted.
#
-# by Eric S. Raymond <esr@thyrsus.com> 30 Jul 1996
+# Changelog:
+#
+# Originally created by Eric S. Raymond <esr@thyrsus.com> 30 Jul 1996
#
# Modified by Erik Rossen <rossen@planet.ch> 15 May 1999
#
+# Added the --nomagick switch, to use file(1) and rdjpgcom(1)
+# to determine the image size instead of identify(1) from the
+# ImageMagick suite.
+#
# Modified by Michael C. Toren <michael@toren.net> 18 Aug 2000;
-# Fixed bug where the SRC attribute needed to be in quotes, improved
-# command line argument parsing (but it could still use some work),
-# added -q switch to omit quotes when generating tags, and -l switch
-# to generate lowercase tags.
#
-# Fix up IMG tags in given documents to contain correct sizes.
-# No longer requires the identify(1) program from the ImageMagick suite if
-# you have a decent file(1) and rdjpgcom(1).
-# To fix up sizes for non-local images it requires the httpget(1) script.
+# Fixed bug where the SRC attribute's value needed to be in quotes,
+# improved command line parsing (but it could still use some work),
+# added -q switch to omit quotes when generating tags, and -l switch
+# to generate lowercase tags. -mct
#
-# Copy, use, and redistribute freely, but don't take my name off it and
-# clearly mark an altered version. Fixes and enhancements cheerfully
-# accepted.
+# Modified by Michael C. Toren <michael@toren.net> 19 Aug 2000;
#
-# This is version 1.6pre+mct
+# Improved the command line parsing some more, now looks for additional
+# arguments via an IMGSIZER envrionmental variable, added the -d switch
+# to set the DocumentRoot, -v switch to display version information,
+# and -h switch to display usage information. -mct
#
+$version = "1.8, 30 Aug 2000";
+
+$splash = <<EOT;
+imgsizer version $version, Eric S. Raymond <esr\@thyrsus.com>
+See my home page at <http://www.tuxedo.org/~esr> for updates.
+EOT
+
+$usage = <<EOT;
+Usage: imgsizer [OPTIONS] [HTML File]
+
+Options:
+
+ -v, --version
+
+ Display version information and exit.
+
+ -h, --help
+
+ Display usage information.
+
+ -m, --no-magick
+
+ Use file(1) and rdjpgcom(1) to determine the image size, instead
+ of identify(1) from the ImageMagick suite, which is the default.
+
+ -d <directory>, --document-root <directory>
+
+ Directory where aboslute image filenames (i.e, ones which contain
+ a leading "/") may be found.
+
+ -l, --lowercase
+
+ Output HTML tag attributes imgsizer generates in lowercase.
+
+ -q, --no-quotes
+
+ Omit the quotes in HTML tag attributes imgsizer generates.
+EOT
+
# Anything that can fetch a URL defined on the command line
# and dump it to stdout will do here.
$fetcher = 'httpget';
-# Output uppercase tags, surrounded by quotes, by default
+# Output uppercase tags, surrounded by quotes, by default.
$lower = 0;
$quotes = 1;
+# Set the default DocumentRoot to the current working directory.
+$root = ".";
+
$out = "imgsizer-out$$";
$dir = "."; # NOTE: if you are doing <yourfile , make sure that pwd is correct!
-$magick=1; # using ImageMagick by default
+$magick = 1; # using ImageMagick by default
$/='>'; # define INPUT_RECORD_SEPARATOR to split on HTML marker boundaries
+# Look in IMGSIZER environmental variable for additional command line options
+unshift @ARGV, split /\s+/, $ENV{IMGSIZER}
+ if ($ENV{IMGSIZER});
+
while ($i = shift @ARGV)
{
last if ($i eq "--");
last;
}
- if ($i =~ /^(--nomagick|-m)$/i)
+ if ($i =~ /^-(\w{2,})$/i)
+ {
+ # Hack to parse "-foo" as "-f -o -o"
+ unshift @ARGV, map { "-$_" } split //, $1;
+ next;
+ }
+
+ if ($i =~ /^(--ver(sion)?|-v)$/i)
+ {
+ die "\n$splash\n";
+ }
+
+ if ($i =~ /^(--help|--usage|-h)$/i)
+ {
+ die "\n$splash\n$usage\n";
+ }
+
+ if ($i =~ /^(--no-?magick|-m)$/i)
{
$magick = 0;
next;
}
+ if ($i =~ /^(--document-?root(=.*)?|-d)$/i)
+ {
+ if ($2 =~ /^=(.*)/)
+ {
+ $root = $1;
+ }
+
+ else
+ {
+ die "Error: --document-root requires a directory name\n"
+ unless (@ARGV);
+
+ $root = shift @ARGV;
+ }
+
+ die "Error: --document-root, \"$root\", is not a directory ",
+ "or does not exist\n"
+ unless (-d $root);
+ next;
+ }
+
if ($i =~ /^(--lower-?(case)?|-l)$/i)
{
$lower = 1;
next;
}
- warn "Warning: Ignoring unknown option: $i\n";
+ die "Error: Unknown option: $i\n";
}
if (@ARGV) {
return $attribs;
}
}
- elsif ($url =~ /^\//) {
- $image = "./$url"; # This catches local files that are in
+ elsif ($url =~ m!^/!) {
+ $image = $root . $url; # This catches local files that are in
# virtual root, assuming we're called
# from /var/www or equivalent.
}
-.TH IMGSIZER 1 "28 Jul 1997"
+.TH IMGSIZER 1 "19 Aug 2000"
.AT 3
.SH NAME
imgsizer \- automatically splice in height and width params for HTML IMG tags
.SH SYNOPSIS
-.B imgsizer [--nomagick] [-q] [-l] [file...]
+.B imgsizer [OPTIONS] [HTML File]
.PP
+.SH OPTIONS
+.TP
+.B \-v, \-\-version
+Display version information and exit.
+.TP
+.B \-h, \-\-help
+Display usage information.
+.TP
+.B \-m, \-\-no\-magick
+Use file(1) and rdjpgcom(1) to determine the image size, instead
+of identify(1) from the ImageMagick suite, which is the default.
+.TP
+.B \-d <directory>, \-\-document\-root <directory>
+Directory where aboslute image filenames (i.e, ones which contain
+a leading "/") may be found.
+.TP
+.B \-l, \-\-lowercase
+Output HTML tag attributes imgsizer generates in lowercase.
+.TP
+.B \-q, \-\-no\-quotes
+Omit the quotes in HTML tag attributes imgsizer generates.
.SH DESCRIPTION
The
.B imgsizer
argument file is not actually modified until the script completes a
successful conversion pass.
.PP
-The -q option suppresses generation of string quotes in generated
-attributes. The -l option generates attributes in lower case.
+The \-q option suppresses generation of string quotes in generated
+attributes. The \-l option generates attributes in lower case.
+The \-d <directory> option sets the DocumentRoot, which images with an
+absolute filename (i.e., ones which contain a leading "/") may be found.
+If none is specified, the DocumentRoot defaults to the current working
+directory.
+.PP
+Additional options may also be specified in the environmental variable
+"IMGSIZER". For example, to avoid typing "imgsizer -options ..." each
+time imgsizer is invoked, you might tell sh (or one of it's decendents):
+.PP
+.RS
+IMGSIZER="\-options"; export IMGSIZER
+.RE
+.PP
+or, if you use csh:
+.PP
+.RS
+setenv IMGSIZER "\-options"
+.RE
.PP
This script is written on Perl, and thus requires a Perl interpreter
on the host system. It also requires either the
.IR identify (1)
utility distributed in the freeware ImageMagick suite of image-display
-and manipulation tools or (with --nomagick) a modern version of
+and manipulation tools, or, with the \-\-no\-magick option, a modern
+version of
.IR file (1)
and
-.IR rdjpgcom (the --nomagick mode is faster). These
+.IR rdjpgcom (1)
+(the --no-magick mode is faster). These
utilities are used to extract sizes from the images;
.B imgsizer
itself has no knowledge of graphics formats. The script will handle any
.IR httpget (1)
are needed and not present.
.SH AUTHOR
-Eric S. Raymond <esr@thyrsus.com>. The --nomagick option is by Erik Rossen.
-See my home page at http://www.tuxedo.org/~esr for updates.
+Originally created by Eric\ S.\ Raymond\ <esr@thyrsus.com>. Additional code
+contributed by Erik\ Rossen and Michael\ C.\ Toren\ <michael@toren.net>.
+.SH AVAILABILITY
+For updates, see <http://www.tuxedo.org/~esr>
.SH SEE ALSO
.IR identify (1),
.IR httpget (1).