Fixed: Dont create tag if image is 1px high
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Philipp Lang 2022-04-04 09:58:30 +02:00
parent d2253a3417
commit d94f21c852
1 changed files with 14 additions and 15 deletions

View File

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