Skip to content

Instantly share code, notes, and snippets.

@atelic
Last active July 26, 2016 14:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atelic/9c7746663d91ad72af6a7205cd74a18e to your computer and use it in GitHub Desktop.
Save atelic/9c7746663d91ad72af6a7205cd74a18e to your computer and use it in GitHub Desktop.
#! /usr/bin/perl
use strict;
use warnings;
use JSON;
use LWP::UserAgent;
use MIME::Base64;
my $json = JSON->new->allow_nonref;
my $ua = LWP::UserAgent->new;
$ua->default_header('Authorization' => "Client-ID c1i3nt1D");
sub make_post {
open(IMAGE, shift) or die "$!";
my $raw_string = do{ local $/; <IMAGE>; };
my $encoded = encode_base64($raw_string);
my $resp = $ua->post("https://api.imgur.com/3/upload", [ 'image' => $encoded ]);
$json->decode($resp->decoded_content);
}
sub upload_image {
my $file = shift;
return 0 if not -e $file;
my $response = make_post($file);
if ($response->{status} == 200) {
print "$response->{data}->{link}\n"
} else {
die "Invalid request: $response";
}
}
foreach my $file (@ARGV) {
upload_image($file);
}

Installing

$ wget https://gist.githubusercontent.com/atelic/9c7746663d91ad72af6a7205cd74a18e/raw/31daf6c9d2e0e688d36931b01b1d7af869535e46/imgur.pl

Dependencies on Fedora

# dnf install perl-libwww-perl perl-JSON

API key

Change c1i3nt1D to your imgur client-id

Usage

$ ./imgur.pl myfile.png

The script will output the direct link to the image:

https://i.imgur.com/<uid>

You can give more than one file argument like:

$ ./imgur.pl file1.png file2.png fileN.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment