2015-06-24 40 views
0

我正在使用一個腳本來調整大小的照片到另一個文件夾,並保持相同的名稱automaticaly發現here和工作很好!如何比較2個文件夾,如果文件丟失採取措施?

我在我的源文件夾中有數百張照片,我打算經常執行此操作。我想通過僅在看到照片不存在於目標文件夾中的情況下調整照片大小來優化時間。我們怎麼做到這一點?

這裏使用的全碼:

function imageResize($file, $path, $height, $width) 
{ 

     $target = 'smallphotos/'; 

     $handle = opendir($path); 

     if($file != "." && $file != ".." && !is_dir($path.$file)) 
     { 

       $thumb = $path.$file; 

       $imageDetails = getimagesize($thumb); 

       $originalWidth = $imageDetails[0]; 

       $originalHeight = $imageDetails[1]; 

       if($originalWidth > $originalHeight) 
       { 

        $thumbHeight = $height; 

        $thumbWidth = ($originalWidth/($originalHeight/$thumbHeight)); 

       } 
       else 
       { 

        $thumbWidth = $width; 

        $thumbHeight = ($originalHeight/($originalWidth/$thumbWidth)); 

       } 

       $originalImage = ImageCreateFromJPEG($thumb); 

       $thumbImage = ImageCreateTrueColor($thumbWidth, $thumbHeight); 

       ImageCopyResized($thumbImage, $originalImage, 0, 0, 0, 0, $thumbWidth, 
       $thumbHeight, $originalWidth, $originalHeight); 

       $filename = $file; 

       imagejpeg($thumbImage, $target.$filename, 100); 

     } 

     closedir($handle); 

} 

    $source = "photos"; 

    $directory = opendir($source); 

    //Scan through the folder one file at a time 

    while(($file = readdir($directory)) != false) 
    { 

      echo "<br>".$file; 

      //Run each file through the image resize function 

      imageResize($file, $source.'/', 640, 480); 

    } 

回答

1

好像你只需要添加一個條件調整大小功能

if ($file != "." && $file != ".." && !is_dir($path.$file) && !is_file($target.$file) {... 

這應該保持它嘗試,如果做任何事情目標文件已經存在。

另一種選擇是,以檢查目標文件調用該函數

while (($file = readdir($directory)) != false) { 
    echo "<br>".$file; 
    //Run each file through the image resize function (if it has not already been resized) 
    if (!is_file("smallphotos/$file")) { 
     imageResize($file, $source.'/', 640, 480); 
    } 
} 
+0

有趣的代碼之前存在,但遺憾的是它不斷調整的所有文件,即使它在目標文件夾中。我們不應該在*** while循環中添加一些東西嗎? – BackTrack57

+0

@ BackTrack57這很奇怪,當我測試它時它對我很好。當然,你可以在while循環中做同樣的檢查。這應該使它更快一點,因爲你會避免進行不必要的函數調用。我更新了答案,以顯示您可以這樣做的一種方式。 –

+0

工作非常好,非常感謝您的時間! – BackTrack57