Like many of my fellow LowendTalk readers, I sometimes post images on LET and other boards. To do that, I need an image hosting service. Going through the work of logging into a web page, uploading through their interface, etc. is tedious. Wouldn’t it be better to do it with an easy command-line script? Indeed, and it’s quite easy.
As far as services, I’ve never liked Imgur and while I do like postimages.cc, they don’t have an API. However, imgbb.com does and in about 15 minutes I had a script that allowed me to upload files and get a URL without any web browser interaction at all.
My requirements were that the script had to work on both Linux and macOS and not require any third-party modules because I didn’t want to have to keep them updated on different operating systems. There are some nice Python scripts but they require the requests module (which is great, but is third-party). Likewise while the right thing to do is parse the JSON response with something like jq, that’s a third-party dependency, so I’ll just munge it with a regex.
Here’s code showing how easy it is.
First, create an account on imgbb.com and then login with it. Then visit api.imgbb.com and click “Get API Key”.
Here’s the script I’m using, which is modified from the example on their site:
#!/bin/bash # change this to your actual API KEY API_KEY=123451234512345 RESPONSE=$(curl -s --location \ --request POST "https://api.imgbb.com/1/upload?key=${API_KEY}" \ --form "image=@$1") URL=$(echo $RESPONSE | sed 's/^.*"display_url":"//' | sed 's/",.*$//') URL=$(echo $URL | sed 's#\\/#/#g') echo $URL
All that’s needed for this script are bash, curl, and sed, which should be present on any modern Unix-like system.
Here’s the output:
$ ~/Dropbox/bin/imgbb_upload.sh ~/Desktop/leb_logo.png https://i.ibb.co/zmhDzKS/leb-logo.png
That URL is now suitable for use on any forum.
Related Posts:
- I Can’t Believe I Bought So Many VPSes on Black Friday (How to Dig Yourself Out) - December 9, 2024
- Glitch Servers’ 30% Off Winter Sale is Now Live! - December 8, 2024
- Have You Missed Any of these LowEndBoxTV Videos? - December 7, 2024
The script doesn’t work if the image path has spaces in it,
“image=@”$1″” should be “image=@$1” or “image=@${1}”
Good catch – updated.
Thanks for the script, it works but it’s resizing the image.
I uploaded 1920×1080 PNG and the URL it displays has image of 640×360. The downloaded image from URL size is 10-15% of actual PNG size.
Could you pls advise on how to get lossless / same size image URL?