1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| <?php error_reporting(E_ERROR); date_default_timezone_set('PRC'); echo date('Y-m-d H:i:s') . PHP_EOL;
abstract class AbstractImagesGroup {
public final function imagesGroup($sourPath, $destPath) { $this->treeDir($sourPath); $this->readExif(); $this->copyFile($destPath); }
protected abstract function treeDir($sourPath);
protected abstract function readExif();
protected abstract function copyFile($destPath); }
class JpgGroup extends AbstractImagesGroup { protected $arrFile = [];
protected $fileGroup = [];
protected function treeDir($sourPath) { $result = []; $myDir = dir($sourPath); while ($file = $myDir->read()) { if ((is_dir($sourPath . DIRECTORY_SEPARATOR . $file)) and ($file != ".") and ($file != "..") and ($file != ".idea")) {
$result[$sourPath . DIRECTORY_SEPARATOR . $file] = $this->treeDir($sourPath . DIRECTORY_SEPARATOR . $file);
} elseif (($file != ".") and ($file != "..") and ($file != ".idea")) { $result[] = $sourPath . DIRECTORY_SEPARATOR . $file; $this->arrFile[] = $sourPath . DIRECTORY_SEPARATOR . $file; }
} $myDir->close(); return $result; }
protected function readExif() { foreach ($this->arrFile as $value) { if (($exif = exif_read_data($value)) !== false) {
$date = '000000'; if ($exif['DateTime']) { $date = date('Ym', strtotime($exif['DateTime'])); }
$make = 'Other'; if ($exif['Make']) { $make = $exif['Make']; } $this->fileGroup[$make][$date][] = $value; } } }
protected function copyFile($destPath) { foreach ($this->fileGroup as $make => $date) { foreach ($date as $key => $value) {
$path = $destPath . DIRECTORY_SEPARATOR . $make . DIRECTORY_SEPARATOR . $key;
is_dir($path) or mkdir($path, 0777, true);
foreach ($value as $item) { if (!is_file($item)) { continue; } $destFile = $path . DIRECTORY_SEPARATOR . basename($item);
if (copy($item, $destFile)) { echo $item . ' group success' . PHP_EOL; } } } } } }
$sourPath = 'C:\\Users\\loous\\Desktop\\test';
$destPath = __DIR__ . DIRECTORY_SEPARATOR . 'imagesGroup';
$file = new JpgGroup();
$file->imagesGroup($sourPath, $destPath);
|
修改文件中变量 $sourPath,照片所在文件目录,可以递归读取照片文件。
将分组完成的照片复制到 $destPath,不会改变原文件