致命错误:允许的内存大小

时间:2011-08-09 05:08:29

标签: php

我从我的图像调整大小脚本中得到一个致命的错误,它接受jpegs并调整它们然后保存。我上传的图片没有超过最大上传限制(不会关闭),并且根据错误我没有接近限制。

我是否遗漏了关于如何使用内存以及为什么它会随机失败但是处理更大的图像?

Fatal error: Allowed memory size of 52428800 bytes exhausted (tried to allocate 15756 bytes) in /home/content/t/w/e/myserver/html/test/includes/resize_class.php on line 34

我在同一目录中有一个php.ini文件

memory_limit = 50M
post_max_size = 100M
file_uploads = On
upload_max_filesize = 192M

第34行:$this->image = imagecreatefromjpeg($filename);

resize_calss.php: 另外要注意。用户一次最多上传3个iamges,我正在循环使用此类来调整大小并将拇指保存在我的服务器上     

/*
* File: SimpleImage.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/

class SimpleImage {

   var $image;
   var $image_type;

   function load($filename) {

      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {

         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {

         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {

         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {

      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {

         imagegif($this->image,$filename);
      } elseif( $image_type == IMAGETYPE_PNG ) {

         imagepng($this->image,$filename);
      }
      if( $permissions != null) {

         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {

      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {

         imagegif($this->image);
      } elseif( $image_type == IMAGETYPE_PNG ) {

         imagepng($this->image);
      }
   }
   function getWidth() {

      return imagesx($this->image);
   }
   function getHeight() {

      return imagesy($this->image);
   }
   function resizeToHeight($height) {

      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }

   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }

   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100;
      $this->resize($width,$height);
   }

   function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;
   }      

}
?>

3 个答案:

答案 0 :(得分:3)

我在我的image manipulation class中做过的事情就是在创建一个新图像后(例如在resize()方法中)破坏原始图像。

尝试将resize()方法更改为此

function resize($width,$height) {
    $new_image = imagecreatetruecolor($width, $height);
    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());

    imagedestroy($this->_image);

    $this->image = $new_image;
}

我还会在load()方法

中添加类似内容
function load($filename) {
    if (is_resource($this->image)) {
        imagedestroy($this->image);
    }

    // and the rest
}

答案 1 :(得分:2)

图片重新调整大小会占用大量内存, 您可以考虑使用ini_set来提高内存限制 特别是在resize_class.php

中的方法(调整大小)

相关问题

答案 2 :(得分:0)

您的脚本似乎已超过memory limit。 (52,428,800字节= 50MB)