public function maximize($img, $maxWidth, $maxHeight, $outputFormat = null, $targetPath = null)
    {
        if ($targetPath && $this->cache->cached($targetPath)) {
            return;
        }

        list($ow, $oh) = $imgInfo = ImageFile::getSize($img);
        $srcImg = ImageFile::get($img, $imgInfo);

        // calculate new size
        if ($maxWidth >= $ow && $maxHeight >= $oh) {
            if ($targetPath) {
                ImageFile::save($targetPath, $srcImg, ImageFile::getType($img, $outputFormat, $imgInfo));
            }
            return;
        }

        if ($ow >= $oh) {
            $nw = $maxWidth;
            $nh = floor(($nw / $ow) * $oh);
        } else {
            $nh = $maxHeight;
            $nw = floor(($nh / $oh) * $ow);
        }

        if ($nw > $maxWidth) {
            $this->widen($img, $maxWidth, $outputFormat, $targetPath);
            return;
        } elseif ($nh > $maxHeight) {
            $this->heighten($img, $maxHeight, $outputFormat, $targetPath);
            return;
        }

        // save
        $dstImg = $this->resample($srcImg, $nw, $nh, $ow, $oh, $imgInfo);

        ImageFile::save($targetPath ?: $img, $dstImg, ImageFile::getType($img, $outputFormat, $imgInfo));
        ImageFile::clean(array(&$dstImg, &$srcImg));
    }