scripts / thumbnails /
04ad91e 6 years ago
1 contributor
76 lines | 2.553kb
#!/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://sebmarque.hd.free.fr/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, rotation 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 \
                  ) \
           )"

for image in $(mysql -N -u $dbuser -p${dbpasswd} $dbname <<< $sqlcmd | awk '{printf("file=%s;rotation=%s\n",$1,$2*90)}'); do
    eval "$image"
    format=""
    fnNoExt="${file%.*}"
    fnExt="${file##*.}"
    fnPath="${file%/*}"
    mkdir -p $destDir/$fnPath
    cmd="convert $site/$file -write mpr:image +delete "
    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 -strip -quality 95 -interlace line -sampling-factor 4:2:2 "
            if test $rotation -ne 0; then
                cmd+="-rotate -$rotation -orient top-left "
            fi
            cmd+="-resize ${imgsize[i+1]}x${imgsize[i+2]} -write ${destDir}/${fnNoExt}-${imgsize[i]:0:2}.$fnExt +delete "
        fi
    done
    cmd=$(awk '{for (i=1; i<=NF - 3; i++) printf("%s ", $i); printf("%s", $(NF-1))}' <<< $cmd)
    if test -n "$format"; then
        echo "$site/$file : $format"
        echo -n "please wait... "
        if test -z "$2"; then
            eval "$(echo -e sudo -u www-data 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 10
        else
            echo $cmd
            echo
        fi
    fi
done