Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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
- }
- [[ $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)
- while [[ $sailoop -lt 2 ]]
- do
- if [[ $sailoop == 1 ]]
- then
- sailoop=2 # raio was provided.
- else
- if [[ "$raio" == "" ]] # Initial raio
- then
- inputsize=$(/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"
- /usr/bin/gifsicle --colors 256 -O2 "$intermediario" > "$destino"
- rm -f "$intermediario"
- destsize=$(/usr/bin/stat "$destino" | awk '($1=="Size:") {print $2}')
- 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
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement