-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |