PHP内存限制问题编辑多个文件

PHP内存限制问题编辑多个文件

问题描述:

I have a script that loops through a directory and edits all the images within it to certain sizes, the problem being that there are 1,000 images totaling up to 300MB.

Is there a way to remove this created image from the memory after each loop so that it doesn't count towards php memory_limit or do I just need to set a memory limit of -1?

foreach($image as $file){
    // obviousment this provides a valid image resource
    $new_image = Common::resizeImg($file['tmp_name'], $file['ext'], 215, 121);
    imagejpeg($new_image, SERVER_ROOT."/img/media/small-".$id.$file_ext, 100);

    // clear/reset this memory???
}

我有一个循环遍历目录的脚本,并将其中的所有图像编辑为特定大小,问题是 共有1,000张图像,总计高达300MB。 p>

有没有办法在每次循环后从内存中删除这个创建的图像,这样它就不会计入php memory_limit,或者我只需要 设置内存限制为-1? p>

  foreach($ image as $ file){
 //显而易见这提供了一个有效的图像资源
 $ new_image = Common:  :resizeImg($ file ['tmp_name'],$ file ['ext'],215,121); 
 imagejpeg($ new_image,SERVER_ROOT。“/ img / media / small  - ”。$ id。$ file_ext,100  ); 
 
 //清除/重置此内存??? 
} 
  code>  pre> 
  div>

You can try invoking imagedestroy, which will clean up any memory associated with the passed-in image resource:

foreach($image as $file){
    // obviousment this provides a valid image resource
    $new_image = Common::resizeImg($file['tmp_name'], $file['ext'], 215, 121);
    imagejpeg($new_image, SERVER_ROOT."/img/media/small-".$id.$file_ext, 100);

    imagedestroy($new_image);
}

Make sure you imagedestroy after you've written to the disk - otherwise you're adding each new image into memory.

You could change the memory_limit in php.ini to anything above the 16MB by default.

In my case i put it in 64 or 128 which is enough. Also you could free the memory with imagedestroy. For example:

$image = imagecreatetruecolor(100, 100); imagedestroy($image);

This way it frees the used memory.