scripts / thumbnails /
Sébastien MARQUE diverses améliorations
dd31aee 10 months ago
1 contributor
111 lines | 3.167kb
#!/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 distinct 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 ${IMPORT:+not} NULL
                  )
           )"

function get_ext_fn () {
    fnNoExt="${1%.*}"
    fnExt="${1##*.}"
    fnPath="${1%/*}"
}

function get_formats () {
    local IFS=$'\n'
    for file in $(mysql -N -u $dbuser -p${dbpasswd} $dbname <<< $sqlcmd); do
        get_ext_fn "$file"
        test $fnExt = 'mp4' && continue
        for size in ${!imgsize[@]}; do
            if ! test -e "${destDir}/${fnNoExt}-${size:0:2}.$fnExt" -a -z "$FORCE"; then
                formats[$file]+="$size "
                let total_ext++
            fi
        done
    done
}

function calc_left () {
    local format=(${formats[$file]})
    unset formats[$file]
    total_ext=$(( $total_ext - ${#format[@]} ))
}

declare -A formats
get_formats

if test -n "$LIST"; then
    for file in ${!formats[@]}; do
        echo "[${#formats[@]}/$total_ext] $file : ${formats[$file]}"
        calc_left
    done
else
    for file in ${!formats[@]}; do
        unset cmd sleep_duration
        get_ext_fn "$file"
        for size in ${formats[$file]}; do
            cmd+=(mpr:image -resize ${width[$size]}x${height[$size]} -write "${destDir}/${fnNoExt}-${size:0:2}.$fnExt" +delete)
            let sleep_duration++
        done
        printf "\r\033[K[${#formats[@]}/$total_ext] $file : ${formats[$file]}"
        cmd[-1]="null:"
        sudo -u www-data nice -20 convert \
            -filter Lanczos -strip -auto-orient -quality 95 -interlace line -sampling-factor 4:2:2 \
            "$site/$file" +write ${cmd[@]}
        for size in ${!imgsize[@]}; do
            test -s "${destDir}/${fnNoExt}-${size:0:2}.$fnExt" || echo -e "\nmissing extension ${size:0:2}.$fnExt"
        done
        sleep $sleep_duration
        calc_left
    done
fi