scripts/photos/prepare-for-web.sh

105 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
# global
source=$(dirname `realpath $0`)
quality=90
size=800
borderWidth=10
border=0
sign=1
# function
prepare()
{
local img="$1"
local size="$2"
local border="$3"
local sign="$4"
local quality="$5"
echo "Preparing ${img} for web"
local path=`dirname "${img}"`
local base=`basename "${img}"`
local basepng=${base/.jpg/.png}
local basejpg=${base/.png/.jpg}
local realSize=${size}
local convertOpt=""
local composeOpt=""
if [ ${border} -gt 0 ]
then
realSize=$((${size}-2*${border}))
convertOpt="-bordercolor white -border ${border}"
composeOpt="-geometry +${border}+${border}"
fi
# check the file
# Build subdir
mkdir -p "${path}/${size}"
# check if there is an embedded profile
profile=
convert -define jpeg:size=64x64 "${img}" icc:/dev/null 2> /dev/null
if [ $? -eq 0 ]
then
profile="-profile ${source}/RT_sRGB.icm +profile icc"
fi
# resize with the least quality loss as possible and remove profile data (assume sRGB)
convert "${img}" -filter Lanczos -resize ${realSize}x${realSize} -unsharp 2x0.7+0.6+.03 ${convertOpt} ${profile} "${path}/${size}/${basepng}"
# logo
if [ ${sign} -ne 0 ]
then
convert "${path}/${size}/${basepng}" ${source}/signature.png -compose src-atop -gravity SouthEast ${composeOpt} -composite "${path}/${size}/${basepng}"
fi
# compress for web
convert "${path}/${size}/${basepng}" -interlace Line -quality ${quality} "${path}/${size}/${basejpg}"
rm -f "${path}/${size}/${basepng}"
}
while getopts "bhnq:s:" flag
do
case $flag in
b)
border=${borderWidth}
if [ ${border} -gt 0 ]
then
echo "Border support enabled (${border}px)"
fi
;;
h)
echo "Usage: $0 [options] <fichier(s)>"
echo "-b : enable ${borderWidth}px white border"
echo "-n : disable signature"
echo "-q <int> : set jpg quality (default : ${quality})"
echo "-s <int> : change the output size (default : ${size})"
exit
;;
n)
sign=0
if [ $sign -eq 0 ]
then
echo "Signature disabled"
fi
;;
q)
quality=${OPTARG}
;;
s)
size=${OPTARG}
;;
esac
done
shift "$((OPTIND-1))"
echo "Output size set to ${size}"
echo "Output jpeg quality set to ${quality}"
for img in "$@"
do
if [ -f "${img}" ]
then
prepare "${img}" $size $border $sign $quality
else
echo "warning : ${img} does not exists"
fi
done