scripts / thumbnails /
Sébastien MARQUE multiples améliorations
5026b47 5 years ago
1 contributor
70 lines | 2.299kb
#!/bin/bash

thumbnailsconf=${1:-${0##*/}.conf}
test -r "$thumbnailsconf" || exit 1

function set_imgsize () {
    imgsize[${#imgsize[@]}]=$1
    imgsize[${#imgsize[@]}]=$2
    imgsize[${#imgsize[@]}]=$3
}

#set_imgsize 'custom' 1656 1242
set_imgsize 'xxlarge' 1224 918
set_imgsize 'xlarge' 1008 756
set_imgsize 'large' 792 594
set_imgsize 'medium' 576 432
set_imgsize 'small' 432 324
set_imgsize 'xsmall' 240 240

if which readconf > /dev/null; then
    eval $(readconf --conf "$thumbnailsconf" --section=mysql --section=path --case-sensitive)
else
    echo readconf needed !
    echo PATH = $PATH
    echo get readconf at https://seb.lautre.net/git/seb/scripts/raw/master/readconf
    exit 1
fi

if ! which convert > /dev/null; then
    echo please install ImageMagick
    exit 1
fi

sqlcmd="select path from ${dbprefix}images \
           where id in ( \
              select distinct image_id from ${dbprefix}image_category \
                  where category_id in ( \
                      select id from ${dbprefix}categories where dir is NULL \
                  ) \
           )"

IFS=$'\n'
for file in $(mysql -N -u $dbuser -p${dbpasswd} $dbname <<< $sqlcmd); do
    format=""
    fnNoExt="${file%.*}"
    fnExt="${file##*.}"
    fnPath="${file%/*}"
    test $fnExt = 'mp4' && continue
    cmd="sudo -u www-data convert \"$site/$file\" -write mpr:image +delete "
    count=0
    for ((i=0; i<${#imgsize[@]}; i+=3)); do
        if ! test -e "${destDir}/${fnNoExt}-${imgsize[i]:0:2}.$fnExt"; then
            format+="${imgsize[i]} "
            cmd+="mpr:image -filter Lanczos -auto-orient -strip -quality 95 -interlace line -sampling-factor 4:2:2 "
            cmd+="-resize ${imgsize[i+1]}x${imgsize[i+2]} -write \"${destDir}/${fnNoExt}-${imgsize[i]:0:2}.$fnExt\" +delete "
            let count++
        fi
    done
    if test -n "$format"; then
        echo "$site/$file : $format"
        echo -n "please wait... "
        cmd=$(sed -r 's/^(.+) -write (.*) \+delete $/\1 \2/' <<< $cmd)
        eval $(echo -e nice -20 $cmd)
        for ((i=0; i<${#imgsize[@]}; i+=3)); do
            test -s "${destDir}/${fnNoExt}-${imgsize[i]:0:2}.$fnExt" || echo "missing extension ${imgsize[i]:0:2}.$fnExt"
        done
        echo "done, sleeping..."
        sleep $(( 2 + $count ))
    fi
done