galbash 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #!/bin/bash
  2. usage() {
  3. printf "\n%s\n\nTry ${0##*/} -h\n\n" "$@"
  4. exit 1
  5. }
  6. help() {
  7. cat <<EOF
  8. ${0##*/} - Create pure HTML galleries with bash
  9. If you call this script without arguments, it will look for images in the
  10. current folder, create thumbnails & HTML files in subfolders $thumbs and $data
  11. and an index.html in the current folder.
  12. Thus, the image files are in the gallery's base folder, still easily accessible
  13. by other means, and everything else is neatly tucked away.
  14. ANNOTATIONS
  15. If there is a file called annotations in the same folder as the original images,
  16. it will be parsed to add (short) annotations to the individual images' HTML
  17. files.
  18. The format is as follows:
  19. imagefilename.ext: Some Annotation Text
  20. Meaning:
  21. At the immediate beginning of the line (no leading whitespace), the name of the
  22. file, followed by a colon ':' (again, no space), followed by a single space.
  23. Everything after that until the end of the line is the annotation.
  24. CSS
  25. The repository provides a gallery.css file, and I highly recommend you use it!
  26. The resulting HTML does not use any javascript, and the CSS is an integral part
  27. of the functionality.
  28. The script will be looking for ~/.config/galbash/gallery.css and copy it to the
  29. appropriate folder.
  30. ALL OPTIONS
  31. -s dir the source directory
  32. Default: $source
  33. -d dir the destination directory (will be created)
  34. Default: $dest
  35. -b string where the back button will take the visitor
  36. Default: $back
  37. -t int Size of thumbnails in pixels. minimum: 10
  38. Default: $thumbsize
  39. -f string the format extension for thumbnails
  40. Default: $thumbformat
  41. -F string where the favicon is
  42. -T string The title of the gallery.
  43. Default: $title
  44. -n Do not wrap from last image to first and vice versa
  45. Default: false, i.e. wrapping is enabled
  46. -N Do not create an index page with thumbnails
  47. Default: false, i.e. a thumbpage is created
  48. -k Create symbolic links instead of copying the original files.
  49. This has no effect if source and destination are the same.
  50. Default: Copy
  51. -h Display this help
  52. See also: notabug.org/ohnonot/galbash
  53. EOF
  54. exit 1
  55. }
  56. source="."
  57. dest="."
  58. back="../" # where is the back button taking us
  59. thumbsize=200
  60. thumbformat=jpg
  61. favicon=""
  62. title="Image Gallery"
  63. wrap=1 # wrap around from last image back to first and vice versa
  64. link=0 # create symlinks instead of copying files
  65. annotations=''
  66. thumbs="thumbs"
  67. data="data"
  68. css="$HOME/.config/galbash/gallery.css"
  69. thumbpage=1
  70. toindex=""
  71. while :; do
  72. case $1 in
  73. -s) if [ -d "$2" ] && [ -r "$2" ]; then
  74. source="${2%/}" # remove possible trailing slash
  75. shift 2
  76. else
  77. usage "Option $1: $2 does not seem to be a readable directory"
  78. fi
  79. ;;
  80. -d) if [[ "$2" == "-"* ]]; then
  81. usage "Option $1: $2 is invalid as destination (cannot start with \"-\")."
  82. else
  83. dest="${2%/}" # remove possible trailing slash
  84. shift 2
  85. fi
  86. ;;
  87. -b) if [[ "$2" == "-"* ]]; then
  88. usage "Option $1: $2 is invalid (cannot start with \"-\")."
  89. else
  90. back="${2%/}" # remove possible trailing slash
  91. shift 2
  92. fi
  93. ;;
  94. -t) if [[ "$2" =~ ^[1-9][0-9]+$ ]]; then
  95. thumbsize="$2"
  96. shift 2
  97. else
  98. usage "Option $1: $2 does not seem to be a positive integer, or is < 10."
  99. fi
  100. ;;
  101. -f) if [[ "$2" =~ ^[a-zA-Z]+$ ]]; then
  102. thumbformat="$2"
  103. shift 2
  104. else
  105. usage "Option $1: $2 does not seem to be an image format."
  106. fi
  107. ;;
  108. -F) if [[ "$2" == "-"* ]]; then
  109. usage "Option $1: $2 is invalid (cannot start with \"-\")."
  110. else
  111. favicon="<link rel=icon type='image/x-icon' href='$2'>"
  112. shift 2
  113. fi
  114. ;;
  115. -T) if [[ "$2" == "-"* ]]; then
  116. usage "Option $1: $2 is invalid as a title (cannot start with \"-\")."
  117. else
  118. title="$2"
  119. shift 2
  120. fi
  121. ;;
  122. -n) wrap=0
  123. shift
  124. ;;
  125. -N) thumbpage=0
  126. shift
  127. ;;
  128. -k) link=1
  129. shift
  130. ;;
  131. -h) help
  132. ;;
  133. *) break
  134. ;;
  135. esac
  136. done
  137. echo "Source directory is $source
  138. Destination is $dest"
  139. # single quotes! eval'd later.
  140. head='<!DOCTYPE html>
  141. <html lang=en>
  142. <head>
  143. <meta charset=utf-8>
  144. $favicon
  145. <title>$title</title>
  146. <link rel=stylesheet href="$css" type="text/css">
  147. <meta name=viewport content="width=device-width, initial-scale=1" />
  148. </head>
  149. <body class="$bodyclass">'
  150. foot="</body>
  151. </html>"
  152. ################################################################################
  153. mkdir -p "$dest/"{"$thumbs","$data"} || usage "Could not create directories in\n\"$dest\""
  154. [[ -r "$css" ]] && cp -f "$css" "$dest/data/gallery.css"
  155. [ -r "$source/annotations" ] && annotations="$source/annotations"
  156. amount=0
  157. imagetext=()
  158. for i in "$source"/*.* ; do
  159. type="$(file -b -L --mime-type "$i")"
  160. if [[ "$type" == "image/"* ]]; then # caveat: e.g. .xcf files are also images
  161. if [[ "$source" != "$dest" ]]; then
  162. if [[ "$link" == "1" ]]; then
  163. ln -s "$i" "$dest/"
  164. else
  165. cp "$i" "$dest/"
  166. fi
  167. fi
  168. # now we do some cleanup
  169. i="${i##*/}" # get filename without path
  170. if [[ "$annotations" != "" ]]; then
  171. imagetext[amount]="$(grep "^$i: " "$source/annotations")"
  172. imagetext[amount]="${imagetext[amount]#$i: }"
  173. fi
  174. new="${i//[^A-Za-z0-9._-]/_}"
  175. if [[ "$new" != "$i" ]]; then
  176. mv "$dest/$i" "$dest/$new" || usage "The file \"$dest/$i\"\ncould not be renamed to\n\"$dest/$new\"\nAborting."
  177. fi
  178. imgs[$((amount++))]="$new"
  179. fi
  180. done
  181. # create thumbs and fill arrays, make thumbnails
  182. for ((i=0;i<amount;i++)); do
  183. thumb[i]="${imgs[i]%.*}.$thumbformat"
  184. convert "$dest/${imgs[i]}" -gravity center -crop 1:1 \
  185. -colorspace RGB +sigmoidal-contrast 11.6933 -define filter:filter=Sinc \
  186. -define filter:window=Jinc -define filter:lobes=3 -resize "$thumbsize" \
  187. -sigmoidal-contrast 11.6933 -colorspace sRGB "$dest/$thumbs/${thumb[i]}"
  188. # the above command apparently adds some enhancement
  189. done
  190. #############################################################################
  191. # create thumbnail index page
  192. if [[ "$thumbpage" == 1 ]]; then
  193. bodyclass="thumbpage"
  194. css="$data/gallery.css"
  195. output="$dest/index.html"
  196. eval "cat <<EOF
  197. $head
  198. EOF
  199. " > "$output"
  200. cat <<EOF >> "$output"
  201. <div class="head clearfix">
  202. <span class=back><a href="$back" >«Back</a></span>
  203. <span class=title>$title</span>
  204. </div>
  205. <div class=content>
  206. EOF
  207. for ((i=0;i<amount;i++)); do
  208. cat <<EOF
  209. <div class=thumb>
  210. <a href="$data/${imgs[i]%.*}.html" >
  211. <img title="Click to View" src="$thumbs/${thumb[i]}" />
  212. </a>
  213. </div>
  214. EOF
  215. done >> "$output"
  216. echo -e "\t</div>\n$foot" >> "$output"
  217. fi
  218. #############################################################################
  219. # create individual pages for each image, with previous/next buttons
  220. bodyclass="fullpage"
  221. title_pre="$title"
  222. css="gallery.css"
  223. [[ "$thumpage" == 1 ]] && back="../" && toindex=" to Index"
  224. for ((i=0;i<amount;i++)); do
  225. output="$dest/$data/${imgs[i]%.*}.html"
  226. title="$title_pre - $((i + 1))/$amount"
  227. [[ "${imagetext[i]}" != "" ]] && text="${imagetext[i]}" || text="${imgs[i]}"
  228. eval "cat <<EOF
  229. $head
  230. EOF
  231. " > "$output"
  232. cat <<EOF >> "$output"
  233. <div class="head clearfix">
  234. <span class=back><a href="$back" >«Back$toindex</a></span>
  235. <span class=imagetext><a title="View/Download" href="../${imgs[i]}">$text</a></span>
  236. <span class=title>$title</span>
  237. </div>
  238. <div class=content>
  239. <div class="thumb previous">
  240. EOF
  241. # calculate previous thumb
  242. showthumb=0
  243. if (( i == 0 )); then
  244. if [[ "$wrap" == "1" ]]; then
  245. j=$(( amount - 1 ))
  246. showthumb=1
  247. fi
  248. else
  249. j="$((i - 1))"
  250. showthumb=1
  251. fi
  252. [[ "$showthumb" == 1 ]] && cat <<EOF >> "$output"
  253. <a href="${imgs[j]%.*}.html">
  254. <img title=Previous src="../$thumbs/${thumb[j]}" />
  255. </a>
  256. EOF
  257. # full image
  258. cat <<EOF >> "$output"
  259. </div>
  260. <div class=full>
  261. <img src="../${imgs[i]}"/>
  262. </div>
  263. <div class="thumb next">
  264. EOF
  265. # calculate next thumb
  266. showthumb=0
  267. if (( i == ( amount - 1 ) )); then
  268. if [[ "$wrap" == "1" ]]; then
  269. j=0
  270. showthumb=1
  271. fi
  272. else
  273. j="$((i + 1))"
  274. showthumb=1
  275. fi
  276. [[ "$showthumb" == 1 ]] && cat <<EOF >> "$output"
  277. <a href="${imgs[j]%.*}.html" >
  278. <img title=Next src="../$thumbs/${thumb[j]}" />
  279. </a>
  280. EOF
  281. echo -e "\t\t</div>\n\t</div>\n$foot" >> "$output"
  282. done