Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- interactive=0
- noupload=0
- function imgur {
- # imgur script by Bart Nagel <[email protected]>
- # version 2
- # I release this as public domain. Do with it what you will.
- # Required: curl
- #
- # Instructions:
- # Put it somewhere in your path and maybe rename it:
- # mv ~/Downloads/imgur.sh ~/bin/imgur
- # Make it executable:
- # chmod +x ~/bin/imgur
- # Stick your API key in the top:
- # vim ~/bin/imgur
- # Upload an image:
- # imgur images/hilarious/manfallingover.jpg
- # The URL will be displayed (and the delete page's URL will be displayed on
- # stderr). If you have xsel or xclip the URL will also be put on the X
- # selection, which you can usually paste with a middle click.
- # API Key provided by [email protected]
- apikey="b3625162d3418ac51a9ee805b1840452"
- # function to output usage instructions
- function usage {
- echo "Usage: $(basename $0) <filename>
- Upload an image to imgur and output its new URL to stdout. Its delete page is
- output to stderr.
- If xsel or xclip is available, the URL is put on the X selection for easy
- pasting." >&2
- }
- # check API key has been entered
- if [ "$apikey" = "Your API key" ]; then
- echo "You first need to edit the script and put your API key in the variable near the top." >&2
- exit 15
- fi
- # check arguments
- if [ "$1" = "-h" -o "$1" = "--help" ]; then
- usage
- exit 0
- elif [ $# -ne 1 ]; then
- if [ $# == 0 ]; then
- echo "No file specified" >&2
- else
- echo "Unexpected arguments" >&2
- fi
- usage
- exit 16
- elif [ ! -f "$1" ]; then
- echo "File \"$1\" not found" >&2
- exit 1
- fi
- # check curl is available
- which curl >/dev/null 2>/dev/null || {
- echo "Couln't find curl, which is required." >&2
- exit 17
- }
- # upload the image
- response=$(curl -F "key=$apikey" -H "Expect: " -F "image=@$1" \
- http://imgur.com/api/upload.xml 2>/dev/null)
- # the "Expect: " header is to get around a problem when using this through the
- # Squid proxy. Not sure if it's a Squid bug or what.
- if [ $? -ne 0 ]; then
- echo "Upload failed" >&2
- exit 2
- elif [ $(echo $response | grep -c "<error_msg>") -gt 0 ]; then
- echo "Error message from imgur:" >&2
- echo $response | sed -r 's/.*<error_msg>(.*)<\/error_msg>.*/\1/' >&2
- exit 3
- fi
- # parse the response and output our stuff
- url=$(echo $response | sed -r 's/.*<original_image>(.*)<\/original_image>.*/\1/')
- deleteurl=$(echo $response | sed -r 's/.*<delete_page>(.*)<\/delete_page>.*/\1/')
- if [[ "$interactive" == "1" ]]
- then
- echo
- echo Imgur link:
- echo $url
- echo
- echo Delete: $deleteurl
- echo All delete urls are saved in ~/deleteurls.txt
- else
- echo $url
- fi
- echo "$url $deleteurl" >> ~/deleteurls.txt
- }
- function creatememe (){
- if [[ "$interactive" == "1" ]]
- then
- echo Press enter to skip a caption.
- if [[ -z $top ]]; then
- echo -n Top caption:
- read top
- fi
- if [[ -z $bottom ]]; then
- echo -n Bottom caption:
- read bottom
- fi
- fi
- width=`identify -format %w meme.jpg`
- convert -background '#0008' -fill white -gravity center -size ${width}x30 \
- caption:"$bottom" \
- meme.jpg +swap -gravity south -composite out.png
- convert -background '#0008' -fill white -gravity center -size ${width}x30 \
- caption:"$top" \
- out.png +swap -gravity north -composite out.png
- if [[ "$noupload" == "1" ]]
- then
- cp out.png "$file"
- else
- imgur out.png
- fi
- rm out.png
- rm meme.jpg
- }
- function checkarg(){
- if [[ "${1:0:1}" == "-" || $1 = "" ]]; then
- cont=0;
- else
- cont=1;
- fi
- }
- function memeselect {
- if [[ "$meme" == "split" ]]
- then
- echo Please select Top Meme
- echo -n meme1 \#:
- read meme1
- echo
- echo Please select bottomMeme
- echo -n meme2 \#:
- read meme2
- convert -crop 100%x50% ./memes/$meme1.jpg "$meme1"%d.jpg
- convert -crop 100%x50% ./memes/$meme2.jpg "$meme2"%d.jpg
- #montage -mode concatenate +adjoin -tile 1x2 "$meme1"0.jpg "$meme2"1.jpg meme.jpg
- convert -append -adaptive-resize "480x480" "$meme1"0.jpg "$meme2"1.jpg meme.jpg
- rm "$meme1"0.jpg "$meme2"1.jpg "$meme1"1.jpg "$meme2"0.jpg
- else
- cp ./memes/$meme.jpg ./meme.jpg
- fi
- }
- cd `dirname $0`
- while :
- do
- case $1 in
- -h | --help)
- echo -e "-h --help display this message"
- echo -e "-s --split #1 #2 create a split meme with #1 on top and #2 on bottom"
- echo -e "-m --meme #1 #2 create a meme with #1 on top and #2 on bottom"
- echo -e "-l --list list all the avalable memes"
- echo -e "-t --top <text> top text"
- echo -e "-b --bottom <text> bottom text"
- echo -e "-i --interactive run in interactive mode"
- echo -e "-f --file <file> output to <file>, don't upload."
- echo
- echo Examples:
- echo Make a meme with top text and bottom text and upload.
- echo mememaker.sh -m 1 -t \"top text" -b \"bottom text"
- echo
- echo Make a split meme with top text and bottom text and upload.
- echo mememaker.sh -s 1 2 -t \"top text" -b \"bottom text"
- echo
- echo Make a split meme with top text and bottom text and save it as file.png.
- echo mememaker.sh -s 1 2 -t \"top text" -b \"bottom text" -f \"file.png\"
- echo
- echo Make a meme in interactive mode.
- echo mememaker.sh -i
- echo
- echo Mkae a meme in interactive mode and save it as file.png.
- echo mememaker.sh -i -f \"file.png\"
- echo
- echo Tips:
- echo imgur delete links are in ~/deleteurls.txt
- echo You don\'t need to specify both captions, you can skip one.
- exit 0
- ;;
- -l | --list)
- cat ./list.txt
- exit 0;;
- -s | --split)
- if [[ -n $2 ]];then
- convert -crop 100%x50% ./memes/$2.jpg "$2"%d.jpg
- convert -crop 100%x50% ./memes/$3.jpg "$3"%d.jpg
- convert -append -adaptive-resize "480x480" "$2"0.jpg "$3"1.jpg meme.jpg
- rm "$2"0.jpg "$2"1.jpg "$3"1.jpg "$3"0.jpg
- shift 3
- else
- echo "Check your arguments. --help for help. -i for interactive." >&2
- exit 0
- fi;;
- -m | --meme)
- if [[ -n $2 ]];then
- cp ./memes/$2.jpg ./meme.jpg
- shift 2
- else
- echo "Check your arguments. --help for help. -i for interactive." >&2
- exit 0
- fi;;
- -t | --top)
- shift
- top=$1
- shift
- checkarg
- while [[ $cont -eq 1 ]]; do
- top="$top $1"
- shift
- checkarg
- done
- cont=1;;
- -b | --bottom)
- shift
- bottom=$1
- shift
- checkarg
- while [[ $cont -eq 1 ]]; do
- bottom="$bottom $1"
- shift
- checkarg
- done
- cont=1;;
- -i | --interactive)
- if [[ -n $2 ]] && [[ "$2" != "-"* ]]
- then
- echo "Check your arguments. --help for help. -i for interactive." >&2
- exit 0
- else
- cat ./list.txt
- echo
- echo split: Create a split meme.
- echo
- echo Please select a meme.
- echo -n meme \#:
- read meme
- echo
- memeselect
- interactive=1
- shift
- fi;;
- -f | --file)
- if [[ -n $2 ]];then
- shift
- noupload=1
- file="$1"
- shift
- checkarg
- while [[ $cont -eq 1 ]]; do
- file="$file $1"
- shift
- checkarg
- done
- if [[ -e "$file" ]]
- then
- echo "Check your arguments. $file exists." >&2
- exit 0
- fi
- cont=1
- else
- echo "Check your arguments. --help for help. -i for interactive." >&2
- exit 0
- fi
- ;;
- --)
- shift
- break
- ;;
- -*)
- echo "Check your arguments. $1 is unknown." >&2
- break
- ;;
- *)
- break
- ;;
- esac
- done
- [[ -f ./meme.jpg ]] && creatememe || echo "Check your arguments. --help for help. -i for interactive."
- # vim:set ft=sh:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement