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);
}
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); }