参考链接

脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/bin/bash

if [ ! -f "$1" ]; then
echo 'Image File Error!'
exit
fi

if [ -z "$2" ]; then
echo 'Width Error!'
exit
fi

if [ -z "$3" ]; then
echo 'Height Error!'
exit
fi

image="$1"
targetWidth="$2"
targetHeight="$3"

size=( `identify -format "%w %h" "${image}"` )
origWidth=${size[0]}
origHeight=${size[1]}

if (( origWidth > targetWidth )); then
echo "origWidth > targetWidth, exit"
exit
fi

if (( origHeight > targetHeight )); then
echo "origHeight > targetHeight, exit"
exit
fi

cp "${image}" "${image}.bak"
convert "${image}" -background none -gravity center -extent ${targetWidth}x${targetHeight} "${image}"