What's the math behind CSS's background-size:cover

前端 未结 4 1341
长发绾君心
长发绾君心 2021-01-30 13:57

I\'m creating an \"image generator\" where users can upload an image and add text and/or draw on it. The outputted image is a fixed size (698x450).

On the client side,

4条回答
  •  一整个雨季
    2021-01-30 14:41

    Thanks to mdi for pointing me in the right direction, but that didn't seem quite right. This is the solution that worked for me:

        $imgRatio = $imageHeight / $imageWidth;
        $canvasRatio = $canvasHeight / $canvasWidth;
    
        if ($canvasRatio > $imgRatio) {
            $finalHeight = $canvasHeight;
            $scale = $finalHeight / $imageHeight;
            $finalWidth = round($imageWidth * $scale , 0);
        } else {
            $finalWidth = $canvasWidth;
            $scale = $finalWidth / $imageWidth;
            $finalHeight = round($imageHeight * $scale , 0);
        }
    

提交回复
热议问题