我需要每天运行一个cron作业来移动宽度或高度超过1000px的所有图像,然后使用默认图像替换移动的图像,该图像位于运行此脚本的同一目录中.
/gwart.co.uk/mediawiki/images/too_large.jpg //file used to replace old image
我已经能够使用以下方式过滤所有图像:
find -type f -regex "^.*\.\(png\|jpg\|jpeg\)$" -printf "%p, " -exec identify -format "%w, %h\n" {} \; | awk -F ',' '$2 > 1000 || $3 > 1000'
哪个会输出
./b1/Eldar_Guardian.png, 892, 1767
./b2/Farseer_with_Guardians1.png, 894, 1308
哪个显示文件,但事实是没有丢弃awk我不知道如何实际移动这些图像,然后将原始文件名存储为too_large.jpg的cp
我的目标是为此使用Bash,但是如果有人愿意,也可以选择Php.
另一个烦人的要求是原始文件类型需要持久存在,其中也存在相关的too_large文件.
>移动高度或宽度超过1000像素的图像,无论哪个匹配.
> cp too_large [.jpg / .png / .jpg]移动图像位置
解决方法:
假设你有详细信息,那么这样的事情怎么样 –
find -type f -regex "^.*\.\(png\|jpg\|jpeg\)$" -printf "%p, " -exec identify -format "%w, %h\n" {} \; |
awk -F ',' '$2 > 1000 || $3 > 1000 {print $1}' |
while read -r file
do mv "$file" "some/other/place/$newName"
cp /gwart.co.uk/mediawiki/images/too_large.jpg "$file"
done
原始查找会垂直和水平返回文件名和像素数.
./b1/Eldar_Guardian.png, 892, 1767
./b2/Farseer_with_Guardians1.png, 894, 1308
你的awk已经选择了超大文件;我刚刚添加了一个步骤,只在找到文件名时打印文件名.
awk -F ',' '$2 > 1000 || $3 > 1000 {print $1}' | # print of X or Y >1k
所以循环只需要读取和处理文件名.