From 7d8bdd61d1c972ecdde22fd3106865650044df77 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Mon, 12 May 2014 15:51:37 +0200 Subject: [PATCH] more efficient fitting --- src/Intervention/Image/Size.php | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Intervention/Image/Size.php b/src/Intervention/Image/Size.php index fe5e3219..807cc770 100644 --- a/src/Intervention/Image/Size.php +++ b/src/Intervention/Image/Size.php @@ -109,23 +109,27 @@ class Size public function fit(Size $size) { - $auto_width = clone $size; - $auto_height = clone $size; - - // create size with auto width - $auto_width->resize(null, $this->height, function ($constraint) { - $constraint->aspectRatio(); - }); - // create size with auto height + $auto_height = clone $size; + $auto_height->resize($this->width, null, function ($constraint) { $constraint->aspectRatio(); }); // decide which version to use if ($auto_height->fitsInto($this)) { + $size = $auto_height; + } else { + + // create size with auto width + $auto_width = clone $size; + + $auto_width->resize(null, $this->height, function ($constraint) { + $constraint->aspectRatio(); + }); + $size = $auto_width; }