Skip to content

fix(Images): handle unsupported or missing extension in ImageMagickHandler::save() - #10746

Open
gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:fix/imagemagick-save-unsupported-extension
Open

gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:fix/imagemagick-save-unsupported-extension

Conversation

@gr8man

@gr8man gr8man commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Description
ImageMagickHandler::save() unconditionally called $this->resource->setImageFormat($extension) outside of error handling, throwing an uncaught ImagickException when saving without an extension or with an unsupported extension (e.g., .tmp). This change checks against Imagick::queryFormats(), falls back to the current image type format, and wraps the operation in standard image save failure handling.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@carson-codeigniter4 carson-codeigniter4 Bot added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 8, 2026
try {
if ($extension !== '' && in_array(strtoupper($extension), Imagick::queryFormats(), true)) {
$this->resource->setImageFormat($extension);
} elseif ($this->image()->imageType !== 0 && ($format = image_type_to_extension($this->image()->imageType, false))) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls use strict boolean check for $format

$this->resource->setImageFormat($extension);

try {
if ($extension !== '' && in_array(strtoupper($extension), Imagick::queryFormats(), true)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imagick::queryFormats() can be assigned to a local variable then reused.

@gr8man
gr8man force-pushed the fix/imagemagick-save-unsupported-extension branch from 893c05c to 981e676 Compare November 9, 2026 18:52

@michalsn michalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also update the note under Converting Images? With this change, the ImageMagick handler uses $imageType as a fallback when the target has no extension or an unsupported extension:

When using ImageMagick, a supported target filename extension determines the output format and takes precedence over $imageType. If the target has no extension or an unsupported extension, the handler uses $imageType instead.

Please also update the existing note about save() under Image Quality to mention this fallback, while preserving the guidance about format prefixes and input validation.

@gr8man
gr8man force-pushed the fix/imagemagick-save-unsupported-extension branch from 981e676 to 7f9cb5f Compare November 12, 2026 10:49

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Verified issues on the current code behavior or pull requests that will fix them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants