Fetch dimensions only once

This commit is contained in:
philipp lang 2022-02-15 17:46:29 +01:00
parent 0382ff970d
commit f419888ec6
1 changed files with 7 additions and 6 deletions

View File

@ -60,20 +60,21 @@ class ImageResizer
{ {
$return = collect([]); $return = collect([]);
$ratios = collect(); $ratios = collect();
$ratios->push($this->dimensions()); $dimensions = $this->dimensions();
$ratios->push($dimensions);
$ratios = $ratios->merge($this->sizes()); $ratios = $ratios->merge($this->sizes());
foreach (collect(Setting::get('breakpoints'))->push($this->dimensions()->get('width'))->unique() as $size) { foreach (collect(Setting::get('breakpoints'))->push($dimensions->get('width'))->unique() as $size) {
foreach ($ratios as $ratio) { foreach ($ratios as $ratio) {
$width = $size; $width = $size;
$height = $size * $ratio->get('height') / $ratio->get('width'); $height = $size * $ratio->get('height') / $ratio->get('width');
if ($height > $this->dimensions()->get('height')) { if ($height > $dimensions->get('height')) {
$width = $width * $this->dimensions()->get('height') / $height; $width = $width * $dimensions->get('height') / $height;
$height = $this->dimensions()->get('height'); $height = $dimensions->get('height');
} }
if (ceil($width) > $this->dimensions()->get('width') || ceil($height) > $this->dimensions()->get('height')) { if (ceil($width) > $dimensions->get('width') || ceil($height) > $dimensions->get('height')) {
continue; continue;
} }