用戶上傳的圖片,一般都沒有經過壓縮,造成空間浪費。因此需要編寫一個程序,查找目錄及子目錄的圖片文件(jpg,gif,png),將大于某值的圖片進行壓縮處理。
代碼如下:
#!/bin/bash# 查找目錄及子目錄的圖片文件(jpg,gif,png),將大于某值的圖片進行壓縮處理# ConfigfolderPath='/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}# 執行compresscompress "$folderPath"exit 0
以上這篇shell 批量壓縮指定目錄及子目錄內圖片的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答