Skip to content

Instantly share code, notes, and snippets.

@vivien vivien/imgur
Last active Apr 1, 2019

Embed
What would you like to do?
Shell script to upload image(s) to imgur.com
#!/bin/sh
#
# Upload image(s) to imgur.com
# Copyright (C) 2014 Vivien Didelot <vivien@didelot.org>
# Licensed under GPL version 3, see http://www.gnu.org/licenses/gpl.txt
#
# Requires "jshon":
# http://kmkeen.com/jshon/
#
# Alternatives, which suck:
# http://imgur.com/tools/imgurbash.sh
# https://raw.githubusercontent.com/JonApps/imgur-screenshot/master/imgur-screenshot.sh
#
# Usage:
# imgur <file>...
# Specify <file> as '-' for standard input.
#
# Examples:
#
# * Upload several images:
# imgur foo.png bar.png
#
# * Take a screenshot and upload it:
# import png:- | imgur -
#
# * Copy the url to clipboard:
# imgur <file> | xclip
#
# * Open the result in a web browser:
# imgur <file> | xargs firefox
#
# * Quick use (no installation):
# curl -s https://gist.githubusercontent.com/vivien/9768953/raw/imgur | sh -s <file>
# API key from Alan@imgur.com, see imgurbash.sh
KEY=b3625162d3418ac51a9ee805b1840452
die () {
echo "$1" >&2
exit 1
}
# Syntax check
[ $# -lt 1 ] && die "usage: imgur <file>... (use - for stdin)"
# Upload every file given as argument
for IMG in "$@"
do
# A more verbose version of `curl -s [...] | jshon -Q -e rsp -e image -e original_image -u`
RESP="`curl -sS -F key=$KEY -H 'Expect: ' -F "image=@$IMG" http://imgur.com/api/upload.json 2>&1`"
[ $? -ne 0 ] && die "$IMG: $RESP"
URL=`echo "$RESP" | jshon -Q -e rsp -e image -e original_image -u`
[ -z "$URL" ] && die "$IMG: `echo "$RESP" | jshon -e rsp -e image -e error_msg -u`"
echo $URL
done
# vim: et ts=2 sw=2
@amitaibu

This comment has been minimized.

Copy link

commented Dec 31, 2014

Thanks for that. I'm going to use that along with PhantomCSS/ PhantomJS tests and Travis-CI

@magamig

This comment has been minimized.

Copy link

commented Nov 26, 2015

Thank you! What I was looking for 👍

@khurshid-alam

This comment has been minimized.

Copy link

commented Mar 7, 2017

Doesn't work for me. json read error: line 2 column 0: '[' or '{' expected near end of file

@egnappahz

This comment has been minimized.

Copy link

commented Apr 26, 2017

http://imgur.com/api/upload.json will explain the status of this script.

@shmup

This comment has been minimized.

Copy link

commented Jan 25, 2018

Noticed this was updated 8 days ago, so I assume it's still working out of the box? Same API key?

[~/Downloads]$ imgur lol.png
json read error: line 2 column 0: '[' or '{' expected near end of file
lol.png:

So I was curious of the response:

[~]$ KEY=b3625162d3418ac51a9ee805b1840452
[~]$ IMG=/home/jared/lol.png
[~]$ curl -sS -F key=$KEY -H 'Expect: ' -F "image=@$IMG" http://imgur.com/api/upload.json 2>&1

I did notice this is the correct upload API route now: https://api.imgur.com/3/upload.json

The one in the script is a 301.

edit: nevermind, I see even comments trigger the last active date up above. Likely this has long been broken.

@ghost

This comment has been minimized.

Copy link

commented Feb 15, 2018

Any updated version we could use?

edit: found a working version here go-gl/testutils#5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.