views:

266

answers:

2

Hi all,

I have a script that creates a thumbnail out of an uploaded image. it works fine with jpgs, but gives me an error

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 26250000 bytes)

when I upload a png image.

The script is:

//create thumbnail; $modwidth and height are calculated in another part of the script
//$original is the path to the full sized image

$tn = imagecreatetruecolor($modwidth, $modheight); 
switch (strrchr($new_image_name,'.')) {
  case ".jpg":
    $image = imagecreatefromjpeg($original);
    break;
  case ".jpeg":
    $image = imagecreatefromjpeg($original);
    break;
  case ".png":
    $image = imagecreatefrompng($original);
    break;
  case ".gif":
    $image = imagecreatefromgif($original);
    break;
}
imagecopyresampled($tn, $image, 0, 0, $x_pos, $y_pos, $modwidth, $modheight, $width, $height); 
switch (strrchr($new_image_name,'.')) {
  case ".jpg":
    imagejpeg($tn, $target_path, 100);
    break;
  case ".jpeg":
    imagejpeg($tn, $target_path, 100);
    break;
  case ".png":
    imagepng($tn, $target_path, 0);
    break;
  case ".gif":
    imagegif($tn, $target_path);
    break;
}

As I said it works perfectly with JPGs and also with GIFs. That memory error appears only with PNGs, and I have only used a 1.2Mb image.

How can i solve this? thanks Patrick

+1  A: 

You need to increase memory_limit setting in php.ini to something like this

memory_limit = 128M
David Kuridža
+2  A: 

Use ini_set('memory_limit', '256M'); before script.

Cesar
Thanks Cesar, it worked.Do I have to decrease that memory limit after the script? (in general, is there any problem in setting a very high memory limit?)
patrick
No problem, this define the memory only for this script, if you need for all scripts you need to change in php.ini
Cesar
To clarify: your image is quite big, if you upload a bigger image your script will fail again. Just try to have a limit on the image size sent to the script.
Ast Derek
This is just moving the goalposts, it has to be said. (i.e.: You'll eventually run out of memory.) If feasible, it's sometimes wise (and faster) to shell out to an external command tool such as ImageMagick.
middaparka