Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
wiwikuan authored Aug 25, 2024
1 parent 809dc70 commit 9903fea
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions jpg-resize.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

# 檢查參數數量
if [ "$#" -ne 3 ]; then
echo "用法: $0 <輸入文件> <縮放百分比> <目標大小(kB)>"
exit 1
fi

input_file="$1"
scale_percent="$2"
target_size="$3"

# 檢查輸入文件是否存在
if [ ! -f "$input_file" ]; then
echo "錯誤: 輸入文件 '$input_file' 不存在"
exit 1
fi

# 生成輸出文件名(始終使用 .jpg 擴展名)
filename=$(basename -- "$input_file")
filename="${filename%.*}"
output_file="${filename}_resized.jpg"

# 執行轉換,強制輸出為 JPG 格式
convert "$input_file" -resize "${scale_percent}%" -define jpeg:extent="${target_size}kb" "$output_file"

# 檢查轉換是否成功
if [ $? -eq 0 ]; then
echo "轉換成功: 輸出文件為 $output_file"
else
echo "錯誤: 轉換失敗"
exit 1
fi

0 comments on commit 9903fea

Please sign in to comment.