{"record":{"id":"bfce4842042ca0d0","repo":"PHPOffice/PhpSpreadsheet","slug":"bitmap-isn-t-a-24bit-true-color-bitmap-n","errorCode":null,"errorMessage":"$bitmap isn't a 24bit true color bitmap.\\n","messagePattern":"\\$bitmap isn't a 24bit true color bitmap\\.\\\\n","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Writer\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Writer/Xls/Worksheet.php","lineNumber":2517,"sourceCode":"        // Read and remove the bitmap width and height. Verify the sizes.\n        $width_and_height = unpack('V2', substr($data, 0, 8)) ?: [];\n        /** @var int */\n        $width = $width_and_height[1];\n        /** @var int */\n        $height = $width_and_height[2];\n        $data = substr($data, 8);\n        if ($width > 0xFFFF) {\n            throw new WriterException(\"$bitmap: largest image width supported is 65k.\\n\");\n        }\n        if ($height > 0xFFFF) {\n            throw new WriterException(\"$bitmap: largest image height supported is 65k.\\n\");\n        }\n\n        // Read and remove the bitmap planes and bpp data. Verify them.\n        $planes_and_bitcount = unpack('v2', substr($data, 0, 4));\n        $data = substr($data, 4);\n        if ($planes_and_bitcount === false || $planes_and_bitcount[2] != 24) { // Bitcount\n            throw new WriterException(\"$bitmap isn't a 24bit true color bitmap.\\n\");\n        }\n        if ($planes_and_bitcount[1] != 1) {\n            throw new WriterException(\"$bitmap: only 1 plane supported in bitmap image.\\n\");\n        }\n\n        // Read and remove the bitmap compression. Verify compression.\n        $compression = unpack('Vcomp', substr($data, 0, 4));\n        $data = substr($data, 4);\n\n        if ($compression === false || $compression['comp'] != 0) {\n            throw new WriterException(\"$bitmap: compression not supported in bitmap image.\\n\");\n        }\n\n        // Remove bitmap data: data size, hres, vres, colours, imp. colours.\n        $data = substr($data, 20);\n\n        // Add the BITMAPCOREHEADER data\n        $header = pack('Vvvvv', 0x000C, $width, $height, 0x01, 0x18);","sourceCodeStart":2499,"sourceCodeEnd":2535,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Writer/Xls/Worksheet.php#L2499-L2535","documentation":"processBitmap() reads the BMP's planes/bitcount pair and requires bitcount == 24: only uncompressed 24-bit true-color bitmaps can be stored in the Xls IMDATA record. 1/4/8-bit palette bitmaps and 16/32-bit formats are rejected with this exception.","triggerScenarios":"insertBitmap() with a common 8-bit palette BMP (saved by MS Paint defaults or older tools), a 1-bit monochrome scan, or a 32-bit BMP exported by image editors.","commonSituations":"Scanner output and legacy clip-art distributed as 8-bit BMPs; default save options of older Windows tooling.","solutions":["Re-encode the image as 24-bit BMP (GD: imagecreatetruecolor + imagebmp)","Convert to PNG/JPEG and use the Drawing API instead","Verify bit depth with getimagesize() before insertion"],"exampleFix":"// before\n$worksheetWriter->insertBitmap(1, 1, 'palette8.bmp');\n\n// after\n$src = imagecreatefrombmp('palette8.bmp');\n$true24 = imagecreatetruecolor(imagesx($src), imagesy($src));\nimagecopy($true24, $src, 0, 0, 0, 0, imagesx($src), imagesy($src));\nimagebmp($true24, 'true24.bmp');\n$worksheetWriter->insertBitmap(1, 1, 'true24.bmp');","handlingStrategy":"validation","validationCode":"$info = unpack('vplanes/vbits', (string) file_get_contents($path, false, null, 26, 4));\nif ($info === false || $info['bits'] !== 24) {\n    // re-encode through GD as 24-bit true color\n    $src = imagecreatefrombmp($path);\n    $dst = imagecreatetruecolor(imagesx($src), imagesy($src));\n    imagecopy($dst, $src, 0, 0, 0, 0, imagesx($src), imagesy($src));\n    imagebmp($dst, $path);\n}\n$worksheetWriter->insertBitmap($row, $col, $path);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Normalize every incoming BMP to 24-bit via imagecreatetruecolor() before insertBitmap()","Reject 1/4/8-bit palette sources early with a clear message instead of failing at save"],"tags":["phpspreadsheet","xls","bitmap","color-depth","image"],"backgroundTag":"unsupported-bit-depth","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}