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

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.