2011-12-15 61 views
2

我运行一个循环,其中包含一个脚本,用于识别图像中的白色背景,然后复制没有白色背景的图像的裁剪版本。在循环结束时,我使用imagedestroy释放内存,但脚本仍然超过内存限制(> 256mb)。 这怎么可能?Imagedestroy不能释放内存

代码内环:

$img = imagecreatefromjpeg($imgSrc); 

/* Script for identifying whitespace */ 

//create new image 
$newimg = imagecreatetruecolor(
      imagesx($img)-($wsLeft+$wsRight), imagesy($img)-($wsTop+$wsBottom)); 

//get new width and height 
$width = imagesx($newimg); 
$height = imagesy($newimg); 

//free memory 
imagedestroy($newimg); 
imagedestroy($img); 
+3

PHP是垃圾回收 - 只是因为你在你的代码中销毁一个变量并不意味着内存被立即释放。 [阅读此](http://php.net/manual/en/features.gc.php)了解更多信息。做`$ img = $ newimg = NULL;`可能会有所帮助。 – DaveRandom 2011-12-15 13:36:46

回答

0

我有记忆同样的问题(在循环中创造数百幅图像)。尝试尽可能多地在功能上。由于任何原因,PHP处理图像资源的功能不同。

<?php 
$newimg = imagecreatefromjpeg2($imgSrc,$wsLeft,$wsRight,$wsTop,$wsBottom); 

function imagecreatefromjpeg2($imgSrc,$wsLeft,$wsRight,$wsTop,$wsBottom){ 
    $img = imagecreatefromjpeg($imgSrc); 
    $newimg = imagecreatetruecolor(
    imagesx($img)-($wsLeft+$wsRight), imagesy($img)-($wsTop+$wsBottom)); 


     imagedestroy($img); 
     return $newimg 
} 
$width = imagesx($newimg); 
$height = imagesy($newimg); 
?>