From af92fd3056b135a220adc1fbdfdeaae37783914c Mon Sep 17 00:00:00 2001 From: Lazizbek Ergashev Date: Thu, 3 Sep 2026 15:04:39 +0500 Subject: [PATCH 1/2] Fix Glide tag erroring when an asset cannot be resolved --- src/Tags/Glide.php | 11 ++++++++++- tests/Tags/GlideTest.php | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Tags/Glide.php b/src/Tags/Glide.php index 8b3bcd861d8..545d72dacbd 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((string) $item); + + if (! $asset) { + throw new AssetNotFoundException( + sprintf('Could not generate a manipulated image from asset [%s]', $item) + ); + } + + return $this->getGenerator()->generateByAsset($asset, $params); } /** diff --git a/tests/Tags/GlideTest.php b/tests/Tags/GlideTest.php index 920aa24a253..f930a3420ec 100644 --- a/tests/Tags/GlideTest.php +++ b/tests/Tags/GlideTest.php @@ -55,6 +55,26 @@ 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_an_asset_url_cannot_be_resolved() + { + $tag = <<<'EOT' +{{ glide src="http://external.com/bar (1).jpg" width="100" }}{{ url }}{{ /glide }} +EOT; + + $this->assertSame('', (string) Parse::template($tag, trusted: true)); + } + + #[Test] + public function it_doesnt_error_when_an_asset_id_cannot_be_resolved() + { + $tag = <<<'EOT' +{{ glide src="test::bar.jpg" width="100" }}{{ url }}{{ /glide }} +EOT; + + $this->assertSame('', (string) Parse::template($tag, trusted: true)); + } + #[Test] public function it_outputs_a_data_url() { From e88e19bd41cf42587259ef23c10ed0eeb1b4069b Mon Sep 17 00:00:00 2001 From: Lazizbek Ergashev Date: Thu, 3 Sep 2026 16:16:52 +0500 Subject: [PATCH 2/2] Keep the asset id when it cannot be resolved --- src/Tags/Glide.php | 4 ++-- tests/Tags/GlideTest.php | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Tags/Glide.php b/src/Tags/Glide.php index 545d72dacbd..bf59de963fb 100644 --- a/src/Tags/Glide.php +++ b/src/Tags/Glide.php @@ -178,7 +178,7 @@ private function generateImage($item) : $this->getGenerator()->generateByPath($item, $params); } - $asset = $item instanceof AssetContract ? $item : Asset::find((string) $item); + $asset = $item instanceof AssetContract ? $item : Asset::find($item); if (! $asset) { throw new AssetNotFoundException( @@ -299,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 f930a3420ec..ebea97bf08d 100644 --- a/tests/Tags/GlideTest.php +++ b/tests/Tags/GlideTest.php @@ -56,11 +56,9 @@ public function it_outputs_an_absolute_url_when_the_url_does_not_have_a_valid_ex } #[Test] - public function it_doesnt_error_when_an_asset_url_cannot_be_resolved() + public function it_doesnt_error_when_a_url_cannot_be_resolved_to_an_asset() { - $tag = <<<'EOT' -{{ glide src="http://external.com/bar (1).jpg" width="100" }}{{ url }}{{ /glide }} -EOT; + $tag = '{{ glide src="http://external.com/bar (1).jpg" width="100" }}{{ url }}{{ /glide }}'; $this->assertSame('', (string) Parse::template($tag, trusted: true)); } @@ -68,9 +66,16 @@ public function it_doesnt_error_when_an_asset_url_cannot_be_resolved() #[Test] public function it_doesnt_error_when_an_asset_id_cannot_be_resolved() { - $tag = <<<'EOT' -{{ glide src="test::bar.jpg" width="100" }}{{ url }}{{ /glide }} -EOT; + $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)); }