Supplying img sizes automatically

What if any tools are available for reading HTML, finding missing width and height attributes on img elements, and filling in the relevant values? So far all I’ve found is this Perl Script from Randal Schwartz circa 1999 and this Perl script based on ImageMagik from Marc Merlins. I haven’t been able to resolve the dependencies for Schwartz’s script yet. Merlins’ runs, but is not XHTML savvy. That may be easy to fix though.

BBEdit almost does this, but it replaces existing height and width attributes too, including ones you’ve deliberately set to a different size. I only want to fill in missing height and width attributes, not change existing ones.

I’d love to find a simple open source GUI tool that could pull this off for an entire site.

4 Responses to “Supplying img sizes automatically”

  1. John Cowan Says:

    http://www.catb.org/~esr/imgsizer/ should do the job for you; a couple of tests show it working fine with both HTML and XHTML styles of image elements. You’ll need the -n option to suppress overwriting explicitly set heights and widths.

    There’s no GUI, but why should there be? If you want to process a whole site, then find /var/www/root -name '*.html' -exec imgsizer -n \; is your friend, substituting of course the path to your document root.

  2. John Cowan Says:

    Arrgh. Make that find /var/www/root -name '*.html' -exec imgsizer -n {} \; instead. (Oh, for a Preview button!)

  3. Elliotte Rusty Harold Says:

    imgsizer is the best one I’ve found yet. Thanks. However it’s still questionable. It’s not using a real parser and it turns these:

    <img src="cup.gif"  height="67" alt="Cup of " />
    <img src="cup.gif" width="89" alt="Cup of "  />

    into these

    <img src="cup.gif"  height="67" alt="Cup of "  width="89" height="67"/>
    <img src="cup.gif" width="89" alt="Cup of "   width="89" height="67"/>

    I’m considering just writing this as an XSLT extension function.

  4. Randal L. Schwartz Says:

    It’s amazing how a throw-away program I wrote under a deadline for a column is still the simplest way to do something, eight years later. :)

Leave a Reply