src/BaseImage.php
protected static function getBox(BoxInterface $sourceBox, $width, $height, $keepAspectRatio = true)
    {
        if ($width === null && $height === null) {
            throw new InvalidParamException('Width and height cannot be null at same time.');
        }

        $ratio = $sourceBox->getWidth() / $sourceBox->getHeight();
        if ($keepAspectRatio === false) {
            if ($height === null) {
                $height = ceil($width / $ratio);
            } elseif ($width === null) {
                $width = ceil($height * $ratio);
            }
        } else {
            if ($height === null) {
                $height = ceil($width / $ratio);
            } elseif ($width === null) {
                $width = ceil($height * $ratio);
            } elseif ($width / $height > $ratio) {
                $width = $height * $ratio;
            } else {
                $height = $width / $ratio;
            }
        }

        return new Box($width, $height);
    }
protected static function getBox(BoxInterface $sourceBox, $width, $height, $keepAspectRatio = true)
    {
        if ($width === null && $height === null) {
            throw new InvalidParamException('Width and height cannot be null at same time.');
        }

        $ratio = $sourceBox->getWidth() / $sourceBox->getHeight();
        if ($keepAspectRatio === false) {
            if ($height === null) {
                $height = ceil($width / $ratio);
            } elseif ($width === null) {
                $width = ceil($height * $ratio);
            }
        } else {
            if ($height === null) {
                $height = ceil($width / $ratio);
            } elseif ($width === null) {
                $width = ceil($height * $ratio);
            } elseif ($width / $height > $ratio) {
                $width = $height * $ratio;
            } else {
                $height = $width / $ratio;
            }
        }

        return new Box($width, $height);
    }
protected static function getBox ( BoxInterface $ sourceBox , $ width , $ height , $ keepAspectRatio = true ) { if ( $ width === null && $ height === null ) { throw new InvalidParamException ( 'Width and height cannot be null at same time.' ) ; } $ ratio = $ sourceBox -> getWidth ( ) / $ sourceBox -> getHeight ( ) ; if ( $ keepAspectRatio === false ) { if ( $ height === null ) { $ height = ceil ( $ width / $ ratio ) ; } elseif ( $ width === null ) { $ width = ceil ( $ height * $ ratio ) ; } } else { if ( $ height === null ) { $ height = ceil ( $ width / $ ratio ) ; } elseif ( $ width === null ) { $ width = ceil ( $ height * $ ratio ) ; } elseif ( $ width / $ height > $ ratio ) { $ width = $ height * $ ratio ; } else { $ height = $ width / $ ratio ; } } return new Box ( $ width , $ height ) ; }
Returns box for an image to be created.

If one of the dimensions is set to `null`, another one is calculated automatically based on width to height ratio
of original image box.

If both of the dimensions are set then new dimensions are calculated so that image keeps aspect ratio.

You can set $keepAspectRatio to false if you want to force fixed width and height.

@param BoxInterface $sourceBox original image box
@param int $width new image width
@param int $height new image height
@param bool $keepAspectRatio should we keep aspect ratio even if both with and height are set
@return BoxInterface new image box

@since 2.1.1
Returns box for an image to be created .