2012-01-11 42 views
2

下面是一個文件iterarot unction我正在處理文件夾迭代並收集所有圖像,它將每個圖像的文件路徑保存到$files array,然後在代碼後面以迭代$files array添加多個然後1個項目到一個PHP數組並迭代它

現在我正在處理的代碼實際上通過imagemagick感知一個文件3次(我沒有寫入原始代碼),所以我試圖改進整個代碼,到目前爲止,我已經切下降到2倍,當這完成時,每張圖像只有1次。

基本上當前的代碼通過ImageMagick的運行的圖片看到它,它是一個圖像或沒有,以後它再喝酒,但這次來獲得圖像擴展,然後第三次是爲了確保它是一個(我知道很瘋狂,人們寫的代碼很糟糕)

所以要減少到1次,當$files array第一次通過imagemagick運行一個圖像文件,以確保它是一個圖像,我現在有函數返回文件擴展名而不是簡單的真/假。所以當文件路徑保存到數組時,我想將返回的文件擴展名保存到具有該特定文件路徑的數組中,以便稍後可以訪問它,而無需再次運行imagemagick。

所以這裏是循環徹底抓取所有圖像文件的文件夾,然後將它們發送到imagemagick的功能。然後,它保存文件路徑的排列,可有人告訴我如何將文件擴展名保存這個數組,然後如何訪問代碼的功能

function find_recursive_images($path) 
{ 
    $directory = new RecursiveDirectoryIterator($path,RecursiveDirectoryIterator::SKIP_DOTS); 
    $iterator = new RecursiveIteratorIterator($directory,RecursiveIteratorIterator::LEAVES_ONLY); 
    $exntensions = array("png", "jpg", "jpeg", "gif", "bmp"); 

    foreach ($iterator as $fileinfo) { 
     if (in_array($fileinfo->getExtension(), $exntensions)) { 
      //This gets the filetype of the image from ImageMagick 
      $img = self::get_type($fileinfo->getPathname()); 
      if ($img) { 
       //This sets the full file path to the $files array 
       $files[] = $fileinfo->getPathname(); 

       // I tried something like this but not correct 
       $files[][] = $img; 
      } 
     } 
    } 
    return $files; 
} 

我那麼像這樣訪問權的陣列目前低於...

foreach ($files as $file) 
{  
    // ie E:\Server\_ImageOptimize\img\testfiles\css3-please.png 
    $source_file = $file; 

    // $file also needs to return a filetype that is got when it ran thorugh imagemagick 

} 
+0

你能夠證明你想要什麼樣的陣列看起來像它通過之後的示例循環? – jprofitt 2012-01-11 15:43:16

+1

這裏有身份危機嗎? ;) – jprofitt 2012-01-11 15:52:56

+0

@jprofitt對每個賬戶的問題是有限制的 – CodeDevelopr 2012-01-11 15:58:30

回答

1

如何爲每個圖像使用關聯數組?試試這個: 要建立你的數組:通過您的阿雷 $files[] = array(location => $fileinfo->getPathname(), type => $img);

要循環:

foreach ($files as $file) {  
    $source_file = $file->location; 
    $extension = $file->type; 

    //do some stuff using your info 
} 
+0

:'foreach($ array as $ key => $ val){echo'key =「。 $鍵。 「value =」。 $ VAL; }' – jere 2012-01-11 15:51:35

+0

ahh是的,謝謝你,在2分鐘內讓我接受這件事 – CodeDevelopr 2012-01-11 15:52:27

相關問題