size::fit PHP Method

fit() static public method

Fits width and height into a defined box and keeps the ratio
static public fit ( integer $width, integer $height, integer $box, boolean $force = false ) : array
$width integer
$height integer
$box integer
$force boolean If width and height are smaller than the box this will force upscaling
return array An array with a key and value for width and height
    static function fit($width, $height, $box, $force = false)
    {
        if ($width == 0 || $height == 0) {
            return array('width' => $box, 'height' => $box);
        }
        $ratio = self::ratio($width, $height);
        if ($width > $height) {
            if ($width > $box || $force == true) {
                $width = $box;
            }
            $height = floor($width / $ratio);
        } elseif ($height > $width) {
            if ($height > $box || $force == true) {
                $height = $box;
            }
            $width = floor($height * $ratio);
        } elseif ($width > $box) {
            $width = $box;
            $height = $box;
        }
        $output = array();
        $output['width'] = $width;
        $output['height'] = $height;
        return $output;
    }

Usage Example

Beispiel #1
0
 function fit($box, $force = false)
 {
     $size = size::fit($this->width(), $this->height(), $box, $force);
     $this->info->width = $size['width'];
     $this->info->height = $size['height'];
     return $this;
 }