JBZoo\Image\Image::bestFit PHP Method

bestFit() public method

Best fit (proportionally resize to fit in specified width/height) Shrink the image proportionally to fit inside a $width x $height box
public bestFit ( integer $maxWidth, integer $maxHeight )
$maxWidth integer
$maxHeight integer
    public function bestFit($maxWidth, $maxHeight)
    {
        // If it already fits, there's nothing to do
        if ($this->_width <= $maxWidth && $this->_height <= $maxHeight) {
            return $this;
        }
        // Determine aspect ratio
        $aspectRatio = $this->_height / $this->_width;
        $width = $this->_width;
        $height = $this->_height;
        // Make width fit into new dimensions
        if ($this->_width > $maxWidth) {
            $width = $maxWidth;
            $height = $width * $aspectRatio;
        }
        // Make height fit into new dimensions
        if ($height > $maxHeight) {
            $height = $maxHeight;
            $width = $height / $aspectRatio;
        }
        return $this->resize($width, $height);
    }

Usage Example

Ejemplo n.º 1
0
 public function testIssue8()
 {
     $excepted = Helper::getExpected(__FUNCTION__ . '.png');
     $actual = Helper::getActual(__FUNCTION__ . '.png');
     $base = Helper::getOrig('issue-8/original.png');
     $img = new Image($base);
     if ($img->getHeight() != $img->getWidth()) {
         if ($img->getWidth() < 175) {
             $img->fitToWidth($img->getWidth());
         } else {
             $img->fitToWidth(175);
         }
     } else {
         $img->bestFit(175, 175);
     }
     $img->saveAs($actual);
     Helper::isFileEq($actual, $excepted);
 }