From 46d5a318eda481f259abbca2efa9074f95cb4880 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Wed, 18 Sep 2024 20:17:38 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=BF=D1=82=D0=B8=D0=BC=D0=B8=D0=B7?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=BD=D1=8B=D0=B9=20=D1=81?= =?UTF-8?q?=D0=BA=D1=80=D0=B8=D0=BF=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- meta/files/zip_image.sh | 166 +++++++++++++++++++++++++++++++++------- 1 file changed, 138 insertions(+), 28 deletions(-) diff --git a/meta/files/zip_image.sh b/meta/files/zip_image.sh index 95ab50b7..5fe63011 100644 --- a/meta/files/zip_image.sh +++ b/meta/files/zip_image.sh @@ -1,34 +1,144 @@ #!/bin/bash -file=comp.flag -if [ -f "$file" ]; then - option="-newer $file" -fi +# Настройки +IMAGE_DIR="./images" +COMP_DIR="$IMAGE_DIR/comp" +WEBP_DIR="$IMAGE_DIR/webp" +THREADS=4 # Количество параллельных процессов -find ./images/ -type f -not -path "./images/comp/*" ! -name "*-no-comp.*" $option -iname "*.png" -exec sh -c ' - png_file="./images/comp/${1#*/images/}" - png_dir="$(dirname "$png_file")" - mkdir -p "$png_dir" - cp "$1" "$png_file" - optipng -o7 "$png_file" - advpng -z4 "$png_file" - pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP -rem sRGB -rem time -ow "$png_file" -' _ {} \; +# Экспортируем необходимые переменные и функции для использования в subshell +export IMAGE_DIR COMP_DIR WEBP_DIR -find ./images/ -type f -not -path "./images/comp/*" ! -name "*-no-comp.*" $option -iregex '.*\.\(jpg\|jpeg\)' -exec sh -c ' - jpg_file="./images/comp/${1#*/images/}" - jpg_dir="$(dirname "$jpg_file")" - mkdir -p "$jpg_dir" - cp "$1" "$jpg_file" - jpegoptim --all-progressive "$jpg_file" -' _ {} \; +# Функция для обработки PNG файлов +process_png() { + local input_file="$1" + local relative_path="${input_file#$IMAGE_DIR/}" + local output_file="$COMP_DIR/$relative_path" + local output_dir="$(dirname "$output_file")" -find ./images/comp -type f -iregex '.*\.\(jpg\|jpeg\|png\)' -not -iregex '.*no-comp\.\(jpg\|jpeg\|png\)' $option -exec sh -c ' - webp_file="./images/webp/${1#*/images/comp/}" - webp_dir="$(dirname "$webp_file")" - mkdir -p "$webp_dir" - cwebp -mt -af -progress -m 6 -q 75 -pass 10 "$1" -o "${webp_file%.*}.webp" -' _ {} \; + mkdir -p "$output_dir" -touch $file -echo "$(date)" > $file \ No newline at end of file + # Проверка хеша файла + local hash_file="${output_file}.md5" + local current_hash + current_hash="$(md5sum "$input_file" | awk '{print $1}')" + + if [ -f "$hash_file" ]; then + local previous_hash + previous_hash="$(cat "$hash_file")" + if [ "$current_hash" == "$previous_hash" ]; then + echo "PNG файл не изменился: $input_file" + return + fi + fi + + cp "$input_file" "$output_file" + + if ! optipng -o7 "$output_file"; then + echo "Ошибка при сжатии $output_file с помощью optipng" >&2 + return 1 + fi + + if ! advpng -z4 "$output_file"; then + echo "Ошибка при сжатии $output_file с помощью advpng" >&2 + return 1 + fi + + if ! pngcrush -rem gAMA -rem alla -rem cHRM -rem iCCP -rem sRGB -rem time -ow "$output_file"; then + echo "Ошибка при сжатии $output_file с помощью pngcrush" >&2 + return 1 + fi + + echo "$current_hash" > "$hash_file" +} + +# Функция для обработки JPEG файлов +process_jpeg() { + local input_file="$1" + local relative_path="${input_file#$IMAGE_DIR/}" + local output_file="$COMP_DIR/$relative_path" + local output_dir="$(dirname "$output_file")" + + mkdir -p "$output_dir" + + # Проверка хеша файла + local hash_file="${output_file}.md5" + local current_hash + current_hash="$(md5sum "$input_file" | awk '{print $1}')" + + if [ -f "$hash_file" ]; then + local previous_hash + previous_hash="$(cat "$hash_file")" + if [ "$current_hash" == "$previous_hash" ]; then + echo "JPEG файл не изменился: $input_file" + return + fi + fi + + cp "$input_file" "$output_file" + + if ! jpegoptim --all-progressive "$output_file"; then + echo "Ошибка при сжатии $output_file с помощью jpegoptim" >&2 + return 1 + fi + + echo "$current_hash" > "$hash_file" +} + +# Функция для конвертации в WebP +process_webp() { + local input_file="$1" + local relative_path="${input_file#$COMP_DIR/}" + local output_file="$WEBP_DIR/$relative_path" + output_file="${output_file%.*}.webp" + local output_dir="$(dirname "$output_file")" + + mkdir -p "$output_dir" + + # Проверка хеша файла + local hash_file="${output_file}.md5" + local current_hash + current_hash="$(md5sum "$input_file" | awk '{print $1}')" + + if [ -f "$hash_file" ]; then + local previous_hash + previous_hash="$(cat "$hash_file")" + if [ "$current_hash" == "$previous_hash" ]; then + echo "WebP файл не изменился: $input_file" + return + fi + fi + + if ! cwebp -mt -af -progress -m 6 -q 75 -pass 10 "$input_file" -o "$output_file"; then + echo "Ошибка при конвертации $input_file в WebP" >&2 + return 1 + fi + + echo "$current_hash" > "$hash_file" +} + +# Экспорт функций для использования в subshell +export -f process_png +export -f process_jpeg +export -f process_webp + +# Обработка PNG файлов +find "$IMAGE_DIR" -type f \ + -not -path "$COMP_DIR/*" \ + ! -name "*-no-comp.*" \ + -iname "*.png" -print0 | \ +xargs -0 -P "$THREADS" -I {} bash -c 'process_png "$@"' _ {} + +# Обработка JPEG файлов +find "$IMAGE_DIR" -type f \ + -not -path "$COMP_DIR/*" \ + ! -name "*-no-comp.*" \ + -iregex '.*\.\(jpg\|jpeg\)' -print0 | \ +xargs -0 -P "$THREADS" -I {} bash -c 'process_jpeg "$@"' _ {} + +# Конвертация в WebP +find "$COMP_DIR" -type f \ + -iregex '.*\.\(jpg\|jpeg\|png\)' \ + -not -iregex '.*no-comp\.\(jpg\|jpeg\|png\)' \ + -print0 | \ +xargs -0 -P "$THREADS" -I {} bash -c 'process_webp "$@"' _ {} \ No newline at end of file