shell 批量壓縮指定目錄及子目錄內(nèi)圖片的方法
更新時間:2017年04月06日 09:45:36 投稿:jingxian
下面小編就為大家?guī)硪黄猻hell 批量壓縮指定目錄及子目錄內(nèi)圖片的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
用戶上傳的圖片,一般都沒有經(jīng)過壓縮,造成空間浪費。因此需要編寫一個程序,查找目錄及子目錄的圖片文件(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 } # 執(zhí)行compress compress "$folderPath" exit 0
以上這篇shell 批量壓縮指定目錄及子目錄內(nèi)圖片的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。