批量修改照片文件名日期排序
发表日期:2022-08-05 23:15:29 | 来源: | | 浏览(1062) 分类:PHP杂项
前言:为什么要写这个小工具?因为换过几台手机及电脑,十年里照片无数,大多照片拍摄时文件名就是日期,但是还有很多照片是微信QQ等同步到电脑上的,文件名已经是随机的了,话说为什么要修改照片文件名为日期的统一格式呢?
因为很多照片早已不知道是哪年哪天拍摄的。我想要让文件夹里按照日期时间来排序照片,这样我就可以清晰的看到这些年的时光,拼凑起凌乱的记忆。
2022年5月9日,2:50:10 半夜写完了这个类,把数千张存档的照片基本排序好了。
思路是读照片的创建时间&修改时间&拍摄时间,然后比较取最早的。文件名和md5读到数组里取最早的那个相同文件去重。生成文件名为img_20150816_131130.jpg 这种格式的文件
用法:把照片全部放在一个文件夹,把这个php文件命名为1.php也放在照片目录,然后命令行执行 : php 1.php
注意点是,请做好照片备份,代码里有许多if(true),if(false)是写的时候用的零时开关,自己理解吧,这个是临时起意写的demo,抽空我写成类工整一点。
<?php
$dir = "D:\个人收藏\照片备份 - 副本\\";
$md5 = [];
$i = 0;
if ($handle = opendir($dir)) {
while (($file = readdir($handle)) !== false) {
if (!in_array($file, ['.', '..', ['1.php']]) && !is_dir($file)) {
if ($file === '1.php') continue;
if (!file_exists($file)) {
var_dump($file);
} else {
if (false) {//是否转小写文件名
rename($file, strtolower($file));
}
if (true) {//程序主体
++$i;
$index = str_pad($i, 3, "0", STR_PAD_LEFT);
$filectime = filectime($file);
$filemtime = filemtime($file);
$time = $filemtime < $filectime ? $filemtime : $filectime;
$EXTENSION = pathinfo($file, PATHINFO_EXTENSION);
@$exif_data = exif_read_data($file);
if (!empty($exif_data['DateTime'])) {
$exif_date = $exif_data['DateTime'];
$fileptime = strtotime($exif_date);
$time = $time < $fileptime ? $time : $fileptime;
}
// DateTimeDigitized
if (!empty($exif_data['DateTimeOriginal'])) {
$exif_date = $exif_data['DateTimeOriginal'];
$fileptime = strtotime($exif_date);
if ($fileptime == false) {
var_dump($file . "\t" . $exif_date . "\t" . $fileptime);
} else {
$time = $time < $fileptime ? $time : $fileptime;
}
}
// exit();
// $filectime = date('Y-m-d H:i:s',$filectime);
// $filemtime = date('Y-m-d H:i:s',$filemtime);
// $time = date('Y-m-d H:i:s',$time);
// var_dump( "$file 创建时间:$filectime 修改时间:$filemtime 取时间:$time" );
// img_20150816_131130.jpg
$newfile = "IMG_" . date('Ymd_His', $time) . "." . $EXTENSION;
if (file_exists($newfile)) {
$newfile = "IMG_" . date('Ymd_His', $time) . "_" . $index . "." . $EXTENSION;
}
rename($file, $newfile);
// var_dump($newfile);
}
if (false) {//是否去重复
$file_md5 = md5_file($file);
if (isset($md5[$file_md5])) {
$repeat = $md5[$file_md5];
var_dump("$repeat -> $file");
$repeat_filectime = filectime($repeat);
$repeat_filemtime = filemtime($repeat);
$repeat_time = $repeat_filemtime < $repeat_filectime ? $repeat_filemtime : $repeat_filectime;
$filectime = filectime($file);
$filemtime = filemtime($file);
$time = $filemtime < $filectime ? $filemtime : $filectime;
if ($time <= $repeat_time) {
unlink($repeat);
$md5[$file_md5] = $file;
} else {
unlink($file);
}
} else {
$md5[$file_md5] = $file;
}
}
if (false) {//是否把照片归类到每天一个文件夹(事实发现同一天拍摄的照片大多是同一类)
$filectime = filectime($file);
$filemtime = filemtime($file);
$time = $filemtime < $filectime ? $filemtime : $filectime;
$_dir = date('Y-m-d', $time);
@mkdir($_dir);
if (!file_exists($_dir . '\\' . $file)) {
copy($file, $_dir . '\\' . $file);
}
}
}
}
}
closedir($handle);
}
?>效果:

- PHP(0)
- PHP杂项(34)
- ip白黑名单验证(0)
- 自实现getallheaders函数 获取header信息(0)
- 随机颜色(0)
- 中文全角半角互转(0)
- 字符和unicode互转(0)
- 日期时间友好展示为今天/昨天/前天/分钟/小时/天前(0)
- 递归创建多级目录(0)
- 字符串截取追加...(0)
- 批量反/转义特殊字符,数据库安全过滤写入,支持字符串、多维数组、对象集合(0)
- 压缩HTML,去换行空格,注释(0)
- 获取文章html中的img标签图片src地址(0)
- html过滤标签空格提取纯文本text(0)
- 自实现http_build_url 函数(0)
- 获取浏览器类型版本及操作系统类型(0)
- curl请求及注意事项(0)
- 二维数组排序(0)
- 文件字节大小换算KB、MB、GB、TB(0)
- 数字金额转汉字大写(0)
- 验证身份证号是否有效(0)
- 批量替换文件名(0)
- 批量修改照片文件名日期排序(0)
- PHP中文编程(0)
- PHP命令行工具-在Linux下写shell脚本(0)
- usort-使用用户自定义的比较函数对数组中的值进行排序(0)
- PHP 使用 curl 提交 json 格式数据(0)
- tp+nginx(0)
- nginx配置(0)
- PHP测试mysql数据库MYSQLI和PDO的性能比较(0)
- PHP高级扩展介绍,高手必学(0)
- 数组转XML(0)
- Memcache和Redis的区别(0)
- PHP7.0新特性(0)
- PHP中的魔术方法总结 :__construct, __destruct等..(0)
- PHP配置文件php.ini详解(0)
- PHP基础-李炎恢系列课程(20)
- 中文函数手册(0)
- 错误处理 函数(13)
- OPcache 函数(6)
- PHP 选项/信息 函数(54)
- Zip 函数(10)
- Hash 函数(15)
- OpenSSL 函数(63)
- Date/Time 函数(51)
- 目录函数(9)
- Fileinfo 函数(6)
- iconv 函数(11)
- 文件系统函数(81)
- 多字节字符串 函数(57)
- GD 和图像处理 函数(114)
- 可交换图像信息(5)
- Math 函数(50)
- 程序执行函数(11)
- PCNTL 函数(23)
- JSON 函数(4)
- SPL 函数(15)
- URL 函数(10)
- cURL 函数(32)
- 网络 函数(33)
- FTP 函数(36)
- Session 函数(23)
- PCRE 函数(11)
- PCRE 正则语法(19)
- 数组 函数(81)
- 类/对象 函数(18)
- 函数处理 函数(13)
- 变量处理 函数(37)
- SimpleXML 函数(3)
- 杂项 函数(31)
- 字符串 函数(101)
宁公网安备 64010402001209号