Eliminate some GitHub action warnings
[geeqie.git] / plugins / image-crop / geeqie-image-crop
1 #!/bin/sh
2
3 ## @file
4 ## @brief Crop image
5 ##
6 ## Requires ImageMagick and exiftool  
7 ## Crops the image to the size set by the Draw Rectangle menu item
8 ##
9
10 process_raw ()
11 {
12         tmpdir=$(mktemp -d "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
13
14         array_length=$(exiv2 -pp "$1" | wc -l)
15
16         if [ "$array_length" -gt 0 ]
17         then
18                 # Take last item - should be highest resolution
19                 exiv2 --location "$tmpdir" -ep"$array_length" "$1"
20
21                 src_filename=$(ls "$tmpdir/")
22                 filename="${src_filename%.*}"
23                 extension="${src_filename##*.}"
24                 rotation=$(exiftool -Orientation -n "$1" | cut -d':' -f2 | xargs)
25                 convert "$tmpdir/$src_filename" -crop "$2" "$tmpdir/$filename-crop.$extension"
26
27                 exiftool -Orientation="$rotation" -n "$tmpdir/$filename-crop.$extension"
28
29                 rm "$tmpdir/$src_filename"
30
31                 geeqie --remote --view="$tmpdir/$filename-crop.$extension"
32                 res=0
33         else
34                 res=1
35         fi
36
37         return "$res"
38 }
39
40 process_plain ()
41 {
42         tmpdir=$(mktemp -d "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
43
44         src_filename=$(basename -- "$1")
45         filename="${src_filename%.*}"
46         extension="${src_filename##*.}"
47         convert "$1" -crop "$2" "$tmpdir/$filename-crop.$extension"
48         if [ $? = 1 ]
49         then
50                 zenity --error --title="$title" --text="Cannot process this file format" --width="$width" --window-icon="$window_icon"
51         else
52                 geeqie --remote --view="$tmpdir/$filename-crop.$extension"
53         fi
54 }
55
56 export window_icon="/usr/local/share/pixmaps/geeqie.png"
57 export title="Geeqie crop image"
58 export width="250"
59
60 if [ -x "$(command -v convert)" ]
61 then
62         if [ -x "$(command -v exiftool)" ]
63         then
64
65                 coords=$(geeqie --remote --get-rectangle)
66
67                 if [ -z "$coords" ]
68                 then
69                         zenity --error --title="$title" --text="Rectangle coordinates have not been set" --width="$width" --window-icon="$window_icon" 2> /dev/null
70                         exit 0
71                 fi
72
73                 filename=$(basename -- "$1")
74                 extension=$(printf '%b' "${filename##*.}" | tr '[:upper:]' '[:lower:]')
75                 if [ "${extension}" = "jpeg" ]
76                 then
77                         process_plain "$1" "$coords"
78                 elif [ "${extension}" = "jpg" ]
79                 then
80                         process_plain "$1" "$coords"
81                 elif [ "${extension}" = "png" ]
82                 then
83                         process_plain "$1" "$coords"
84                 elif [ "${extension}" = "tif" ]
85                 then
86                         process_plain "$1" "$coords"
87                 elif [ "${extension}" = "tiff" ]
88                 then
89                         process_plain "$1" "$coords"
90                 else
91                         process_raw "$1" "$coords"
92                         if [ $? = 1 ]
93                         then
94                                 process_plain "$1" "$coords"
95                         fi
96                 fi
97         else
98                 zenity --info --title="$title" --width="$width" --height=100 --text="Crop image\n\nexiftool is not installed" --title="$title" --window-icon="$window_icon" 2> /dev/null
99                 exit 0
100         fi
101 else
102         zenity --info --title="$title" --width="$width" --height=100 --text="Crop image\n\nImageMagick is not installed" --title="$title" --window-icon="$window_icon" 2> /dev/null
103         exit 0
104 fi
105