Grafika\Gd\Editor::resizeFit PHP Method

resizeFit() public method

Resize image to fit inside the given dimension. No part of the image is lost.
public resizeFit ( Image &$image, integer $newWidth, integer $newHeight ) : Editor
$image Image
$newWidth integer Width in pixels.
$newHeight integer Height in pixels.
return Editor
    public function resizeFit(&$image, $newWidth, $newHeight)
    {
        $width = $image->getWidth();
        $height = $image->getHeight();
        $ratio = $width / $height;
        // Try basing it on width first
        $resizeWidth = $newWidth;
        $resizeHeight = round($newWidth / $ratio);
        if ($resizeWidth > $newWidth or $resizeHeight > $newHeight) {
            // Oops, either with or height does not fit
            // So base on height instead
            $resizeHeight = $newHeight;
            $resizeWidth = $newHeight * $ratio;
        }
        $this->_resize($image, $resizeWidth, $resizeHeight);
        return $this;
    }