php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #23177 imagecreatefromgd2 consumes too much memory
Submitted: 2003-04-12 07:59 UTC Modified: 2003-04-15 21:18 UTC
From: alberty at neptunelabs dot de Assigned:
Status: Not a bug Package: GD related
PHP Version: 4CVS-2003-04-12 (stable) OS: i686-pc-linux-gnu
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: alberty at neptunelabs dot de
New email:
PHP Version: OS:

 

 [2003-04-12 07:59 UTC] alberty at neptunelabs dot de
Hi,

i've found a bug with handling gd2 images.

The problem is affected with the bundled libgd (2.0.12).

I've convert an image (4932x3543 pixel) with the pngtogd2 tool:

./pngtogd2 mytestpic.png mytestpic.gd2 4932 2

My memory limit is (php.ini):
memory_limit = 162M 

The script:
<?php
$filename='mytestpic.gd2';
$image=imagecreatefromgd2($filename);
?>


results an wrong error message:

Fatal error: Allowed memory size of 169869312 bytes exhausted at (null):0 (tried to allocate 22581552 bytes) 

At first, 22581552 bytes is only the filesize of the mytestpic.gd2 file.

Also it shouldn be a problem to load an image with 4932x3543 pixel, because this image need no more
than 69896304 bytes (3+1 channels).


Regards,

Steve

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-04-14 19:47 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The memory allocation is:
width * height * channels, which in your case would be upwards of 200megs.
 [2003-04-15 02:04 UTC] alberty at neptunelabs dot de
Hi Ilia,

you have declared this bug report #23177 as bogus,
but I don?t understand your calculation:

4932 x 3543 x 3 = 50 MB
4932 x 3543 x 4 = 67 MB

Also you ignore the reported wrong error display in php:
>> (tried to allocate 22581552 bytes)
= 22 MB ?!

Your calculation is only correct if you reserve an integer for each channel instead of a byte.

Best regards,

-- 
Steve Alberty
 [2003-04-15 21:18 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

First of all the 162 memory limit was exausted not in one allocation, but a fair number of them. GD is not exactly memory friendly and can create many temporary & permanent buffers while reading the image from disk.
Consider gdImageCreateTrueColor() (internal GD function)
one of this things this function will do before even doing a single calculation is allocate:
(length * 4) * 2
(width * 2 * length)

These  two allocations alone already took some 39.5 megs

Keep in mind that while this is happening your png image had too been loaded to memory and is taking up a few dosen megs (at least).
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 04 11:01:37 2025 UTC