2025-02-07 15:45:21 +00:00
|
|
|
#!/bin/bash
|
|
|
|
IFS="
|
|
|
|
"
|
|
|
|
#Set your parameters here
|
|
|
|
folder=/var/www/friendica
|
|
|
|
storagefolder=storage
|
|
|
|
|
2025-02-07 16:21:07 +00:00
|
|
|
loop_1() {
|
2025-02-07 15:45:21 +00:00
|
|
|
t=$(file "${p}")
|
2025-02-07 16:21:07 +00:00
|
|
|
if [[ "${t}" =~ JPEG ]]; then
|
2025-02-07 15:45:21 +00:00
|
|
|
nice -n 10 jpegoptim -m 76 "${p}" #&> /dev/null
|
2025-02-07 16:21:07 +00:00
|
|
|
elif [[ "${t}" =~ GIF ]]; then
|
2025-02-07 15:45:21 +00:00
|
|
|
nice -n 10 gifsicle --batch -O3 --lossy=80 --colors=255 "${p}" #&> /dev/null
|
|
|
|
#Specific compression for large GIF files
|
2025-02-07 16:21:07 +00:00
|
|
|
while [[ $(stat -c%s "${p}" || 0) -ge 512000 ]]; do
|
2025-02-25 11:40:48 -06:00
|
|
|
frameamount=$(($(exiftool -b -FrameCount "${p}" || 1) - 1))
|
|
|
|
nice -n 10 gifsicle "${p}" $(seq -f "#%g" 0 2 "${frameamount}") -O3 --lossy=80 --colors=255 -o "${p}" #&> /dev/null
|
2025-02-07 15:45:21 +00:00
|
|
|
done
|
2025-02-07 16:21:07 +00:00
|
|
|
elif [[ "${t}" =~ PNG ]]; then
|
2025-02-07 15:45:21 +00:00
|
|
|
nice -n 10 oxipng -o max "${p}" #&> /dev/null
|
2025-02-07 16:21:07 +00:00
|
|
|
elif [[ "${p}" =~ Web/P ]]; then
|
|
|
|
#If file is not animated
|
2025-02-10 15:15:46 +00:00
|
|
|
if [[ -f "${p}" ]]; then
|
|
|
|
if grep -v -q -e "ANIM" -e "ANMF" "${p}"; then
|
|
|
|
nice -n 10 cwebp -mt -af -quiet "${p}" -o /tmp/temp.webp #&> /dev/null
|
|
|
|
if [[ -f /tmp/temp.webp ]]; then
|
|
|
|
size_new=$(stat -c%s "/tmp/temp.webp" || 0)
|
|
|
|
size_original=$(stat -c%s "${p}")
|
|
|
|
if [[ "${size_original}" -gt "${size_new}" ]]; then
|
|
|
|
mv /tmp/temp.webp "${p}" #&> /dev/null
|
|
|
|
else
|
|
|
|
rm /tmp/temp.webp #&> /dev/null
|
|
|
|
fi
|
2025-02-07 16:21:07 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2025-02-07 15:45:21 +00:00
|
|
|
}
|
|
|
|
|
2025-02-07 16:21:07 +00:00
|
|
|
find "${folder}/${storagefolder}" -depth -mindepth 2 -type f -size +50k -not -iname "index.html" | (
|
|
|
|
while read -r p; do
|
2025-02-07 15:45:21 +00:00
|
|
|
loop_1 "${p}" &
|
2025-02-07 16:21:07 +00:00
|
|
|
until [[ $(jobs -r -p | wc -l) -lt $(($(getconf _NPROCESSORS_ONLN) / 2)) ]]; do
|
2025-02-07 15:45:21 +00:00
|
|
|
wait -n
|
|
|
|
done
|
|
|
|
done
|
|
|
|
)
|
|
|
|
wait
|