2013-07-02 74 views
0

我有下面的腳本,經過一定量的迭代後停止沒有錯誤。當它使用的圖像是〜4MB時,腳本在大約10次迭代後停止。當圖像大約1MB時,腳本在約30次迭代後停止。當圖像較小時,腳本可以進行500次迭代。它似乎沒有超時,因爲它發生在每次不同的時間。PHP腳本突然停止(imagecopyresampled)

當我在我自己的機器上使用xxamp測試它時,沒有任何錯誤,並且腳本完美無缺。

我猜這是一些類型的內存問題?在記憶方面,我完全沒有經驗和無知。

編輯: 我在imagecopyresampled函數之前和之後放置一個回聲。腳本似乎停止在這個功能上。

這裏是我的循環,對目錄中的每個文件雲:

while (false !== ($filer = readdir($handle))) { 
     if (is_file($srcDir . '/' . $filer)) { 

      set_time_limit(20); 
      $counter++; 
      $total = $x; 
      $percent = intval($counter/$total * 100)."%"; 

     // Javascript for updating the progress bar and information 
      echo '<script language="javascript"> 
      document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#333;\">&nbsp;</div>"; 
      document.getElementById("information").innerHTML="'.$counter.' images processed."; 
      </script>'; 

     //location/filename variable 
     $filename = $srcDir . '/' . $filer; 

     // This is for the buffer achieve the minimum size in order to flush data 
     echo str_repeat(' ',1024*64); 

     // Send output to browser immediately 
     flush(); 


     //get the extension of the image 
     $path_parts = pathinfo("$filename"); 
     $ext = strtolower ($path_parts['extension']); 

     $width = 100; 
     $height = 100; 

     // Get new dimensions 
     list($width_orig, $height_orig) = getimagesize($filename); 
     $ratio_orig = $width_orig/$height_orig; 

     if ($width/$height > $ratio_orig) { 
     $width = $height*$ratio_orig; 
     } else { 
     $height = $width/$ratio_orig; 
     } 

     // Resample 
     $image_p = imagecreatetruecolor($width, $height); 

     $image = imagecreatefromjpeg($filename); 
     imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); 

    // Output the small image 
    imagejpeg($image_p, "$destDir/$filer", 100); 

    //Move the big image 
    rename($srcDir . '/' . $filer, $destDirbig . '/' . $filer); 
    echo $counter; 

     } 

    } 
+0

我可能是錯的,但我不知道,'假==($文件= READDIR($句柄))'是正確的語法。 '!='是'不等於'的標準。如果你正在嘗試文本,如果它是'完全不相等',我想你想'!(($ file = readdir($ handle))=== false)' –

回答

0

set_time_limit(20);價值提升到一個更高的超時值...

提供了錯誤信息可能會更深入的瞭解。

此外,使用imagejpeg後,請使用imagedestroy($image_p)釋放內存。

+0

沒有錯誤信息,我已經嘗試了兩個那些,仍然得到我的循環突然結束。 – EvWill

+0

那麼,因爲你沒有檢查你想要處理什麼樣的文件,所以很有可能你正在打一個非JPEG文件......嘗試啓用'error_reporting'來查看。 –

+0

啓用了錯誤報告功能後,我將非JPEG文件放在那裏,並且出現錯誤。 – EvWill

0

也許嘗試添加一些自己的錯誤日誌記錄。

$filename = $srcDir . '/' . $filer; 

echo "Trying {$filename}"; 

if (is_file($filename)) { 

    //do the processing 

} else { 
    echo "{$filename} was not a file"; 
} 

然後你至少會看到它試過的文件以及它何時停止工作。看看是否有模式?

+0

問題是,如果我保持手動重新啓動腳本,所有圖像都會得到處理。它不停留在特定的圖像上,它在X張圖像後停止。 (X根據圖像大小而變化) – EvWill

0

我有這樣的問題,解決的辦法是臨時增加的內存限制腳本:

ini_set ("memory_limit", "100M");