50 lines
1.8 KiB
Bash
50 lines
1.8 KiB
Bash
|
#!/bin/bash
|
||
|
|
||
|
# Directory containing the images
|
||
|
input_dir="./public"
|
||
|
output_dir="./public/tmp"
|
||
|
|
||
|
rm -rf ${output_dir}
|
||
|
mkdir -p ${output_dir}
|
||
|
|
||
|
for input_file in "$input_dir"/*; do
|
||
|
if [ -f "$input_file" ]; then
|
||
|
filename=$(basename "$input_file")
|
||
|
extension="${filename##*.}"
|
||
|
output_file="${output_dir}/${filename}"
|
||
|
|
||
|
case "$extension" in
|
||
|
jpg)
|
||
|
# local max=65536
|
||
|
if [ $(stat -f%z "${input_file}") -gt 65536 ]; then
|
||
|
magick "${input_file}" -resize 30% "${output_file}"
|
||
|
while [ $(stat -f%z "$output_file") -gt 65536 ]; do
|
||
|
echo "Processing JPEG file: $output_file"
|
||
|
magick "${output_file}" -resize 30% "${output_file}"
|
||
|
done
|
||
|
fi
|
||
|
mv "${output_file}" "${input_file}"
|
||
|
;;
|
||
|
gif)
|
||
|
if [ $(stat -f%z "$input_file") -gt 1048576 ]; then
|
||
|
ffmpeg -y -i "$input_file" -vf "scale=iw*0.75:ih*0.75" "${output_file}"
|
||
|
while [ $(stat -f%z "$output_file") -gt 1048576 ]; do
|
||
|
echo "Processing GIF file: $output_file"
|
||
|
# Compress GIF files using ffmpeg
|
||
|
ffmpeg -y -i "$output_file" -vf "scale=iw*0.75:ih*0.75:flags=lanczos,split[s0][s1];[s0]palettegen=max_colors=32[p];[s1][p]paletteuse=dither=bayer" "${output_dir}/tmp-${filename}"
|
||
|
mv "${output_dir}/tmp-${filename}" "${output_file}"
|
||
|
done
|
||
|
mv "${output_file}" "${input_file}"
|
||
|
fi
|
||
|
;;
|
||
|
*)
|
||
|
echo "Skipping file: $input_file"
|
||
|
;;
|
||
|
esac
|
||
|
fi
|
||
|
done
|
||
|
|
||
|
rm -rf ${output_dir}
|
||
|
|
||
|
echo "Compression complete!"
|