如何比较2个文件夹,如果文件丢失,请采取措施?

时间:2015-06-24 20:32:05

标签: php image

我正在使用脚本将照片调整到另一个文件夹,并保持同名自动找到here并且效果很好!

我的源文件夹中有数百张照片,我会经常这样做。我想通过仅在目标文件夹中看不到照片时调整照片大小来优化时间。我们怎么能这样做?

以下是使用的完整代码:

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 个答案:

答案 0 :(得分: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);
    }
}