usualoma
11/29/2009 - 9:29 PM

gistfile1.pl

sub _width_height {
    my $src = shift;

    my $content = get($src);
    if (! $content) {
        return '';
    }
    my ($x, $y) = imgsize(\$content);
    return ' width="' . $x . '" height="' . $y .'" ';
}

sub _insert_width_height {
    my $text = shift;

    my $padding = '(((?!width=)(?!height=)[^>])*)';
    my $src = '(src="([^"]*)")';

    $text =~ s/(<\s*img\s+)$padding$src$padding(\/?>)/
        $1 . $2 . $4 . _width_height($5) . $6 . $8
    /ige;

    $text;
}