/** * Crops the given image using the given image request options. * * @param Image $image The image to resize * @param Request $request The image request * @return Image The image thumbnail */ private function crop(Image $image, Request $request) { if ($request->getDimensions() === Request::ORIGINAL) { return $image; } list($width, $height) = $this->processDimensions($request->getDimensions()); $resizeWidth = $width; $resizeHeight = $height; $originalWidth = $request->getMeta()->getWidth(); $originalHeight = $request->getMeta()->getHeight(); $originalLandscape = $originalWidth > $originalHeight; $cropLandscape = $width > $height; $equals = $width === $height; if ($originalLandscape) { if ($cropLandscape) { $coefficient = $originalHeight / $height; $scaledWidth = round($originalWidth / $coefficient); $left = round(($scaledWidth - $width) / 2); $top = 0; if ($scaledWidth < $width) { $coefficient = $originalWidth / $width; $scaledHeight = round($originalHeight / $coefficient); $left = 0; $top = round(($scaledHeight - $height) / 2); } } else { $coefficient = $originalHeight / $height; $scaledWidth = round($originalWidth / $coefficient); $left = round(($scaledWidth - $width) / 2); $top = 0; } } else { if ($cropLandscape || $equals) { $coefficient = $originalWidth / $width; $scaledHeight = round($originalHeight / $coefficient); $left = 0; $top = round(($scaledHeight - $height) / 2); } else { $coefficient = $originalHeight / $height; $scaledWidth = round($originalWidth / $coefficient); $left = round(($scaledWidth - $width) / 2); $top = 0; } } $image->resize($resizeWidth, $resizeHeight, Image::FILL); $image->crop($left, $top, $width, $height); return $image; }