Skip to content

Commit c6d23c8

Browse files
authored
[5.x] Throw UnableToReadFile for invalid images in ImageGenerator (#14043)
1 parent 01ca084 commit c6d23c8

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/Imaging/ImageGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ private function validateImage()
306306
}
307307

308308
if (! ImageValidator::isValidImage($extension, $mime)) {
309-
throw new \Exception("Image [{$path}] does not actually appear to be a valid image.");
309+
throw UnableToReadFile::fromLocation($path, "Image [{$path}] does not actually appear to be a valid image.");
310310
}
311311
}
312312

tests/Imaging/ImageGeneratorTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Support\Facades\Event;
1111
use Illuminate\Support\Facades\Storage;
1212
use League\Flysystem\Local\LocalFilesystemAdapter;
13+
use League\Flysystem\UnableToReadFile;
1314
use League\Glide\Manipulators\Watermark;
1415
use League\Glide\Server;
1516
use PHPUnit\Framework\Attributes\DataProvider;
@@ -85,6 +86,22 @@ public function it_generates_an_image_by_asset()
8586
Event::assertDispatchedTimes(GlideImageGenerated::class, 1);
8687
}
8788

89+
#[Test]
90+
public function it_throws_unable_to_read_file_when_asset_is_not_a_valid_image()
91+
{
92+
Storage::fake('test');
93+
$file = UploadedFile::fake()->create('foo/hoff.jpg', 100);
94+
Storage::disk('test')->putFileAs('foo', $file, 'hoff.jpg');
95+
$container = tap(AssetContainer::make('test_container')->disk('test'))->save();
96+
$asset = tap($container->makeAsset('foo/hoff.jpg'))->save();
97+
98+
ImageValidator::shouldReceive('isValidImage')->andReturnFalse();
99+
100+
$this->expectException(UnableToReadFile::class);
101+
102+
$this->makeGenerator()->generateByAsset($asset, ['w' => 100]);
103+
}
104+
88105
#[Test]
89106
public function it_generates_cache_manifest_for_multiple_asset_manipulations()
90107
{

0 commit comments

Comments
 (0)