用户上传的图片,一般都没有经过压缩,造成空间浪费。因此需要编写一个程序,查找内容介绍及子内容介绍的图片文件(jpg,gif,png),将大于某值的图片进行压缩处理。
代码如下:
<pre
class=”brush:xhtml;”>
#!/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
<
p>以上这篇shell 批量压缩指定内容介绍及子内容介绍内图片的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持站长源码网。
本文从互联网转载,来源地址:www.downzz.com/linux-shell/170750.html,原作者保留一切权利,若侵权或引用不当,请联系九八云(cmy.cn)删除。【九八云,优质云服务器提供商】