LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

Using the imgbb.com API for Easy Command-Line Image Hosting

Tags: , , , , Date/Time: May 25, 2021 @ 1:14 am, by raindog308

ImgBB LogoLike 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.

 

I'm Andrew, techno polymath and long-time LowEndTalk community Moderator. My technical interests include all things Unix, perl, python, shell scripting, and relational database systems. I enjoy writing technical articles here on LowEndBox to help people get more out of their VPSes.

3 Comments

  1. dvader:

    The script doesn’t work if the image path has spaces in it,

    “image=@”$1″” should be “image=@$1” or “image=@${1}”

    May 26, 2021 @ 3:03 am | Reply
  2. Good catch – updated.

    May 26, 2021 @ 3:24 pm | Reply
  3. Bateman:

    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?

    June 21, 2021 @ 1:14 am | Reply

Leave a Reply

Some notes on commenting on LowEndBox:

  • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
  • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
  • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

Your email address will not be published. Required fields are marked *