0

I am trying batch resize a bunch of images (~220). I am getting: (everytime)

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 19520 bytes) in C:\xampp\htdocs\jason\inc\admin\image.resize.functions.php on line 31

I get the error at the same point EVERY TIME, regardless of how many times I call $n->resizeToHeight($c). It will cycle through about 100 photos then crash as soon as I try to resize ONE. Or I can resize 100 photos and it will crash on the 101st. I can even go through all cases of switch($k) (cause it resize 300 photos), and it will still crash after the 101st instance of the class Image.

Does my explanation of the error make sense? I am throwing around "100", it may not necessarily be 100, could be 75, or 125 (don't know the exact number). It just consistently crashes at the same spot.

Should be noted: I am testing with XAMPP. I haven't tried it in a real environment (because I can't yet).

resize-images.php (what is necessary)

    foreach($imgs as $img){
        $i = new Image($img['cid']);
        foreach($go as $k=>$c){
            if($i->$k > $c+IMGTOL || $i->$k < $c-IMGTOL) {
                $n = new SimpleImage();
                $n->load(ORIGABSLOC."/".$i->filename);
                $n->resizeToHeight($c);
                switch($k){
                    case "image_height":
                        $loc = IMGABSLOC;
                        break;
                    case "thumb_height":
                        $loc = THUMBABSLOC;
                        break;
                    case "mobile_height":
                        $loc = MOBILEABSLOC;
                        break;
                }
                $n->save($loc."/".$i->filename);
                $n->destroy();
                unset($n);
            }
            echo "<style>#$k-$count{background-color:blue;}</style>";
            flush_();
        }
        $count++;
        unset($i);
    }

I echo the <style> and flush_() it so I get a loading bar effect. Doesn't matter if I flush or not, still crashes at the same spot.

Image class (what is necessary)

class Image{
function Image($id){
    $img = selectImages(array('type'=>4,'id'=>$id));
    if(sizeof($img) < 1) {
        return NULL;
    }
    if(sizeof($img)>0){
        foreach($img[0] as $k=>$c){
            if(!is_numeric($k)) {
                $this->$k = $c;
            }
        }
        $this->type = $this->display_type;
        $size = @getimagesize(IMGABSLOC."/".$this->filename);
        $this->height = $size[1];
        $this->width = $size[0];
        $this->image_height = $size[1];
        $this->image_width = $size[0];
        $size = @getimagesize(ORIGABSIMGLOC."/".$this->filename);
        $this->original_height = $size[1];
        $this->original_width = $size[0];
        $size = @getimagesize(THUMBABSLOC."/".$this->filename);
        $this->thumb_height = $size[1];
        $this->thumb_width = $size[0];
        $size = @getimagesize(MOBILEABSLOC."/".$this->filename);
        $this->mobile_height = $size[1];
        $this->mobile_width = $size[0];
        $this->anchor = $this->album;
    }
    else return false;
}

SimpleImage Class (what is necessary)

    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 resizeToHeight($height) {
      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $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());
      imagedestroy($this->image);
      $this->image = $new_image;   
   }
   function destroy(){
    imagedestroy($this->image);
    }
}
Jason
  • 3,357
  • 1
  • 22
  • 29
  • It's been a while since I have seen PHP4 OO. Anyway, what version of PHP are you using? You may find [this post](http://stackoverflow.com/questions/3061440/php-possible-memory-leak) helpful. Try putting some debugging code (memory_get_usage()) to find out where is the leak coming from. – netcoder Nov 03 '10 at 22:29
  • Figured it out. `imagecreatefromjpeg` was getting a large image. I didn't realize the image was that big. It's fixed now, thanks. – Jason Nov 03 '10 at 22:53

1 Answers1

0

Fixed. Appears as though it didn't like some images.

Jason
  • 3,357
  • 1
  • 22
  • 29