#!/usr/bin/perl -w use strict; # This script was written by Rick Dean. # It will make a lame attempt to create a _half.html # file with bigger (half size) thumbnails if ($#ARGV == -1) { # if no command line arguments printf("usage: $0 filename.html [..]\n"); exit(1); }; sub halfhtml { my ($filename) = @_; open(INFILE,"<$filename") || die "couldn't open $filename"; print("process $filename\n"); my $sourcedoc; read(INFILE,$sourcedoc,1000000) || die "couldn't read $filename"; close(INFILE); my $outfilename = $filename; $outfilename =~ s/.html$/_half.html/i || die "$filename doesn't end in .html"; my $changed=0; my @html = split('<',$sourcedoc); for(my $i=0;$i < $#html;$i++) { # for every tag if ($html[$i] =~ /(^[^>]*)/) { # grab tag contents (everything until > ) my $tag=$1; next unless $tag =~ /^img\s/i; # must begin with img next unless $tag =~ /src=(\S*)/; # must have src= my $src=$1; $src =~ s/^"(.*)"$/$1/; # strip bounding double quotes next unless $src =~ /.jpg$/i; # must be a link to a picture next if $src =~ /_half.jpg$/i; # but not a half picture my $newsrc = $src; $newsrc =~ s/_small.jpg$/_half.jpg/i; # change the image name if(! -s $newsrc) { # if file doesn't exist (or has zero length) print("couldn't find $newsrc\n"); next; # skip because half image doesn't exist }; next unless -s $newsrc; # half must exist and be non-zero in length $html[$i] =~ s/width=([^>\s]*)//i; # delete width attributes $html[$i] =~ s/height=([^>\s]*)//i; # delete height attributes my($width,$height) = pixelSizeOfFile($newsrc); $html[$i] =~ s/src=([^>\s]*)/src="$newsrc" WIDTH=$width HEIGHT=$height/i or print("damn\n"); $changed=1; }; }; if($changed) { open(OUTFILE,">$outfilename") || die "couldn't open $filename"; print OUTFILE join('<',@html); close(OUTFILE); my $mode = (stat($filename))[2]; chmod($mode,$outfilename); }; } for my $filename (@ARGV) { next if $filename =~ /_half.html$/i; # if end in _half.html case insensitive next if ! $filename =~ /.html$/i; # if doesn't end in .html halfhtml($filename); }; # jpegsize : gets the width and height (in pixels) of a jpeg file # Andrew Tong, werdna@ugcs.caltech.edu February 14, 1995 # modified slightly by alex@ed.ac.uk sub jpegsize { my($JPEG) = @_; my($done)=0; my($c1,$c2,$ch,$s,$length, $dummy)=(0,0,0,0,0,0); my($a,$b,$c,$d); if(defined($JPEG) && read($JPEG, $c1, 1) && read($JPEG, $c2, 1) && ord($c1) == 0xFF && ord($c2) == 0xD8 ){ while (ord($ch) != 0xDA && !$done) { # Find next marker (JPEG markers begin with 0xFF) # This can hang the program!! while (ord($ch) != 0xFF) { return(0,0) unless read($JPEG, $ch, 1); } # JPEG markers can be padded with unlimited 0xFF's while (ord($ch) == 0xFF) { return(0,0) unless read($JPEG, $ch, 1); } # Now, $ch contains the value of the marker. if ((ord($ch) >= 0xC0) && (ord($ch) <= 0xC3)) { return(0,0) unless read ($JPEG, $dummy, 3); return(0,0) unless read($JPEG, $s, 4); ($a,$b,$c,$d)=unpack("C"x4,$s); return ($c<<8|$d, $a<<8|$b ); } else { # We **MUST** skip variables, since FF's within variable names are # NOT valid JPEG markers return(0,0) unless read ($JPEG, $s, 2); ($c1, $c2) = unpack("C"x2,$s); $length = $c1<<8|$c2; last if (!defined($length) || $length < 2); read($JPEG, $dummy, $length-2); } } } return (0,0); } sub pixelSizeOfFile { my($filename) = @_; my($imageData); my($width,$height); open(IMAGEFILE,$filename); ($width,$height) = jpegsize(\*IMAGEFILE); close(IMAGEFILE); return ($width,$height); }