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