aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin/image/shellrc
blob: fbb2c20c4be7bcbb4bf7069c14b7ab1198b3c651 (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
# Image Utilities

# Concatenate image vertically
image.vertical() {
  ext=${1##*.}
  convert "$@" -append $(basename -s .$ext $1)-$(basename -s .$ext ${@: -1}).${format:-$ext}
}

# Concatenate image horizontally
image.horizontal() {
  ext=${1##*.}
  convert "$@" +append output.$ext
}

# Export image with data url format
image.from_data_url() {
  [ -z "$1" ] && echo File name needed && return 1
  xsel -ob | sed -E 's/^.+,//' | base64 -d >$1
  identify $1
}

# Resize image
image.resize() {
  ext=${1##*.}
  convert -resize $2 $1 output.$ext
}

# Upload image to vps
# Usage:
#   image.upload foo.png
image.upload() {
  local month=$(date +%Y-%m)
  local filename=${2:-$(date +"%d_%Hh%Mm%Ss").${1##*.}}
  local fpath='$HOME/data/s3.photos/'$month
  cat $1 | ssh vps "mkdir -p $fpath && cat >$fpath/$filename" && \
  echo https://topo.tw/photos/$month/$filename || \
  echo Fail to upload
}

# Create an image contains text
image.text() {
  convert \
    -size 230x130 \
    -background lightblue \
    -font Noto-Sans-Bold \
    -pointsize 25 \
    -fill black \
    -gravity Center \
    caption:"${1:=foo}" \
    -flatten \
    "${2:=foo}".jpg
}