Memory exhauted on a GD beforecreatefromjpeg function

fomalhaut

Member
Messages
107
Reaction score
0
Points
16
Hello.

I'm trying to resize a client image before insert it into my web site. The image is choosen on the client post throught a form, and is correctly obtained. It's accessible by $_FILES['fileNameAIns'] array.

PHP:
  $max_cote = 1600;
  list($wOri, $hOri) = getimagesize($_FILES['fileNameAIns']['tmp_name']);
  $ratio = $wOri / $hOri;
  if ($wOri > $hOri) {
    $modWidth = $max_cote;
    $modHeight = $max_cote / $ratio;
  }
  else {
    $modHeight = $max_cote;
    $modWidth = $max_cote * $ratio;
  }
  $tn = imagecreatetruecolor($modWidth, $modHeight);
  $imageOri = $_FILES['fileNameAIns']['tmp_name'];
  echo '<br />before imagecreatefromjpeg:'.$imageOri;
  $image = imagecreatefromjpeg($imageOri);
  echo '<br />before imagecopyresampled';
  imagecopyresampled($tn, $image, 0, 0, 0, 0, $modWidth, $modHeight, $wOri, $hOri);
  echo '<br />before imagejpeg';
  imagejpeg($tn);
the last messages I find on the page is :
Code:
<br />before imagecreatefromjpeg:C:\xampp\tmp\php1624.tmp<br />
<b>Fatal error</b>:  Allowed memory size of 33554432 bytes exhausted (tried to allocate 3072 bytes) in <b>C:\xampp\htdocs\jardin\inc\gesJardin.inc</b> on line <b>82</b><br />
Line 82 is the imagecreatefromjpeg function.

Could someone show me where I'm wrong please ?

Just a precision : on the disk of client machine, the image size is 3,05 Mo.
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
1. Is this on x10hosting? The memory limit is supposed to be 64M

2. Apparently imagecreatefromjpeg can be a memory hog. See: PHP manual for imagecreatefromjpeg and search the page for 'channel'

Quoting from one of the posts...

The memory required to load an image using imagecreatefromjpeg() is a function
of the image's dimensions and the images's bit depth, multipled by an overhead.
It can calculated from this formula:
Num bytes = Width * Height * Bytes per pixel * Overhead fudge factor
Where Bytes per pixel = Bit depth/8, or Bits per channel * Num channels / 8.
This script calculates the Overhead fudge factor by loading images of
various sizes.
 

fomalhaut

Member
Messages
107
Reaction score
0
Points
16
Thank you very much descalzo.
1. No, it was not on x10, because I test my application on my localhost. So the memory limit is probably 32Mo.

2.I've add before the image manipulation the function ini_set('memory_limit', '100M')...

3. ... and it works well !

Thanks again. I'll go further into the memory management.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
fomalhaut said:
2.I've add before the image manipulation the function ini_set('memory_limit', '100M')...

3. ... and it works well !

.

Be aware that you cannot set the memory limit on x10. If you want to test the script to see if it will work on x10, set the memory limit to 64M on your local script.
 

fomalhaut

Member
Messages
107
Reaction score
0
Points
16
Hello Descalzo

Yes, of course, you are right. I've made some tests on my owm machine and I can limit the memory at 40Mo, but as a precaution, I code
PHP:
ini_set('memory_limit', '50M');
This is sufficient.

Thanks again and have a good day.
 
Top