scripts / thumbnails /
467c98c 2 years ago
1 contributor
86 lines | 2.634kb
#!/bin/bash

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

declare -A imgsize width height

imgsize[xxlarge]="1656 1242"
imgsize[xlarge]=" 1224  918"
imgsize[large]="  1008  756"
imgsize[medium]=" 792   594"
imgsize[small]="  576   432"
imgsize[xsmall]=" 432   324"
imgsize[2small]=" 240   180"

if test $# -gt 0; then
    filter=$(echo $@ | sed 's/ /|/g')
    for size in ${!imgsize[@]}; do
        if ! [[ $size =~ ^(${filter})$ ]]; then
            unset imgsize[$size]
        else
            geometry=(${imgsize[$size]})
            width[$size]=${geometry[0]}
            height[$size]=${geometry[1]}
        fi
    done
fi

for size in ${!imgsize[@]}; do
    geometry=(${imgsize[$size]})
    width[$size]=${geometry[0]}
    height[$size]=${geometry[1]}
done

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 size in ${!imgsize[@]}; do
        if ! test -e "${destDir}/${fnNoExt}-${size:0:2}.$fnExt"; then
            format+="$size "
            cmd+="mpr:image -filter Lanczos -auto-orient -strip -quality 95 -interlace line -sampling-factor 4:2:2 "
            cmd+="-resize ${width[$size]}x${height[$size]} -write \"${destDir}/${fnNoExt}-${size: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 size in ${!imgsize[@]}; do
            test -s "${destDir}/${fnNoExt}-${size:0:2}.$fnExt" || echo "missing extension ${size:0:2}.$fnExt"
        done
        echo "done, sleeping..."
        sleep $(( 2 + $count ))
    fi
done