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:
- MetWeb has a 30% Off Deal on Cheap VPS Offers in Utah for Our Readers! - December 21, 2024
- Is Your Soul as Dark as a Christmas Stocking’s Coal?Make Your Online World Match - December 20, 2024
- Hosteroid has a HOT, Limited Stock Offer in Vienna or Amsterdam! - December 19, 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?