summaryrefslogtreecommitdiff
path: root/src/lib/imagemagick.source.sh
blob: cdc909cff2fc5f543d8e68e00555be56a4e11fd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# shellcheck disable=SC2034
resolve_imagemagick_command() {
    local -r operation="$1"; shift
    local -n command_ref="$1"; shift

    command_ref=()

    case "$operation" in
        convert)
            if command -v magick >/dev/null 2>&1; then
                command_ref=(magick)
                return
            fi
            if command -v convert >/dev/null 2>&1; then
                command_ref=(convert)
                return
            fi
            ;;
        identify)
            if command -v magick >/dev/null 2>&1; then
                command_ref=(magick identify)
                return
            fi
            if command -v identify >/dev/null 2>&1; then
                command_ref=(identify)
                return
            fi
            if command -v convert >/dev/null 2>&1; then
                command_ref=(convert)
                return
            fi
            ;;
        *)
            printf 'ERROR: Unknown ImageMagick operation %s\n' \
                "$operation" >&2
            return 1
            ;;
    esac

    printf 'ERROR: ImageMagick is required; install magick or convert\n' >&2
    return 127
}

imagemagick() {
    local -a imagemagick_command=()

    resolve_imagemagick_command convert imagemagick_command || return

    run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \
        "${imagemagick_command[@]}" "$@"
}

imagemagick_identify() {
    local -a imagemagick_command=()

    resolve_imagemagick_command identify imagemagick_command || return

    if [ "${imagemagick_command[0]}" = convert ]; then
        if [[ "${1:-}" = '-verbose' && $# -eq 2 ]]; then
            run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \
                "${imagemagick_command[@]}" "$2" -verbose info:
        else
            run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \
                "${imagemagick_command[@]}" "$@" info:
        fi
        return
    fi

    run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \
        "${imagemagick_command[@]}" "$@"
}