Create a gist now

Instantly share code, notes, and snippets.

anonymous /teachat-imgur.sh Secret
Created Sep 15, 2017

What would you like to do?
upload images along with auto-generated thumbnails to imgur and generate bbcode
#!/usr/bin/env bash
## resize and upload
set -eu
## if $1 is unset
if [ -z ${1+x} ]; then
echo "no argument given"
exit 1
fi
# check if $img_file is a URL
url_regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
if [[ $1 =~ $url_regex ]]; then
tmpfile='/tmp/teachat-imgur.jpg'
curl -o "$tmpfile" "$1"
imgpath="$tmpfile"
else
imgpath="$1"
fi
shrunkimgpath="/tmp/teachat-imgur-shrunk.jpg"
# teachat allows [img] to be max 640x1000
# resize so that width is at most 600px and height is at most 950px
convert "$imgpath" -resize 600x950\> "$shrunkimgpath"
imgout=$(imgur-screenshot.sh "$imgpath")
imgshrunkout=$(imgur-screenshot.sh "$shrunkimgpath")
echo 'orig image:'
echo '----------'
echo "$imgout"
echo '----------'
echo 'shrunk image:'
echo '----------'
echo "$imgshrunkout"
echo '----------'
imgurl=$(echo "$imgout" | pcregrep -o1 'image link: (https?.+)')
imgshrunkurl=$(echo "$imgshrunkout" | pcregrep -o1 'image link: https?://(.+)')
# teachat needs [img] to be http and not https
imgshrunkurl="http://$imgshrunkurl"
teachatstr="[url=$imgurl][img]$imgshrunkurl[/img][/url]"
echo "$teachatstr"
echo -n "$teachatstr" | pbcopy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment