Calling imagecolorallocate() for every pixel results in millions of allocations for larger images. A 2000×2000 image would perform 4 million allocations, causing severe performance degradation and memory issues.
for ($y = 0; $y < $height; $y++) {
for ($x = 0; $x < $width; $x++) {
[$r, $g, $b] = $decoded[$y][$x];
- imagesetpixel($image, $x, $y, imagecolorallocate($image, $r, $g, $b));
+ $color = ($r << 16) | ($g << 8) | $b;
+ imagesetpixel($image, $x, $y, $color);
}
}
Calling imagecolorallocate() for every pixel results in millions of allocations for larger images. A 2000×2000 image would perform 4 million allocations, causing severe performance degradation and memory issues.
Use direct color values instead: