{"record":{"id":"10d4c75c3c7a3232","repo":"gchq/CyberChef","slug":"error-opening-image-file-err","errorCode":null,"errorMessage":"Error opening image file. (${err})","messagePattern":"Error opening image file\\. \\((.+?)\\)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/ConvertImageFormat.mjs","lineNumber":91,"sourceCode":"        const pngFilterMap = {\n            Auto: PNGFilterType.AUTO,\n            None: PNGFilterType.NONE,\n            Sub: PNGFilterType.SUB,\n            Up: PNGFilterType.UP,\n            Average: PNGFilterType.AVERAGE,\n            Paeth: PNGFilterType.PATH,\n        };\n\n        const mime = formatMap[format];\n\n        if (!isImage(input)) {\n            throw new OperationError(\"Invalid file format.\");\n        }\n        let image;\n        try {\n            image = await Jimp.read(input);\n        } catch (err) {\n            throw new OperationError(`Error opening image file. (${err})`);\n        }\n        try {\n            let buffer;\n            switch (mime) {\n                case JimpMime.jpeg:\n                    buffer = await image.getBuffer(mime, {\n                        quality: jpegQuality,\n                    });\n                    break;\n                case JimpMime.png:\n                    buffer = await image.getBuffer(mime, {\n                        filterType: pngFilterMap[pngFilterType],\n                        deflateLevel: pngDeflateLevel,\n                    });\n                    break;\n                default:\n                    buffer = await image.getBuffer(mime);\n                    break;","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/ConvertImageFormat.mjs#L73-L109","documentation":"After isImage() accepts the input, Convert Image Format calls Jimp.read(input) to decode. If decoding fails (corrupt/truncated payload, structurally invalid image, or a format this Jimp version can't decode), the error is wrapped as 'Error opening image file.' The type sniff passed but the real decode failed.","triggerScenarios":"Truncated file (valid header, incomplete body); corrupt image structure; sub-format/color space Jimp can't handle (CMYK JPEG, 16-bit PNG); Jimp version lacking the needed codec.","commonSituations":"Partial download; exotic re-exported variant; dependency upgrade changing supported formats.","solutions":["Re-save the source as a standard PNG or baseline JPEG.","Confirm the file is complete and uncorrupted.","Check the bundled Jimp version's supported formats.","Transcode exotic formats to PNG externally first."],"exampleFix":"// before: corrupt/truncated input that isImage accepts but Jimp rejects\n// after: supply a complete, valid image file","handlingStrategy":"try-catch","validationCode":"import { isImage } from \"src/core/lib/FileType.mjs\";\n// Reduces (not eliminates) the chance before Jimp.read.\nconst bytes = input instanceof ArrayBuffer ? new Uint8Array(input) : input;\nif (!isImage(bytes)) {\n  throw new Error(\"Not a recognized image — Jimp.read would likely fail.\");\n}","typeGuard":"import { isImage } from \"src/core/lib/FileType.mjs\";\nfunction isLikelyDecodableImage(buf) {\n  const bytes = buf instanceof ArrayBuffer ? new Uint8Array(buf) : buf;\n  return isImage(bytes) !== false;\n}","tryCatchPattern":"try {\n  result = await convertImageFormat.run(input, args);\n} catch (err) {\n  if (/Error opening image file/.test(err.message)) {\n    // isImage passed but Jimp decode failed: corrupt/truncated or unsupported sub-format.\n    // Re-export source as PNG/baseline JPEG, then retry.\n  }\n  throw err;\n}","preventionTips":["isImage passing does not guarantee Jimp can decode — verify integrity separately.","Prefer standard PNG/baseline JPEG sources.","Re-test supported formats after any Jimp upgrade."],"tags":["cyberchef","image","jimp","decode","operation-error"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}