当前位置: 移动技术网 > 网络运营>服务器>Linux > shell 批量压缩指定目录及子目录内图片的方法

shell 批量压缩指定目录及子目录内图片的方法

2017年12月08日  | 移动技术网网络运营  | 我要评论

用户上传的图片,一般都没有经过压缩,造成空间浪费。因此需要编写一个程序,查找目录及子目录的图片文件(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 批量压缩指定目录及子目录内图片的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网