2021-03-22 13:19:35 +01:00
#!/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
2022-10-26 18:16:58 +02:00
pngq = "60-90"
2021-03-22 13:19:35 +01:00
# 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 "
2022-09-14 19:40:50 +02:00
/usr/bin/convert -resize $maxwidth \> " $2 " " $thumbname "
2022-10-26 18:16:58 +02:00
/usr/bin/pngquant --quality= $pngq --skip-if-larger --force --transbug " $thumbname " -o " $thumbname "
2021-03-22 13:19:35 +01:00
/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