• shell 批量压缩指定文件夹及子文件夹内图片


    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


  • 相关阅读:
    Python集合(set)类型的操作
    3GPP接口定义及相关协议一览
    OSS基本概念介绍
    建造者模式(Builder Pattern)
    组合模式(Composite Pattern)
    观察者模式(Observe Pattern)
    ReentrantLock
    <logger>和<root>
    logback的configuration
    logback的加载过程
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4353027.html
Copyright © 2020-2023  润新知