From: Eric S. Raymond Date: Tue, 30 Jul 1996 15:18:08 +0000 (+0000) Subject: Initial revision X-Git-Tag: 1.6~11 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=efe8718d6b7684e40584d761723ba8ceaa134266;p=imgsizer.git Initial revision --- efe8718d6b7684e40584d761723ba8ceaa134266 diff --git a/imgsizer b/imgsizer new file mode 100755 index 0000000..ded6833 --- /dev/null +++ b/imgsizer @@ -0,0 +1,129 @@ +#!/usr/bin/perl +# Works with either Perl4 or Perl5, Perl4 is much faster. +# +# imgsizer -- correct image sizes in WWW pages +# +# Fix up IMG tags in given documents to contain correct sizes. +# Requires the identify(1) program from the ImageMagick suite. +# To fix up sizes for non-local images it requires the httpget(1) script. +# +# The man page for this program is included at the end of this file and can be +# viewed using the command 'nroff -man imgsizer'. +# +# by Eric S. Raymond 30 Jul 1996 +# 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.0. +# + +if (@ARGV) { + $out = "imgsizer-out$$"; + foreach $x (@ARGV) { + $file = $x; + open(TEMP, ">$out") + || die("imgsizer: couldn't open tempfile $out \n"); + close(STDIN); + if (!open(STDIN, $file)) { + print STDERR "imgsize: couldn't open ${file} for input\n"; + } else { + select(TEMP); + &sizefix; + close(TEMP); + if (!rename($out, $file)) { + print STDERR "imgsize: couldn't replace ${file}\n}"; + } + } + } + unlink($out); +} +else { + $file = "stdin"; + &sizefix; +} + +sub getchar +{ + $pushback = <> 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 (<>) { + while (/') { + $tag .= $char; + } + print attrfix($tag); + print ">"; + $_ = $pushback; + } + print $_; + } +} + +sub error +{ + print STDERR "\"${file}\", line $.: $_[0]\n"; +} + + +sub attrfix +# fix up a space-separated sequence of image attributes +{ + $attribs = $_[0]; + + if (!($attribs =~ /SRC="([^"]*)"/i)) { + &error("image '${attribs}' has no SRC attribute"); + return $attribs; + } + $url = $1; + + if ($url =~ /^http:/) { + $image = "imgsizer-tmp$$"; + $status = system("httpget ${url} >${image}"); + if ($status) { + &error("can't fetch $1"); + unlink("imgsizer-tmp$$"); + return $attribs; + } + } + else { + $image = $url; # This catches local files + } + + ($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 ($attribs =~ /WIDTH=[0123456789]+/) { + $attribs = "$`WIDTH=${xc}$'"; + } else { + $attribs .= " WIDTH=${xc}"; + } + if ($attribs =~ /HEIGHT=[0123456789]+/) { + $attribs = "$`HEIGHT=${yc}$'"; + } else { + $attribs .= " HEIGHT=${yc}"; + } + return $attribs; +}