dongying6896 2015-06-24 20:32
浏览 37
已采纳

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

I'm using a script for resize photos to another folder and keep the same name automaticaly found here and working great!

I have hundreds of photos in my source folder and i'm going to do this action often. I would like to optimize the time by making it resize the photos only if it sees the photos doesn't exist in the destination folder. How can we do that?

Here is the full code used:

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条回答 默认 最新

  • dongmu9253 2015-06-24 20:55
    关注

    Seems like you could just add one more condition to the resize function

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

    This should keep it from trying to do anything if the target file already exists.

    Another option would be to check if the destination file exists before calling the function

    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);
        }
    }   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口