Skip to content

Instantly share code, notes, and snippets.

@twolfson
Last active November 12, 2022 01:32
Show Gist options
  • Save twolfson/ad9084fb286e1baee969bbe5eabebe5f to your computer and use it in GitHub Desktop.
Save twolfson/ad9084fb286e1baee969bbe5eabebe5f to your computer and use it in GitHub Desktop.
Script to upload images to Imgur via curl

Last edited: Nov 15 2016

With visual testing we occasionally need to debug images in CI environments. We were previously using Imgur's v1 API:

https://github.com/twolfson/twolfson.com/blob/3.102.0/test/perceptual-tests/upload-screenshots.sh

but that's been shutdown and v2 and v3 seems like they need login for uploading images. We don't like that since these are rarely setup and we don't want to associate development content with personal content.

Thankfully Imgur supports uploading images via the browser without logging in. After trial/error, we got the following script for uploading a single image:

#!/usr/bin/env bash
# Exit on first error
set -e

# Define our filepath and filename
filename="path/to/my/file.png"
content_type="image/png"
# path/to/my/file.png -> file.png
filename="$(basename filepath)"
curl -X POST "http://imgur.com/upload" \
  -H "Referer: http://imgur.com/upload" \
  -F "Filedata=@\"$filepath\";filename=$filename;type=$content_type"

# Response: {"data":{"hashes":["1YZD9eY"],"hash":"1YZD9eY","deletehash":"XA3NaYKvzQrGnCn","album":false,"edit":false,"gallery":null,"animated":false,"height":10,"width":10,"ext":".png"},"success":true,"status":200}
# Image URL: http://imgur.com/1YZD9eY

One drawback of this approach is we don't have cookies so we cannot properly delete the image =(

We could use a curl cookie jar but it's easier said than done to transfer that out of CI

Please don't abuse this setup, we don't want it to be disabled due to abuse =/

@DurvalMenezes
Copy link

DurvalMenezes commented Aug 28, 2021

Seems outdated, there's some JS manipulation now, or I'm not woke up

Working fine for me, just replace 'http://' with 'https://' or else curl gets a redirect which apparently it doesn't follow.

EDIT: upload seems fine, shows the hash for later access, but accessing it always shows a 403 Imgur is temporarily over capacity. Please try again later. message, so apparently it's indeed broken.

@macabeus
Copy link

macabeus commented Mar 3, 2022

Updated version:

#!/bin/sh

filepath="/Users/macabeus/image.png"

image_link=$(curl -X POST "https://api.imgur.com/3/upload" \
  -F "image=@\"$filepath\"" | jq ".data.link" -r)

echo $image_link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment