您现在的位置是:网站首页> 编程资料编程资料
shell 批量压缩指定目录及子目录内图片的方法_linux shell_
2023-05-26
408人已围观
简介 shell 批量压缩指定目录及子目录内图片的方法_linux shell_
用户上传的图片,一般都没有经过压缩,造成空间浪费。因此需要编写一个程序,查找目录及子目录的图片文件(jpg,gif,png),将大于某值的图片进行压缩处理。
代码如下:
#!/bin/bash # 查找目录及子目录的图片文件(jpg,gif,png),将大于某值的图片进行压缩处理 # Config folderPath='/home/fdipzone/photo' # 图片目录路径 maxSize='1M' # 图片尺寸允许值 maxWidth=1280 # 图片最大宽度 maxHeight=1280 # 图片最大高度 quality=85 # 图片质量 # 压缩处理 # Param $folderPath 图片目录 function compress(){ folderPath=$1 if [ -d "$folderPath" ]; then for file in $(find "$folderPath" \( -name "*.jpg" -or -name "*.gif" -or -name "*.png" \) -type f -size +"$maxSize" ); do echo $file # 调用imagemagick resize图片 $(convert -resize "$maxWidth"x"$maxHeight" "$file" -quality "$quality" -colorspace sRGB "$file") done else echo "$folderPath not exists" fi } # 执行compress compress "$folderPath" exit 0 以上这篇shell 批量压缩指定目录及子目录内图片的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
相关内容
- 完美解决mac环境使用sed修改文件出错的问题_linux shell_
- shell 使用数组作为函数参数的方法(详解)_linux shell_
- linux 中open()函数详解及简单实例_linux shell_
- Linux 中C语言getcwd()函数的用法_linux shell_
- linux crontab 实现每秒执行的实例_linux shell_
- crontab每10秒执行一次的实现方法_linux shell_
- 基于shell的if和else详解_linux shell_
- Vim中列出TODO与FIXME等备注的方法_linux shell_
- Linux bash删除文件中含“指定内容”的行功能示例_linux shell_
- 使用ntpdate工具校正linux服务器时间(实现方法)_linux shell_
