PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception
Could not get image alpha color
Error message
Could not get image alpha color
What it means
While cloning a palette-based MemoryDrawing that has a transparent color, cloneResource() copies the transparency by calling imagecolorallocatealpha() on the freshly created clone. If that allocation fails — typically the clone's 256-color palette is exhausted, or GD cannot allocate the color under memory pressure — it throws 'Could not get image alpha color'.
Source
Thrown at src/PhpSpreadsheet/Worksheet/MemoryDrawing.php:109
imagealphablending($clone, false);
imagesavealpha($clone, true);
} else {
$clone = imagecreate($width, $height);
if (!$clone) {
throw new Exception('Could not clone image resource');
}
// If the image has transparency...
$transparent = imagecolortransparent($this->imageResource);
if ($transparent >= 0) {
// Starting with Php8.0, next function throws rather than return false
$rgb = imagecolorsforindex($this->imageResource, $transparent);
imagesavealpha($clone, true);
$color = imagecolorallocatealpha($clone, $rgb['red'], $rgb['green'], $rgb['blue'], $rgb['alpha']);
if ($color === false) {
throw new Exception('Could not get image alpha color');
}
imagefill($clone, 0, 0, $color);
}
}
//Create the Clone!!
imagecopy($clone, $this->imageResource, 0, 0, 0, 0, $width, $height);
$this->imageResource = $clone;
}
/**
* @param resource $imageStream Stream data to be converted to a Memory Drawing
*
* @throws Exception
*/
public static function fromStream($imageStream): selfView on GitHub (pinned to 65b080eef4)
Solutions
- Convert the image to truecolor before cloning: imagepalettetotruecolor($gd) — the truecolor clone path does not allocate palette entries.
- Re-quantize the source to 255 or fewer colors so the alpha entry fits.
- Catch the failure and rebuild the drawing from its rendered bytes via MemoryDrawing::fromString($drawing->getContents()) instead of cloning.
Example fix
// before
$copy = clone $sheet; // transparent GIF -> Could not get image alpha color
// after
$gd = $memoryDrawing->getImageResource();
if ($gd !== null && !imageistruecolor($gd)) {
imagepalettetotruecolor($gd);
}
$copy = clone $sheet; Defensive patterns
Strategy: fallback
Validate before calling
$gd = $memoryDrawing->getImageResource();
if ($gd !== null && !imageistruecolor($gd)) {
imagepalettetotruecolor($gd); // truecolor clones never allocate palette colors
} Try / catch
try {
$copy = clone $sheet;
} catch (PhpSpreadsheetException $e) {
if (str_contains($e->getMessage(), 'alpha color')) {
// palette exhausted: rebuild the drawing from its rendered bytes instead
$new = MemoryDrawing::fromString($memoryDrawing->getContents());
$sheet->removeDrawingByOffset... // replace the drawing, then retry
}
} Prevention
- Transparent GIF/PNG-8 sources take the palette clone path — convert to truecolor early
- Quantize palette images to at most 255 colors before embedding
- Treat clone failures as a signal to rebuild drawings rather than to deep-copy sheets
When it happens
Trigger: clone $worksheet / $sheet->copy() on a sheet holding a transparent GIF or 8-bit PNG whose palette is full when the alpha entry is added; memory-constrained environments where the color allocation fails.
Common situations: Template reports that embed transparent GIF/PNG-8 logos or icons; deep-copying spreadsheets (clone, copy) in loops; images quantized to exactly 256 colors before embedding.
Related errors
- Could not clone image resource
- Value cannot be converted to an image
- Unsupported binary comparison operator
- Cloning the calculation engine is not allowed!
- Unsupported numeric binary operation
AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17).
Data as JSON: /api/errors/19589da67f9f582c.
Report an issue: GitHub.