{"record":{"id":"018455575c6ebc09","repo":"jackwener/OpenCLI","slug":"failed-to-read-image-dimensions-for-filepath","errorCode":null,"errorMessage":"Failed to read image dimensions for ${filePath}","messagePattern":"Failed to read image dimensions for (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/instagram/_shared/private-publish.js","lineNumber":269,"sourceCode":"    }\n    if (chunkType === 'VP8L' && bytes.length >= 25) {\n        const bits = bytes.readUInt32LE(21);\n        return {\n            width: (bits & 0x3fff) + 1,\n            height: ((bits >> 14) & 0x3fff) + 1,\n        };\n    }\n    return null;\n}\nfunction readImageDimensions(filePath, bytes) {\n    const ext = path.extname(filePath).toLowerCase();\n    const dimensions = ext === '.png'\n        ? readPngDimensions(bytes)\n        : ext === '.webp'\n            ? readWebpDimensions(bytes)\n            : readJpegDimensions(bytes);\n    if (!dimensions) {\n        throw new CommandExecutionError(`Failed to read image dimensions for ${filePath}`);\n    }\n    return dimensions;\n}\nexport function readImageAsset(filePath) {\n    const bytes = fs.readFileSync(filePath);\n    const { width, height } = readImageDimensions(filePath, bytes);\n    return {\n        filePath,\n        fileName: path.basename(filePath),\n        mimeType: inferMimeType(filePath),\n        width,\n        height,\n        byteLength: bytes.length,\n        bytes,\n    };\n}\nexport function isInstagramFeedAspectRatioAllowed(width, height) {\n    const ratio = width / Math.max(height, 1);","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/instagram/_shared/private-publish.js#L251-L287","documentation":"readImageDimensions parses PNG/WebP/JPEG headers to obtain width/height required for the private upload; when none of the format parsers recognizes the bytes this error is thrown, since Instagram needs declared media dimensions.","triggerScenarios":"Calling prepareImageAssetForPrivateUpload / readImageAsset with a file whose extension-branch parser (png/webp/jpeg) fails: corrupt file, unsupported extension treated as jpeg, truncated download, or zero-byte file.","commonSituations":"A .jpg file that is actually another format, an SVG or GIF passed where a raster image is expected, partially downloaded image, file with wrong extension.","solutions":["Verify the file is a valid, non-corrupt PNG/WebP/JPEG (open it locally or run `file image.png`)","Rename the file so its extension matches the actual format","Re-export or re-download the image","Convert unsupported formats to JPEG/PNG before upload"],"exampleFix":"// before\nawait upload({ media: 'logo.svg' });\n// after\n// $ magick logo.svg logo.png\nawait upload({ media: 'logo.png' });","handlingStrategy":"validation","validationCode":"const bytes = fs.readFileSync(filePath);\nif (bytes.length < 16) throw new Error(`${filePath} is not a readable image`);\nconst sig = bytes.subarray(0, 4).toString('hex');\nconst ok = sig === '89504e47' || bytes.subarray(0,4).toString() === 'RIFF' || bytes[0] === 0xFF && bytes[1] === 0xD8;\nif (!ok) throw new Error(`${filePath} is not PNG/WebP/JPEG`);","typeGuard":"function isKnownImageExt(p) {\n  return ['.png', '.jpg', '.jpeg', '.webp'].includes(path.extname(p).toLowerCase());\n}","tryCatchPattern":"try {\n  await publish(cfg);\n} catch (e) {\n  if (/Failed to read image dimensions/.test(e.message)) {\n    // re-encode the asset and retry\n    execSync(`magick ${cfg.media} ${cfg.media.replace(/\\.\\w+$/, '.png')}`);\n  } else throw e;\n}","preventionTips":["Validate image files are real PNG/WebP/JPEG before upload","Ensure file extensions match actual encodings","Check files for truncation/corruption after downloads","Convert SVG/GIF/HEIC assets to JPEG or PNG first"],"tags":["image","validation","file-format"],"backgroundTag":"invalid-image-file","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}