Reputation: 842
I have this code:
echo PHP_INT_MAX . '<br/>';
echo 174400 * 249600 . '<br/>';
$img = imagecreatetruecolor(174400, 249600);
and it gives me this output:
9223372036854775807
43530240000
Warning: imagecreatetruecolor(): gd warning: product of memory allocation multiplication would exceed INT_MAX, failing operation gracefully in /home/bartek/Documents/WWW/WOF/application/controllers/EditorController.php on line 53
The first question is why imagecreatetruecolor is failing gracefully? And the second question is what can I do about it? How can I create big images in PHP?
Upvotes: 2
Views: 6940
Reputation: 7606
The error is triggered by guard code in the GD library, which checks for integer overflow. 174400 x 249600 = 43530240000 = 0x0000000A-229AC000. The number does not fit into a 32-bit integer so GD blows up. In theory, someone could go in there and fix the code so 64-bit integers are used instead. No one has done so yet, since it's rather unlikely people will allow more than 4 gig of memory per request. Someday it'll happen.
Incidentally, it takes 5 bytes to store each pixel in GD: one byte for R, G, B, and A, plus one byte for anti-aliasing.
Upvotes: 6