public function resizeToBestFit($max_width, $max_height, $allow_enlarge = false)
{
if ($this->getSourceWidth() <= $max_width && $this->getSourceHeight() <= $max_height && $allow_enlarge === false) {
return $this;
}
$ratio = $this->getSourceHeight() / $this->getSourceWidth();
$width = $max_width;
$height = $width * $ratio;
if ($height > $max_height) {
$height = $max_height;
$width = $height / $ratio;
}
return $this->resize($width, $height, $allow_enlarge);
}