[SCRIPT] Add thumbnail generator
This commit is contained in:
parent
224ead80db
commit
aecc5f25ea
1 changed files with 83 additions and 0 deletions
83
.scripts/generate_tumbnails.sh
Executable file
83
.scripts/generate_tumbnails.sh
Executable file
|
@ -0,0 +1,83 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
if [ "$#" -ne 2 ]
|
||||||
|
then
|
||||||
|
maxwidth=800
|
||||||
|
else
|
||||||
|
maxwidth=$2
|
||||||
|
fi
|
||||||
|
|
||||||
|
check_replace(){
|
||||||
|
# Si le thumbnail généré est plus gros que l'original (ça arrive régulièrement surtout avec les png) on garde l'original et on le copie comme si c'était le thumbnail.
|
||||||
|
size_orig=$(stat -c %s $1)
|
||||||
|
size_thumb=$(stat -c %s $2)
|
||||||
|
if [ "$size_orig" -lt "$size_thumb" ]; then
|
||||||
|
rm "$2"
|
||||||
|
cp "$1" "$2"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
thumbnail(){
|
||||||
|
echo -en "Processing image \e[0;34m$2\e[0m :"
|
||||||
|
extension="$(echo $2 | awk -F\. '{print $NF}')"
|
||||||
|
thumbname="$(dirname $2)/$(basename -s .$extension $2).thumb.$extension"
|
||||||
|
thumbwebpname="$(dirname $2)/$(basename -s .$extension $2).thumb.webp"
|
||||||
|
thumbavifname="$(dirname $2)/$(basename -s .$extension $2).thumb.avif"
|
||||||
|
|
||||||
|
maxwidth=$1
|
||||||
|
jpegq=75
|
||||||
|
webpq=55
|
||||||
|
cavifq=35
|
||||||
|
|
||||||
|
# Si une image est un thumbnail on la considère comme déjà bien traitée.
|
||||||
|
if [[ $2 == *".thumb."* ]]; then
|
||||||
|
echo -e " \e[0;31mest un thumb !\e[0m"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Si une image a déjà un thumbnail, on la considère comme déjà traitée et donc on y retouche pas.
|
||||||
|
if [ -f "$(dirname $2)/$(basename -s .$extension $2).thumb.$extension" ]; then
|
||||||
|
echo -e " \e[0;31ma déjà un thumb !\e[0m"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
case "$extension" in
|
||||||
|
jpg | jpeg | JPG | JPEG )
|
||||||
|
/usr/bin/convert -resize $maxwidth\> -quality 100 "$2" "$thumbname.LOSSLESS"
|
||||||
|
/usr/bin/convert -quality $jpegq "$thumbname.LOSSLESS" "$thumbname"
|
||||||
|
/usr/bin/convert -quality $webpq "$thumbname.LOSSLESS" "$thumbwebpname"
|
||||||
|
/usr/bin/cavif --quiet --quality $cavifq "$thumbname.LOSSLESS" --output "$thumbavifname"
|
||||||
|
rm "$thumbname.LOSSLESS"
|
||||||
|
jpegoptim --quiet --strip-all "$2"
|
||||||
|
check_replace "$2" "$thumbname"
|
||||||
|
;;
|
||||||
|
png | PNG)
|
||||||
|
# Ce con de pngcrush a tendance à faire n'importe quoi si on lui dit de réécrire par dessus les images, du coup on passe par un fichier temporaire qu'on renomme par la suite.
|
||||||
|
pngcrush -warn "$2" "$2.tmp"
|
||||||
|
mv -f "$2.tmp" "$2"
|
||||||
|
usr/bin/convert -resize $maxwidth\> "$2" "$thumbname"
|
||||||
|
/usr/bin/convert -resize $maxwidth\> -quality $webpq "$2" "$thumbwebpname"
|
||||||
|
pngcrush -warn "$thumbname" "$thumbname.tmp"
|
||||||
|
mv -f "$thumbname.tmp" "$thumbname"
|
||||||
|
check_replace "$2" "$thumbname"
|
||||||
|
|
||||||
|
/usr/bin/convert -resize $maxwidth\> -quality 100 "$2" "$thumbname.LOSSLESS"
|
||||||
|
/usr/bin/cavif --quiet --quality $cavifq "$thumbname.LOSSLESS" --output "$thumbavifname"
|
||||||
|
rm "$thumbname.LOSSLESS"
|
||||||
|
|
||||||
|
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
sizeorig=$(stat -c %s $2 | numfmt --to=iec --padding=3)
|
||||||
|
sizethumb=$(stat -c %s $thumbname | numfmt --to=iec --padding=3)
|
||||||
|
sizewebp=$(stat -c %s $thumbwebpname | numfmt --to=iec --padding=3)
|
||||||
|
sizeavif=$(stat -c %s $thumbavifname | numfmt --to=iec --padding=3)
|
||||||
|
echo -e "\e[0;32mOK\e[0;m : orig : $sizeorig > $sizethumb ; webp:$sizewebp ; avif:$sizeavif"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export -f thumbnail
|
||||||
|
export -f check_replace
|
||||||
|
|
||||||
|
find $1 -name '*.jpg' -or -name '*.jpeg' -or -name '*.JPG' -or -name '*.JPEG' -or -name '*.png' -or -name '*.PNG' | parallel --jobs 16 thumbnail $maxwidth
|
||||||
|
|
Loading…
Reference in a new issue