2011-12-15 60 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); 
?>