diff --git a/src/Tags/Glide.php b/src/Tags/Glide.php index 8b3bcd861d8..bf59de963fb 100644 --- a/src/Tags/Glide.php +++ b/src/Tags/Glide.php @@ -15,6 +15,7 @@ use Statamic\Facades\Image; use Statamic\Facades\Path; use Statamic\Facades\URL; +use Statamic\Imaging\AssetNotFoundException; use Statamic\Imaging\ImageGenerator; use Statamic\Support\Str; @@ -177,7 +178,15 @@ private function generateImage($item) : $this->getGenerator()->generateByPath($item, $params); } - return $this->getGenerator()->generateByAsset(Asset::find($item), $params); + $asset = $item instanceof AssetContract ? $item : Asset::find($item); + + if (! $asset) { + throw new AssetNotFoundException( + sprintf('Could not generate a manipulated image from asset [%s]', $item) + ); + } + + return $this->getGenerator()->generateByAsset($asset, $params); } /** @@ -290,7 +299,7 @@ private function normalizeItem($item) // Double colons indicate an asset ID. if (Str::contains($item, '::')) { - return Asset::find($item); + return Asset::find($item) ?? $item; } // In a subfolder installation, the subfolder will likely be passed in diff --git a/tests/Tags/GlideTest.php b/tests/Tags/GlideTest.php index 920aa24a253..ebea97bf08d 100644 --- a/tests/Tags/GlideTest.php +++ b/tests/Tags/GlideTest.php @@ -55,6 +55,31 @@ public function it_outputs_an_absolute_url_when_the_url_does_not_have_a_valid_ex $this->assertSame('https://statamic.com/foo', $parse); } + #[Test] + public function it_doesnt_error_when_a_url_cannot_be_resolved_to_an_asset() + { + $tag = '{{ glide src="http://external.com/bar (1).jpg" width="100" }}{{ url }}{{ /glide }}'; + + $this->assertSame('', (string) Parse::template($tag, trusted: true)); + } + + #[Test] + public function it_doesnt_error_when_an_asset_id_cannot_be_resolved() + { + $tag = '{{ glide src="test::bar.jpg" width="100" fit="crop_focal" }}{{ url }}{{ /glide }}'; + + $this->assertSame('', (string) Parse::template($tag, trusted: true)); + } + + #[Test] + #[DefineEnvironment('relativeRouteUrl')] + public function it_doesnt_error_when_an_asset_id_cannot_be_resolved_and_images_are_served_directly() + { + $tag = '{{ glide src="test::bar.jpg" width="100" }}{{ url }}{{ /glide }}'; + + $this->assertSame('', (string) Parse::template($tag, trusted: true)); + } + #[Test] public function it_outputs_a_data_url() {