2

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

}
?>
5
  • 2
    Show us the function in resize_class.php that contains line 34, and then people can try to help you. How are we supposed to debug code we can't see? :) Commented Aug 9, 2011 at 5:11
  • Have you run an xDebug trace to see what is taking all the memory? Maybe you can find the leak so you can stay under the 50MB limit you exceeded? Commented Aug 9, 2011 at 5:52
  • Sorry about that see edit above regarding line 34. I will take a look at xDebug Commented Aug 9, 2011 at 5:59
  • @Denoteone I believe the request was for the entire function / method containing line #34. A single line isn't going to help much though at a guess, I'd say you're not cleaning up resources, eating more memory each time you use imagecreatefromjpeg() Commented Aug 9, 2011 at 6:02
  • @Phil the full script has been added. I also noted that I am looping through the script and using the class multiple times each time the page is submitted. Commented Aug 9, 2011 at 6:14

4 Answers 4

3

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
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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

2 Comments

This is the first comment on this answer
@Phil "...based on the error I am not close to the limit." <-- the OP seems not understanding what the 50M means in the php.ini file.
0

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

post_max_size = 200M
upload_max_filesize = 192M

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.