<?php

namespace IZON\Thumber\ImageResizer\Strategies;

use IZON\Thumber\Image\IImageSize;
use IZON\Thumber\Image\ImageSize;
use IZON\Thumber\ImageResizer\IImageResizerStrategy;

/**
 * CoverImageResizerStrategy computes cover size of image
 *
 * @author lukas
 */
class CoverImageResizerStrategy implements IImageResizerStrategy {

    public static function getSize(IImageSize $imageSize, IImageSize $containerSize, bool $upsize = false): IImageSize {
        $isContainerBigger = $imageSize->getWidth() < $containerSize->getWidth() || $imageSize->getHeight() < $containerSize->getHeight();
        if(!$upsize && $isContainerBigger) {
            return $imageSize;
        }

        if($containerSize->getAspectRatio() > $imageSize->getAspectRatio()) {
            $height = $containerSize->getHeight();
            $width = (int) floor($height / $imageSize->getAspectRatio());
        } else {
            $width = $containerSize->getWidth();
            $height = (int) floor($width * $imageSize->getAspectRatio());
        }
        return ImageSize::create($width, $height);
    }

}

