# imgsizer -- correct image sizes in WWW pages
#
# by Eric S. Raymond <esr@thyrsus.com> 30 Jul 1996
+# modified by Erik Rossen <rossen@planet.ch> 15 May 1999
#
# Fix up IMG tags in given documents to contain correct sizes.
-# Requires the identify(1) program from the ImageMagick suite.
+# 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.
#
# Copy, use, and redistribute freely, but don't take my name off it and
# clearly mark an altered version. Fixes and enhancements cheerfully
# accepted.
#
-# This is version 1.5.
+# This is version 1.6pre.
#
# Anything that can fetch a URL defined on the command line
$fetcher = 'httpget';
$out = "imgsizer-out$$";
-if (@ARGV) {
+$dir = "."; # NOTE: if you are doing <yourfile , make sure that pwd is correct!
+$magick=1; # using ImageMagick by default
+$/='>'; # define INPUT_RECORD_SEPARATOR to split on HTML marker boundaries
+
+if ("$ARGV[0]" =~ /^(--nomagick|-m)$/i) {
+ $magick=0;
+ shift;
+}
+
+if (@ARGV) {
foreach $x (@ARGV) {
$file = $x;
$dir = &dirname($x);
return($file);
}
-sub getchar
-{
- $pushback = <STDIN> if (length($pushback) == 0);
- $char = substr($pushback, 0, 1);
- $pushback = substr($pushback,1);
- return $char;
-}
-
-
sub sizefix
# Apply attrfix to the attributes in each image tag
{
while (<STDIN>) {
- while (/<IMG\s+/i) {
- print $`; print $&;
- flush;
- $pushback = $';
- $tag = '';
- while (($char = &getchar) ne '>') {
- $tag .= $char;
- }
+ if (/(<IMG\s+)(.*)>/is) {
+ print $`; print $1;
+ $tag = $2;
print &attrfix($tag);
print ">";
- $_ = $pushback;
}
- print $_;
+ else {
+ print $_;
+ }
}
}
{
$attribs = $_[0];
- if (!($attribs =~ /SRC="([^"]*)"/i)) {
+ if ($attribs !~ /SRC\s*=\s*\"([^"]*)\"/is) {
&error("image \"${attribs}\"' has no SRC attribute");
return $attribs;
}
}
# Don't try to fix up percent sizes
- if ($attribs =~ /[0-9]%/) {
+ if ($attribs =~ /[0-9]%/s) {
return $attribs;
}
- ($name, $size) = split(' ', `identify ${image}`);
- unlink("imgsizer-tmp$$");
- if ($name =~ /:/) {
- &error("can't analyze included image ${image}");
- return $attribs;
+ if ($magick) {
+ ($name, $size) = split(' ', `identify ${image}`);
+ unlink("imgsizer-tmp$$");
+ if ($name =~ /:/) {
+ &error("can't analyze included image ${image}");
+ return $attribs;
+ }
+ if (!($size=~ /([0-9]+)x([0-9]+)/)) {
+ &error("size field ${size} is ill-formed");
+ return $attribs;
+ }
+ $xc = $1;
+ $yc = $2;
}
- if (!($size=~ /([0-9]+)x([0-9]+)/)) {
- &error("size field ${size} is ill-formed");
- return $attribs;
+
+ else {
+ $result = `file ${image}`;
+ if ($result =~ /JPEG/) {
+ $result = `rdjpgcom -verbose ${image}`;
+ unlink("imgsizer-tmp$$");
+ if ($result =~ /rdjpgcom:/) {
+ &error("can't analyze included image ${image}");
+ return $attribs;
+ }
+ else {
+ $result =~ /(\d+)w \* (\d+)h/;
+ $xc = $1;
+ $yc = $2;
+ }
+ }
+ else {
+ unlink("imgsizer-tmp$$");
+ if ($result !~ /(\d+) x (\d+)/) {
+ &error("can't analyze included image ${image}");
+ return $attribs;
+ }
+ else {
+ $xc = $1;
+ $yc = $2;
+ }
+ }
}
- $xc = $1;
- $yc = $2;
- if ($attribs =~ /WIDTH=[0123456789]+/i) {
- $attribs = "$`WIDTH=${xc}$'";
+ if ($attribs =~ /WIDTH\s*=\s*["0123456789]+/is) {
+ $attribs = "$`WIDTH=\"${xc}\"$'";
} else {
- $attribs .= " WIDTH=${xc}";
+ $attribs .= " WIDTH=\"${xc}\"";
}
- if ($attribs =~ /HEIGHT=[0123456789]+/i) {
- $attribs = "$`HEIGHT=${yc}$'";
+ if ($attribs =~ /HEIGHT\s*=\s*["0123456789]+/is) {
+ $attribs = "$`HEIGHT=\"${yc}\"$'";
} else {
- $attribs .= " HEIGHT=${yc}";
+ $attribs .= " HEIGHT=\"${yc}\"";
}
return $attribs;
}
.SH NAME
imgsizer \- automatically splice in height and width params for HTML IMG tags
.SH SYNOPSIS
-.B imgsizer [file...]
+.B imgsizer [--nomagick] [file...]
.PP
.SH DESCRIPTION
The
successful conversion pass.
.PP
This script is written on Perl, and thus requires a Perl interpreter
-on the host system. It also requires the
+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. The
-.IR identify (1)
-utility is used to extract sizes from the images;
+and manipulation tools or (with --nomagick) a modern version of
+.IR file (1)
+and
+.IR rdjpgcom (the --nomagick 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
image format known to
.IR httpget (1)
must be executable and present in your execution path;
.B imgsizer
-will complain and fail if either
-.IR identify (1)
-or
+will complain and fail if either the required size-reporting tools or
.IR httpget (1)
-is needed and not present.
+are needed and not present.
.SH AUTHOR
-Eric S. Raymond <esr@thyrsus.com>. See my home page at
-http://www.ccil.org/~esr for updates.
+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.
.SH SEE ALSO
.IR identify (1),
.IR httpget (1).