Skip to content

Instantly share code, notes, and snippets.

@bokwoon95
Last active July 24, 2024 10:47
Show Gist options
  • Save bokwoon95/ee940d1fb80d3605aa407a9869348005 to your computer and use it in GitHub Desktop.
Save bokwoon95/ee940d1fb80d3605aa407a9869348005 to your computer and use it in GitHub Desktop.

Revisions

  1. bokwoon95 revised this gist Jul 24, 2024. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions imgresize.sh
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ exit 1
    fi
    width="$(vipsheader "$INPUT_PATH" -f width)"
    height="$(vipsheader "$INPUT_PATH" -f height)"
    scale_factor="$(awk -v "width=$width" -v "height=$height" 'BEGIN {
    scale_factor="$(awk -v "width=$width" -v "height=$height" "BEGIN {
    aspect_ratio = width / height;
    is_tall_img = aspect_ratio < 9 / 16;
    if (is_tall_img || width > height) {
    @@ -26,11 +26,11 @@ scale_factor="$(awk -v "width=$width" -v "height=$height" 'BEGIN {
    print 1080 / height;
    }
    }
    }')"
    }")"
    if test "$scale_factor" = "1"; then
    mv "$INPUT_PATH" "$OUTPUT_PATH"
    else
    vips resize "$INPUT_PATH" "$OUTPUT_PATH" "$scale_factor"
    echo "$INPUT_PATH: width = $width, old height = $height"
    echo "$OUTPUT_PATH: width = $(vipsheader "$OUTPUT_PATH" -f width), height = $(vipsheader "$OUTPUT_PATH" -f height)"
    echo "$INPUT_PATH: input_width = $width, input_height = $height"
    echo "$OUTPUT_PATH: output_width = $(vipsheader "$OUTPUT_PATH" -f width), output_height = $(vipsheader "$OUTPUT_PATH" -f height)"
    fi
  2. bokwoon95 renamed this gist Jul 1, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. bokwoon95 created this gist Jul 1, 2024.
    36 changes: 36 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/usr/bin/env bash
    set -e
    set -u
    set -o pipefail
    INPUT_PATH="$1"
    OUTPUT_PATH="$2"
    if test ! -f "$INPUT_PATH"; then
    echo "INPUT_PATH $INPUT_PATH does not exist"
    exit 1
    fi
    width="$(vipsheader "$INPUT_PATH" -f width)"
    height="$(vipsheader "$INPUT_PATH" -f height)"
    scale_factor="$(awk -v "width=$width" -v "height=$height" 'BEGIN {
    aspect_ratio = width / height;
    is_tall_img = aspect_ratio < 9 / 16;
    if (is_tall_img || width > height) {
    if (width <= 1080) {
    print 1;
    } else {
    print 1080 / width;
    }
    } else {
    if (height <= 1080) {
    print 1;
    } else {
    print 1080 / height;
    }
    }
    }')"
    if test "$scale_factor" = "1"; then
    mv "$INPUT_PATH" "$OUTPUT_PATH"
    else
    vips resize "$INPUT_PATH" "$OUTPUT_PATH" "$scale_factor"
    echo "$INPUT_PATH: width = $width, old height = $height"
    echo "$OUTPUT_PATH: width = $(vipsheader "$OUTPUT_PATH" -f width), height = $(vipsheader "$OUTPUT_PATH" -f height)"
    fi