-
Notifications
You must be signed in to change notification settings - Fork 0
/
retamanho
executable file
·124 lines (110 loc) · 3.14 KB
/
retamanho
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/zsh
VERBOSE=0
QUIET=0
if [ "x$1" = "x-q" ]
then
shift
QUIET=1
elif [ "x$1" = "x-v" ]
then
shift
VERBOSE=1
fi
sailoop=0
arquivo="$1"
raio="$2"
if [[ $# -eq 1 ]] # Only file as argument
then
sailoop=0 # don't exit the loop
minsize=480000
maxsize=512000
elif [[ $raio -lt 20.0 ]] # Two arguments, second is is the desired ratio
then
sailoop=1 # exit the loop after changing the picture's size
else
sailoop=0
minsize=$raio # the second parameter was not the ratio, but the minimum size
raio=""
if [[ "$3" == "" ]]
then
maxsize=512000
else
maxsize=$3
fi
fi
desiredsize=$(((maxsize+minsize)/2)) # this number is integer
[ -f "$arquivo" ] || {
echo "**** Erro! Primeiro parametro \"$1\" precisa ser um arquivo legivel!" >&2
exit 1
}
gif=no
if /usr/bin/file "$arquivo" | grep -q 'GIF image data'
then
gif=yes
else
gif=no
fi
[[ $sailoop -eq 1 && "$raio" -lt 0.1 ]] && {
echo "**** Erro! Segundo parametro \"$2\" precisa ser o raio (numerico) de contracao!" >&2
exit 2
}
destino="${arquivo//./-pequeno.}"
largura=$(identify "$arquivo" | head -1 | sed 's/.* \([0-9].[0-9]*x[0-9].[0-9]*\) .*/\1/' | cut -f1 -dx)
altura=$(identify "$arquivo" | head -1 | sed 's/.* \([0-9].[0-9]*x[0-9].[0-9]*\) .*/\1/' | cut -f2 -dx)
set raioprovided=0
while [[ $sailoop -lt 2 ]]
do
if [[ $sailoop == 1 ]]
then
sailoop=2 # raio was provided.
raioprovided=1
else
if [[ "$raio" == "" ]] # Initial raio
then
inputsize=$(LC_ALL=C /usr/bin/stat "$arquivo" | awk '($1=="Size:") {print $2}')
raio=$(echo -e "scale=3\nsqrt($inputsize/$desiredsize)" | bc -l) # rough estimate, square root of pictures ratio.
fi
fi
novalargura=$((largura/raio))
novaaltura=$((altura/raio))
novalargura=${novalargura%%.*}
novaaltura=${novaaltura%.*}
intermediario=$(/bin/mktemp)
/usr/bin/convert -resize ${novalargura}x${novaaltura}\! $arquivo "$intermediario"
if [[ "$gif" = "yes" ]]
then
/usr/bin/gifsicle --colors 256 -O2 "$intermediario" > "$destino"
rm -f "$intermediario"
else
/bin/mv -f "$intermediario" "$destino"
fi
destsize=$(LC_ALL=C /usr/bin/stat "$destino" | awk '($1=="Size:") {print $2}')
if [[ $raioprovided -eq 0 ]]
then
if [[ $destsize -gt $minsize && $destsize -lt $maxsize ]]
then
sailoop=2
elif [[ $destsize -ge $maxsize || $destsize -le $minsize ]]
then
proporcao=$(echo -e "scale=3\n($destsize/$desiredsize)" | bc -l) # let's fix up our ratio.
if [[ $VERBOSE -eq 1 ]]
then
echo "*** Not yet: size ${novalargura}x${novaaltura}, ratio $raio, target size $destsize"
fi
raio=$((((raio*proporcao)+raio)/2)) # we went bigger than expected, let's make the ratio bigger (this the target picture smaller)
else
echo "*** Internal error calculating sizes. Please check script. destsize=$destsize maxsize=$maxsize minsize=$minsize"
exit 1
fi
fi
done
if [[ $VERBOSE -eq 1 ]]
then
echo "*** Final: size ${novalargura}x${novaaltura}, ratio $raio, target size $destsize"
fi
if [[ $QUIET == 1 ]]
then
echo $(ls -lh "$destino" | awk '{print $5}')
else
echo "*** $destino file size is $(ls -lh "$destino" | awk '{print $5}')."
fi