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