2010-11-03 57 views
0

我正在尝试批量调整一大堆图像(〜220)的大小。我得到:(每次)(图像调整大小)每次都耗尽内存,即使未创建图像实例

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

我得到的错误在同一点上每一次,不管有多少次我打电话$n->resizeToHeight($c)。当我尝试调整ONE大小时,它会循环显示大约100张照片,然后崩溃。或者我可以调整100张照片,并且会在第101张上崩溃。我甚至可以通过switch($k)(导致它调整300张照片)的所有情况,并且它仍会在类Image的第101个实例之后崩溃。

我对错误的解释是否有意义?我投掷“100”,它可能不一定是100,可能是75或125(不知道确切的数字)。它只是一直在同一个地方崩溃。

需要注意的是:我正在用XAMPP进行测试。我没有在真实的环境中尝试过它(因为我还不能)。

resize-images.php(什么是必要的)

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

我呼应<style>flush_(),所以我得到一个加载条的效果。如果我冲洗或不冲洗,无关紧要,仍然在同一个地方崩溃。

Image class(什么是必要的)

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(什么是必要的)

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); 
    } 
} 
+0

自从我看到PHP4 OO已经有一段时间了。无论如何,你使用的是什么版本的PHP?你可能会发现[这篇文章](http://stackoverflow.com/questions/3061440/php-possible-memory-leak)有帮助。尝试使用一些调试代码(memory_get_usage())来查明泄漏来自哪里。 – netcoder 2010-11-03 22:29:18

+0

想通了。 'imagecreatefromjpeg'获得了一张大图片。我没有意识到这个形象是那么大。现在已经修好了,谢谢。 – Jason 2010-11-03 22:53:21

回答

0

固定。看起来好像它不喜欢某些图像。