Skip to content

Commit 893c05c

Browse files
committed
fix(Images): handle unsupported or missing extension in ImageMagickHandler::save()
1 parent 3ad5144 commit 893c05c

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Release Date: Unreleased
1414

1515
## Bugs Fixed
1616

17+
- **Images:** Fixed a bug in `ImageMagickHandler::save()` where saving without an extension or with an unsupported extension threw an `ImagickException` instead of falling back to the image's format.
1718
- **Security:** Fixed a bug where `Security::__construct()` threw a `TypeError` when the CSRF cookie was an array, causing an HTTP 500 response for affected requests.
1819
- **Worker Mode:** Fixed a bug where every request that ended with an uncaught exception, including a `404 Page Not Found` without a 404 override, called `exit()` in the exception handler. This ended the worker script, so FrankenPHP booted a new one, and the worker loop skipped its cleanup after the request. The exception handler now returns to the worker loop.
1920

‎system/Images/Handlers/ImageMagickHandler.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,16 @@ public function save(?string $target = null, int $quality = 90): bool
321321
$this->resource->setImageCompressionQuality($quality);
322322

323323
$extension = pathinfo($target, PATHINFO_EXTENSION);
324-
$this->resource->setImageFormat($extension);
325324

326325
try {
326+
if ($extension !== '' && in_array(strtoupper($extension), Imagick::queryFormats(), true)) {
327+
$this->resource->setImageFormat($extension);
328+
} elseif ($this->image()->imageType !== 0 && ($format = image_type_to_extension($this->image()->imageType, false))) {
329+
if (in_array(strtoupper($format), Imagick::queryFormats(), true)) {
330+
$this->resource->setImageFormat($format);
331+
}
332+
}
333+
327334
$result = $this->resource->writeImage($target);
328335

329336
chmod($target, $this->filePermissions);

‎tests/system/Images/ImageMagickHandlerTest.php‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,48 @@ public function testImageConvert(): void
408408
$this->assertSame(IMAGETYPE_PNG, exif_imagetype($this->root . 'ci-logo.png'));
409409
}
410410

411+
public function testSaveWithoutExtension(): void
412+
{
413+
$this->handler->withFile($this->origin . 'ci-logo.jpeg');
414+
$this->handler->resize(100, 100);
415+
$target = $this->root . 'ci-logo-no-ext';
416+
$this->handler->save($target);
417+
418+
$this->assertFileExists($target);
419+
$this->assertSame(IMAGETYPE_JPEG, exif_imagetype($target));
420+
}
421+
422+
public function testSaveWithUnsupportedExtension(): void
423+
{
424+
$this->handler->withFile($this->origin . 'ci-logo.jpeg');
425+
$this->handler->resize(100, 100);
426+
$target = $this->root . 'ci-logo.tmp';
427+
$this->handler->save($target);
428+
429+
$this->assertFileExists($target);
430+
$this->assertSame(IMAGETYPE_JPEG, exif_imagetype($target));
431+
}
432+
433+
public function testSaveWithoutExtensionAfterConvert(): void
434+
{
435+
$this->handler->withFile($this->origin . 'ci-logo.jpeg');
436+
$this->handler->convert(IMAGETYPE_PNG);
437+
$target = $this->root . 'ci-logo-converted-no-ext';
438+
$this->handler->save($target);
439+
440+
$this->assertFileExists($target);
441+
$this->assertSame(IMAGETYPE_PNG, exif_imagetype($target));
442+
}
443+
444+
public function testSaveFailedThrowsImageException(): void
445+
{
446+
$this->expectException(ImageException::class);
447+
$this->expectExceptionMessage('Unable to save the image. Please make sure the image and file directory are writable.');
448+
449+
$this->handler->withFile($this->origin . 'ci-logo.jpeg');
450+
$this->handler->save('/non_existent_directory_xyz/ci-logo.jpg');
451+
}
452+
411453
public function testImageReorientLandscape(): void
412454
{
413455
for ($i = 0; $i <= 8; $i++) {

0 commit comments

Comments
 (0)