Skip to content

Critical performance issue: excessive color allocations. #7

@galchenok

Description

@galchenok

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:

         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);
             }
         }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions