简体   繁体   中英

PHp - memory error when resizing a PNG image

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

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

您需要将php.ini中的memory_limit设置增加到这样的值

memory_limit = 128M

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM