upating <ch.ellips/> handling to current practices
[project-aon.git] / common / scripts / fix-image-sizes.pl
1 #!/usr/bin/env perl 
2
3 use warnings;
4 use strict;
5
6 use File::Spec;
7 use Image::Size;
8
9 my $PROG_NAME = 'fix-image-sizes.pl';
10 my $USAGE = "typical usage:\t${PROG_NAME} *.png <input.xml >output.xml\n";
11 die $USAGE if $#ARGV < 0;
12
13 my %image_widths = ( );
14 my %image_heights = ( );
15
16 while( my $file = shift @ARGV ) {
17     my( $volume, $directory, $filename ) = File::Spec->splitpath( $file );
18     ( $image_widths{ $filename }, $image_heights{ $filename } ) = 
19         imgsize( $file );
20 }
21
22 while( my $line = <> ) {
23     if( $line =~ m{<instance.*src="([^"]+)"} ) {
24     my $image = $1;
25         if( exists $image_widths{$image} && defined $image_widths{$image} ) {
26             my $image = $1;
27             if( $line =~ m{width="[^"]*"} ) {
28                 $line =~ s/width="[^"]*"/width="$image_widths{$image}"/;
29             }
30             else {
31                 $line =~ s/(src="[^"]*")/$1 width="$image_widths{$image}"/;
32             }
33
34             if( $line =~ m{height="[^"]*"} ) {
35                 $line =~ s/height="[^"]*"/height="$image_heights{$image}"/;
36             }
37             else {
38                 $line =~ s/(src="[^"]*")/$1 height="$image_heights{$image}"/;
39             }
40         }
41     }
42     print $line;
43 }