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\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']);
}
}