开发者

Fatal error: Allowed memory size

I am getting a fatal error from my image resize script that takes jpegs and resizes them and then saves. The image I am uploading are not over the max upload limit (not ever close) and based on the error I am not close to the limit.

Am I missing something about how the memory is used and why it would fail randomly but work on images that are larger?

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

I have a php.ini file in the same directory

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

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

resize_calss.php: Also to note. The user is uploading up to 3 iamges at a time and I am looping through and using this class to resize and save the thumbs on my server

/*
* 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;
   }      

}
?>


Something I've done in my image manipulation class that this one does not do is destroy the original image after creating a new one (such as in the resize() method).

Try changing the resize() method to this

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;
}

I'd also add something similar in the load() method

function load($filename) {
    if (is_resource($this->image)) {
        imagedestroy($this->image);
    }

    // and the rest
}


image re-size will used lots memory, you can consider to use ini_set to boost the memory limit specifically at the method (doing resize) in resize_class.php

related questions

  • Efficient JPEG Image Resizing in PHP
  • reasonable PHP memory_limit for image resize
  • an enthusiasm guy who create a memory calculator for this


Your script seems to have exceeded the memory limit. (52,428,800 bytes = 50MB)


post_max_size shall be graeter than or equal to upload_max_filesize in php.ini

post_max_size = 200M
upload_max_filesize = 192M
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜