diff --git a/classes/TagGenerator.php b/classes/TagGenerator.php index ea9dfdb..67fe1da 100644 --- a/classes/TagGenerator.php +++ b/classes/TagGenerator.php @@ -4,17 +4,14 @@ namespace Aweos\Resizer\Classes; use Aweos\Resizer\Compressors\Compressor; use Aweos\Resizer\Compressors\CompressorNotFoundException; -use Aweos\Resizer\Compressors\Factory as CompressorFactory; use Aweos\Resizer\Exceptions\ResizerException; use Aweos\Resizer\Lib\MediaPath; use Aweos\Resizer\Models\Setting; -use Cache; use Exception; use Illuminate\Support\Collection; -use Storage; - -class TagGenerator { +class TagGenerator +{ private Compressor $compressor; public MediaPath $path; private array $defaultOptions = [ @@ -38,13 +35,13 @@ class TagGenerator { $files = $this->possibleFiles($ratio); - if ($files === null) { + if (null === $files) { return $this->fallback(); } $sizes = $this->parseSizes($files, $sizes); - $srcset = $files->map(function($file) { + $srcset = $files->map(function ($file) { return "{$file->get('url')} {$file->get('width')}w"; }); @@ -56,7 +53,7 @@ class TagGenerator { $options['lazy'] ? 'data-src' : 'src' => $files->last()->get('url'), 'class' => data_get($options, 'class'), ])->filter(fn ($value) => (bool) $value)); - + $this->compressor->end(); return $html; @@ -80,14 +77,17 @@ class TagGenerator { $filename = $this->path->filename(); $basePath = $this->path->versionsDirPath(); [$originalWidth, $originalHeight] = $this->compressor->originalSize(); - $aspectRatio = $ratio === 'original' + $aspectRatio = 'original' === $ratio ? $originalWidth / $originalHeight : $this->size($ratio)[0] / $this->size($ratio)[1]; $result = collect([]); foreach ($this->path->versions() as $version) { - if ($version->get('width') / ($version->get('height')+1) > $aspectRatio || $version->get('width') / ($version->get('height')-1) < $aspectRatio) { + if (1 === $version->get('height')) { + continue; + } + if ($version->get('width') / ($version->get('height') + 1) > $aspectRatio || $version->get('width') / ($version->get('height') - 1) < $aspectRatio) { continue; } @@ -106,7 +106,7 @@ class TagGenerator { private function htmlAttributes($attr): string { - return $attr->map(function($value, $key) { + return $attr->map(function ($value, $key) { return "{$key}=\"{$value}\""; })->implode(' '); } @@ -114,18 +114,18 @@ class TagGenerator { private function parseSizes(Collection $files, ?string $sizes = null): Collection { if (is_null($sizes)) { - return $files->map(function($file) { + return $files->map(function ($file) { return "(max-width: {$file->get('width')}px) {$file->get('width')}px"; }); } $sizes = collect(explode('|', $sizes)); $minSize = $sizes->shift(); - $sizes = $sizes->sortByDesc(function($size) { + $sizes = $sizes->sortByDesc(function ($size) { return explode(':', $size)[0]; }); - return $sizes->map(function($size) { + return $sizes->map(function ($size) { $components = explode(':', $size); return "(min-width: {$components[0]}px) {$components[1]}"; @@ -148,5 +148,4 @@ class TagGenerator { return explode('x', $size['aspect_ratio']); } - }