Implements an adaptive resize function just like the phpthumbs library implementation