{"record":{"id":"8f442cf18a10af69","repo":"PHPOffice/PhpSpreadsheet","slug":"bitmap-largest-image-width-supported-is-65k-n","errorCode":null,"errorMessage":"$bitmap: largest image width supported is 65k.\\n","messagePattern":"\\$bitmap: largest image width supported is 65k\\.\\\\n","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Writer\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/Writer/Xls/Worksheet.php","lineNumber":2507,"sourceCode":"        $size_array = unpack('Vsa', substr($data, 0, 4)) ?: [];\n        /** @var int */\n        $size = $size_array['sa'];\n        $data = substr($data, 4);\n        $size -= 0x36; // Subtract size of bitmap header.\n        $size += 0x0C; // Add size of BIFF header.\n\n        // Remove bitmap data: reserved, offset, header length.\n        $data = substr($data, 12);\n\n        // 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);","sourceCodeStart":2489,"sourceCodeEnd":2525,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/Writer/Xls/Worksheet.php#L2489-L2525","documentation":"While parsing the BMP info header, processBitmap() reads the width field and rejects values above 0xFFFF (65,535 pixels). The BIFF8 IMDATA record stores dimensions in 16-bit fields, so wider images cannot be represented in the .xls output this code path builds.","triggerScenarios":"Calling insertBitmap() with a valid 24-bit BMP whose pixel width exceeds 65,535 (e.g. large panoramas or print-resolution images).","commonSituations":"High-resolution source photos fed directly into legacy Xls embedding; automated pipelines that never downscale images.","solutions":["Resize the image so width and height are both at most 65,535 px","Export as Xlsx and attach the image through the Drawing API, which has no such limit","Scale down via GD/Imagick before calling insertBitmap()"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"$fh = fopen($path, 'rb');\nfseek($fh, 18); // biWidth at offset 18 in the info header\n$width = unpack('V', (string) fread($fh, 4))[1];\nfclose($fh);\n\nif ($width > 0xFFFF) {\n    $im = imagecreatefrombmp($path);\n    imagescale($im, 65535, (int) (imagesy($im) * 65535 / $width));\n    imagebmp($im, $path); // overwrite with resized 24-bit BMP\n}\n$worksheetWriter->insertBitmap($row, $col, $path);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Downscale images to sane display size before embedding - Xls cells render them small anyway","For images above 65k px, use the Xlsx writer + Drawing"],"tags":["phpspreadsheet","xls","bitmap","image-size","limits"],"backgroundTag":"image-dimension-limit-exceeded","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}