From 341349848d4b0d28d473706f6035d230dbe29c9a Mon Sep 17 00:00:00 2001 From: Philipp Lang Date: Mon, 6 Sep 2021 13:03:38 +0200 Subject: [PATCH] Fixed: Dont generate versions for SVG --- classes/TagGenerator.php | 2 +- tests/ImageTagTest.php | 21 +++++++++++++++++++++ tests/ResizerTest.php | 14 ++++++++++++++ tests/stub/close.svg | 1 + 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/stub/close.svg diff --git a/classes/TagGenerator.php b/classes/TagGenerator.php index 77afd96..6a6f206 100644 --- a/classes/TagGenerator.php +++ b/classes/TagGenerator.php @@ -78,7 +78,7 @@ class TagGenerator { $files = $this->possibleFiles($ratio); if ($files->isEmpty()) { - return 'src="/storage/app/media/'.$path.'"'; + return 'src="'.$this->media->findFiles($path)[0]->publicUrl.'"'; } $sizes = $files->map(function($file) { diff --git a/tests/ImageTagTest.php b/tests/ImageTagTest.php index 4d16338..d88149b 100644 --- a/tests/ImageTagTest.php +++ b/tests/ImageTagTest.php @@ -85,4 +85,25 @@ class ImageTagTest extends TestCase )); } + public function testPickOriginalImageWhenSvgIsGiven() + { + Setting::set('folders', ['pages']); + Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1200x280']]); + Setting::set('breakpoints', [640]); + $this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg')); + + $this->assertFalse(str_contains( + app(TagGenerator::class)->generate('pages/test.svg', 'size'), + 'srcset', + )); + $this->assertFalse(str_contains( + app(TagGenerator::class)->generate('pages/test.svg', 'size'), + 'sizes', + )); + $this->assertTrue(str_contains( + app(TagGenerator::class)->generate('pages/test.svg', 'size'), + 'src="'.$this->media->findFiles('/pages/test.svg')[0]->publicUrl.'"', + )); + } + } diff --git a/tests/ResizerTest.php b/tests/ResizerTest.php index 11793f4..4b4430c 100644 --- a/tests/ResizerTest.php +++ b/tests/ResizerTest.php @@ -155,4 +155,18 @@ class ResizerTest extends TestCase $this->assertHasFile('pages/neu/test-100x80.jpg'); } + public function testDontGenerateSvgImage(): void + { + Setting::set('folders', [['folder' => '/pages']]); + Setting::set('sizes', []); + Setting::set('breakpoints', ['100']); + + $file = UploadedFile::fake()->image('test.jpg', 500, 400); + $media = MediaLibrary::instance(); + $media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg')); + Event::fire('media.file.upload', [null, '/pages/test.svg', null]); + + $this->assertFileCount(0, 'pages/neu'); + } + } diff --git a/tests/stub/close.svg b/tests/stub/close.svg new file mode 100644 index 0000000..42c342a --- /dev/null +++ b/tests/stub/close.svg @@ -0,0 +1 @@ +