Files
piwigo_scan/piwigo_thumbgen.sh

100 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
#Author: Poul Serek
shopt -s globstar
#echo "Starting Piwigo thumbnail generation"
#Remember a trailing '/'
sourceDir="$1/"
destDir="$2/"
echo "Source: $sourceDir"
echo "Dest: $destDir"
counter=0
fnNoExt=""
fnExt=""
fnPath=""
STARTTIME=$(date +%s)
# drop CIFS cache in case target files existed before
#sync; echo 3 > /proc/sys/vm/drop_caches;
for file in "$sourceDir"/**/*.{jpg,JPG,jpeg,JPEG,png,PNG}
do
if [[ ! -f "$file" ]]
then
continue
fi
# replace all blanks and brakets
exp='[() ]'
if [[ "$file" =~ $exp ]]
then
fileNew=${file//$exp/_}
mv "${file}" "$fileNew"
file="$fileNew"
echo "Space replaced: ${file}"
fi
fnNoExt="${file%.*}"
fnExt="${file##*.}"
fnPath="${file%/*}"
fnPath="${fnPath#$sourceDir}"
fnNoExt="${fnNoExt#$sourceDir}"
#echo "${fnPath} ${fnNoExt}"
#If the medium thumbnail exists we assume that the rest also exists and skip this image
if [ ! -f "${destDir}${fnNoExt}-me.${fnExt}" ]; then
mkdir -p "${destDir}${fnPath}"
# image format specific checks to display warnings
exp='\.(png|PNG)$'
if [[ ! "$file" =~ $exp ]] ; then
#Error checking
result=$(jpeginfo -c "$file")
if [[ $result != *"[OK]"* ]]
then
echo $result
fi
fi
#auto rotate source image according to exif orientation information
orient=$(exiftool -Orientation "${file}")
if [[ $orient != *"normal"* ]]; then
quali=$(identify -format '%Q' "${file}")
if [ "$quali" -le "0" ]; then
quali=94
fi
echo "rotate with quality $quali preserving timestamp: $file"
TIMESTAMP="$( ls -l --time-style='+%Y%m%d%H%M.%S' "${file}" | cut --delimiter=' ' --fields=6 )"
mogrify -auto-orient -quality ${quali} "${file}"
touch -t ${TIMESTAMP} "${file}"
fi
#echo "MISSING! ${destDir}${fnNoExt}-me.${fnExt}"
#Store correctly oriented base image (medium) in memory. All other thumbnails are created from this
convert "${file}" -auto-orient -thumbnail 1008x756^ -write mpr:baseline +delete \
mpr:baseline -write "${destDir}${fnNoExt}-la.${fnExt}" +delete \
mpr:baseline -thumbnail 792x594 -write "${destDir}${fnNoExt}-me.${fnExt}" +delete \
mpr:baseline -thumbnail 150x9999 -write "${destDir}${fnNoExt}-cu_s150x9999.${fnExt}" +delete \
mpr:baseline -thumbnail 144x144 -write "${destDir}${fnNoExt}-th.${fnExt}" +delete \
mpr:baseline -define jpeg:size=144x144 -thumbnail 120x120^ -gravity center -extent 120x120 "${destDir}${fnNoExt}-sq.${fnExt}"
fi
counter=$[$counter +1]
if [ $(($counter%100)) -eq 0 ]; then
ENDTIME=$(date +%s)
echo "Processed: ${counter} - Executing for $((($ENDTIME - $STARTTIME)/60)) minutes"
fi
done
chown -R www-data:pi ${destDir}../
ENDTIME=$(date +%s)
echo "It took $((($ENDTIME - $STARTTIME)/60)) minutes to complete this task..."