#!/usr/local/bin/perl -w
# by Jamie (jamie@westlake.com)
# asks for starting dir and HTML document root, then uses
# Image::Size to set HEIGHT and WIDTH attrs of all
tags
require 5.0;
use Image::Size;
# configure:
$debug = 0;
#--------------------
$examined_count =0;
$changed_count =0;
$total_errors =0;
$longest_line =0;
$| = 1; # don't buffer output - helps filename status display look right
$here = `pwd`;
chomp($here);
print "Starting directory (return for $here)?";
$startdir = ;
chomp($startdir);
if ($startdir)
{
chdir($startdir) || die "Can't chdir to $startdir.\n";
}
else
{
$startdir=$here;
}
print "Site's HTML document root (return for $here)?";
$docroot = ;
chomp($docroot);
if(!$docroot) {$docroot = $here;}
if(! -d $docroot) {die "Can't see $docroot. Sorry.\n";}
if($docroot =~ /\/$/) {chop($docroot);} # get rid of trailing slash if it's there
# get a list of all the .html files
@htmlfiles = grep { m/\.(\/.*\.s?html)\s*$/; $_ = $1 ? $startdir.$1 : undef; } `du -a`;
while ($current_html_doc = shift(@htmlfiles))
{
chomp($current_html_doc);
show_file("$current_html_doc");
&setImgTagSize($current_html_doc);
$total_errors += $errs_in_file;
$examined_count++;
}
show("$examined_count files examined, $changed_count files changed, $total_errors errors.\n\n");
exit;
sub setImgTagSize
{
($html_filename) = @_;
my($html_file, $temp_filename);
if (open (HTMLDOC, $html_filename))
{
$html_file = join('',());
close(HTMLDOC);
}
else
{
show("Can't read $html_filename\n");
return;
}
$htmlfile_dir = $html_filename;
while (chop($htmlfile_dir) ne "/") {}
$temp_filename = "$htmlfile_dir/imagesizer_tmp$$";
$changed_file = &substImgSizes($html_file);
if ($changed_file eq $html_file)
{
return;
}
if (open (TEMPFILE, ">$temp_filename")) # for writing!
{
print TEMPFILE $changed_file;
close(TEMPFILE);
### delete original file
unlink $html_filename;
### and rename tempfile to original filename
link $temp_filename, $html_filename;
unlink $temp_filename;
print "\rChanged file $html_filename" . " " x 20 . "\n";
$changed_count++;
}
else
{
show("Can't create temp file in $htmlfile_dir\n");
return;
}
}
sub substImgSizes
{
my($file)=@_;
# grab ... from
or
and call substOneImgSize on it
$file =~ s!\
]*)\>!"
"!geis;
return $file;
}
sub substOneImgSize
{
my($imgtagattrs)=@_;
my($imgfile, $imgpath);
if ($debug) {print "\nGot IMG tag attrs: [$imgtagattrs]\n";}
# getting something like:
# SRC="blah" ISMAP ALIGN="right"
# or maybe
# SRC="blah" ALIGN="right" HEIGHT="20" width=100
# set height and width if not present; if one/both is present, don't touch it
if ($imgtagattrs =~ /height/i || $imgtagattrs =~ /width/i)
{
return $imgtagattrs; # already specifying height, width, or both, don't touch
}
# get image filename from src="..." or src = ...
$imgtagattrs =~ m/src\s*=\s*([\"\']?)([^\"\' ]*)\1/i;
$imgfile = $2;
if ($debug) {print "\nImage file found in SRC attr: $imgfile\n";}
if (!$imgfile)
{
show_err("Required SRC attribute not found in IMG tag!");
return $imgtagattrs;
}
# make sure we are in the right directory - the image file could be url-relative or absolute
if ($imgfile =~ /^\//)
{
# leading slash = URL-absolute; prepend document root to get pathname
$imgpath = $docroot . $imgfile;
}
else
{
# no leading slash = URL-relative; chdir to HTML document's dir
$imgpath=$imgfile;
chdir($htmlfile_dir);
}
if (! -f $imgpath)
{
# file doesn't exist in filesystem
if ($imgfile =~ m!^http://!)
{
show_err(" Skipping $imgfile")
}
}
else
{
# get image size via Image::Size module
my($img_width, $img_height) = imgsize($imgpath);
if ($img_width && $img_height)
{
if ($debug) {print "\nGot width $img_width height $img_height\n";}
$imgtagattrs .= " HEIGHT=\"$img_height\" WIDTH=\"$img_width\"";
}
else
{
show_err(" $imgfile exists but I can't get its size.\n");
}
}
return $imgtagattrs;
}
sub show
{
# this cool fn displays messages with CR not LF so they all show on the same line
my($mess) = @_;
$mess_len = length($mess);
$longest_line = ($longest_line >= $mess_len) ? $longest_line : $mess_len;
print "\r$mess" . (" " x ($longest_line - $mess_len));
if ($debug) {print "\n";}
}
sub show_err
{
my($mesg)=@_;
if ($errs_in_file == 0) {$mesg="\n".$mesg;}
show($mesg);
$errs_in_file++;
}
sub show_file
{
my($mesg)=@_;
show("Examining file: $mesg");
$errs_in_file=0;
return;
}