CI png optimized check

This commit is contained in:
cx384 2024-12-31 16:27:10 +01:00
parent 1427a98c59
commit 5aeaf20849
2 changed files with 57 additions and 0 deletions

26
.github/workflows/png_file_checks.yml vendored Normal file
View file

@ -0,0 +1,26 @@
name: png_file_checks
# Check whether all png files are in a valid format
on:
push:
paths:
- '**.png'
- '.github/workflows/**.yml'
pull_request:
paths:
- '**.png'
- '.github/workflows/**.yml'
jobs:
png_optimized:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install deps
run: |
sudo apt-get update
sudo apt install -y optipng
- name: Check whether all png files are optimized
run: |
./util/ci/check_png_optimized.sh

31
util/ci/check_png_optimized.sh Executable file
View file

@ -0,0 +1,31 @@
#!/bin/bash -e
# Only warn if decrease is more than 3%
optimization_requirement=3
git ls-files "*.png" | sort -u | (
optimized=1
temp_file=$(mktemp)
echo "Optimizing png files:"
while read file; do
# Does only run a fuzzy check without -o7 -zm1-9 since it would be too slow otherwise
decrease=($(optipng -nc -strip all -out "$temp_file" -clobber "$file" |& \
sed -n 's/.*(\([0-9]\{1,\}\) bytes\? = \([0-9]\{1,\}\)\.[0-9]\{2\}% decrease).*/\1 \2/p'))
if [[ -n "${decrease[*]}" ]]; then
if [ "${decrease[1]}" -ge "$optimization_requirement" ]; then
echo -en "\033[31m"
optimized=0
else
echo -en "\033[32m"
fi
echo -e "Decrease: ${decrease[0]}B ${decrease[1]}%\033[0m $file"
fi
done
rm "$temp_file"
if [ "$optimized" -eq 0 ]; then
echo -e "\033[1;31mWarning: Could optimized png file(s) by more than $optimization_requirement%.\033[0m" \
"Apply 'optipng -o7 -zm1-9 -nc -strip all -clobber <filename>'"
exit 1
fi
)