Add sizes attribute
This commit is contained in:
parent
ca9b03f36a
commit
ea55bfd427
|
@ -103,7 +103,7 @@ class Plugin extends PluginBase
|
||||||
public function registerMarkupTags() {
|
public function registerMarkupTags() {
|
||||||
return [
|
return [
|
||||||
'filters' => [
|
'filters' => [
|
||||||
'resize' => fn ($media, $size = 'original') => app(CacheManager::class)->get($media, $size)
|
'resize' => fn ($media, $size = 'original', $sizes = null) => app(CacheManager::class)->get($media, $size, $sizes)
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,10 +16,10 @@ class CacheManager
|
||||||
$this->fileObserver = $fileObserver;
|
$this->fileObserver = $fileObserver;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get(string $path, string $size): string
|
public function get(string $path, string $size, ?string $sizes): string
|
||||||
{
|
{
|
||||||
return Cache::tags($this->getTag($path))
|
return Cache::tags($this->getTag($path, $sizes))
|
||||||
->rememberForever($this->cacheKey($path, $size), fn () => $this->tag->generate($path, $size));
|
->rememberForever($this->cacheKey($path, $size), fn () => $this->tag->generate($path, $size, $sizes));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete(string $path): void
|
public function delete(string $path): void
|
||||||
|
|
|
@ -68,7 +68,7 @@ class TagGenerator {
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function generate(string $path, ?string $ratio = 'original'): string
|
public function generate(string $path, ?string $ratio = 'original', $sizes = null): string
|
||||||
{
|
{
|
||||||
$this->path = $path;
|
$this->path = $path;
|
||||||
$files = $this->possibleFiles($ratio);
|
$files = $this->possibleFiles($ratio);
|
||||||
|
@ -77,9 +77,7 @@ class TagGenerator {
|
||||||
return 'src="'.$this->media->findFiles($path)[0]->publicUrl.'"';
|
return 'src="'.$this->media->findFiles($path)[0]->publicUrl.'"';
|
||||||
}
|
}
|
||||||
|
|
||||||
$sizes = $files->map(function($file) {
|
$sizes = $this->parseSizes($files, $sizes);
|
||||||
return "(max-width: {$file->get('width')}px) {$file->get('width')}px";
|
|
||||||
});
|
|
||||||
|
|
||||||
$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";
|
||||||
|
@ -99,4 +97,22 @@ class TagGenerator {
|
||||||
return "{$key}=\"{$value}\"";
|
return "{$key}=\"{$value}\"";
|
||||||
})->implode(' ');
|
})->implode(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function parseSizes(Collection $files, ?string $sizes = null): Collection
|
||||||
|
{
|
||||||
|
if (is_null($sizes)) {
|
||||||
|
return $files->map(function($file) {
|
||||||
|
return "(max-width: {$file->get('width')}px) {$file->get('width')}px";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
$sizes = collect(explode('|', $sizes));
|
||||||
|
$minSize = $sizes->shift();
|
||||||
|
|
||||||
|
return $sizes->map(function($size) {
|
||||||
|
$components = explode(':', $size);
|
||||||
|
|
||||||
|
return "(min-width: {$components[0]}px) {$components[1]}";
|
||||||
|
})->push($minSize);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,4 +125,18 @@ class ImageTagTest extends TestCase
|
||||||
$this->assertEquals($output, Cache::tags(['resizer', 'resizer.pages/test.svg'])->get('resizer.original.pages/test.svg'));
|
$this->assertEquals($output, Cache::tags(['resizer', 'resizer.pages/test.svg'])->get('resizer.original.pages/test.svg'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGenerateSizesAttribute()
|
||||||
|
{
|
||||||
|
Setting::set('folders', ['pages']);
|
||||||
|
Setting::set('sizes', []);
|
||||||
|
Setting::set('breakpoints', [100, 200]);
|
||||||
|
$this->media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get());
|
||||||
|
UploadedFile::fake()->image('test-100x100.jpg', 100, 100)->storeAs('uploads/public/c/pages', 'test-100x100.jpg');
|
||||||
|
UploadedFile::fake()->image('test-200x200.jpg', 200, 200)->storeAs('uploads/public/c/pages', 'test-200x200.jpg');
|
||||||
|
|
||||||
|
$output = app(Twig::class)->parse('{{ "/pages/test.jpg" | resize("original", "1rem|200:10px|500:20px") }}');
|
||||||
|
|
||||||
|
$this->assertStringContainsString('sizes="(min-width: 200px) 10px, (min-width: 500px) 20px, 1rem"', $output);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue