歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> shell如何批量壓縮指定目錄及子目錄內圖片

shell如何批量壓縮指定目錄及子目錄內圖片

日期:2017/3/3 15:47:36   编辑:關於Linux

用戶上傳的圖片,一般都沒有經過壓縮,造成空間浪費。因此需要編寫一個程序,查找目錄及子目錄的圖片文件(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" "$file")  
     
        done  
     
    else  
        echo "$folderPath not exists"  
    fi  
}  
     
# 執行compress  
compress "$folderPath"  
     
exit 0

本欄目更多精彩內容:http://www.bianceng.cn/OS/Linux/

Copyright © Linux教程網 All Rights Reserved