#!/bin/sh
# Extract (still) image x and y dimensions from file $1 using local tools.
#
# Should work for at least .png, .jpg, .gif images.
# Will try at least ImageMagick 'identify' and 'file'
# Will return an empty result and a non-zero return code in case of error.
#
# Example response:
#     245x267
# for a 245px width, 267px height image.


# Try with ImageMagick 'identify' utility.
# Extract width and height (or nothing in case of error).
if `which identify >/dev/null`; then
    exec identify -ping -format "%wx%h" $1
fi


# Try with 'file' utility.
# On a Mac 10.12.x, file output may look like this:
#     img/SIKP2000/SIKP2000-1.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, progressive, precision 8, 3264x2448, frames 3
# or:
#     img/GBintensity.png: PNG image data, 833 x 332, 8-bit colormap, non-interlaced
# Note the "3264x2448," and "833 x 332," sections,
# not consistent and not even present for all 'file' implementations.
FILEOUT="`file $1`"
FILEOUTDIMS="`echo $FILEOUT | sed -n -e 's/^.* \([1-9][0-9]*\) *x *\([1-9][0-9]*\)[, ].*$/\1x\2/p'`"
if [ "" != "$FILEOUTDIMS" ]; then
    # Success!
    echo $FILEOUTDIMS
    exit 0
fi


# Failed.
exit 1
