PHp - memory error when resizing a PNG image

笑着哭i 提交于 2019-12-02 16:23:26

问题


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:


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




回答2:


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

memory_limit = 128M


来源:https://stackoverflow.com/questions/1722352/php-memory-error-when-resizing-a-png-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!