#!/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> 30 Jul 1996
# modified by Erik Rossen <rossen@planet.ch> 15 May 1999
#
# 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.
#
# 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.6pre.
#

# Anything that can fetch a URL defined on the command line 
# and dump it to stdout will do here. 
$fetcher = 'httpget';	

$out = "imgsizer-out$$";
$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);
		open(TEMP, ">$out")
			|| die("imgsizer: couldn't open tempfile $out \n");
		close(STDIN);
		if (!open(STDIN, "$file")) {
			print STDERR "imgsize: couldn't open $x for input\n";
		} else {
			select(TEMP);
			&sizefix;
			close(TEMP);
			if (`cmp $out $file` && system("cp $out $file")) {
				print STDERR "imgsize: couldn't replace ${file}\n}";
			}
		}
	}
	unlink($out);
}
else {
	$file = "stdin";
	&sizefix;
}

sub dirname
# return filename with the basename part stripped away
{
	local($file) = @_;
	if ($file =~ /\//) {
		$file =~ s:/[^/]*$::;
	} else {
		$file = ".";
	}
	return($file);
}

sub sizefix
# Apply attrfix to the attributes in each image tag
{
	while (<STDIN>) {
		if (/(<IMG\s+)(.*)>/is) {
			print $`; print $1;
			$tag = $2;
			print &attrfix($tag);
			print ">";
		}
		else {
			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\s*=\s*\"([^"]*)\"/is) {
		&error("image \"${attribs}\"' has no SRC attribute");
		return $attribs;
	}
	$url = $1;

	# fetch the image
	if ($url =~ /^http:/) {
		$image = "imgsizer-tmp$$";
		$status = system("$fetcher ${url} >${image}");
		if ($status) {
			&error("can't fetch $1");
			unlink("imgsizer-tmp$$");
			return $attribs;
		}
	}
        elsif ($url =~ /^\//) {
              $image = "./$url";      # This catches local files that are in
				      # virtual root, assuming we're called
				      # from /var/www or equivalent.
        }
        elsif (!($url =~ /^[a-z]+:/)) {
                $image = "$dir/$url";   # This catches local files
	}

	# Don't try to fix up percent sizes
	if ($attribs =~ /[0-9]%/s) {
		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;
	}

	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;
			}
		}
	}

	if ($attribs =~ /WIDTH\s*=\s*["0123456789]+/is) {
		$attribs = "$`WIDTH=\"${xc}\"$'";
	} else {
		$attribs .= " WIDTH=\"${xc}\"";
	}
	if ($attribs =~ /HEIGHT\s*=\s*["0123456789]+/is) {
		$attribs = "$`HEIGHT=\"${yc}\"$'";
	} else {
		$attribs .= " HEIGHT=\"${yc}\"";
	}
	return $attribs;
}
