This tutorial was a request from Anandu.
Imgur provides developers to upload images anonymously using their app. For this you only need to signup for Imgur and get an Application Client ID. In this post I’m going to tell you the step by step process of uploading images using Imgur API in PHP. This is pretty easy. You only have to send a POST request to a file on their site.

Sign Up

Go to this page to sign up. It supports Social signup with Google, Facebook, Twitter & Yahoo! or you can sign up via the form.

Create Application & Get Client ID

You need to create an app to get the Client ID. For creating an application go to this page.
Select Anonymous usage without user authorization option as Authorization Type.

Fill up all the other fields including CAPTCHA. Then click submit button.
You will now get a client ID and a client Secret key. We will only need a client ID.

HTML

<form action="upload.php" enctype="multipart/form-data" method="POST">
 Choose Image : <input name="img" size="35" type="file"/><br/>
 <input type="submit" name="submit" value="Upload"/>
</form>

The above form is just like any other normal file upload form.

upload.php

<?
$img=$_FILES['img'];
if(isset($_POST['submit'])){ 
 if($img['name']==''){  
  echo "<h2>An Image Please.</h2>";
 }else{
  $filename = $img['tmp_name'];
  $client_id="88fd52d307ecceb";
  $handle = fopen($filename, "r");
  $data = fread($handle, filesize($filename));
  $pvars   = array('image' => base64_encode($data));
  $timeout = 30;
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
  curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
  curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
  $out = curl_exec($curl);
  curl_close ($curl);
  $pms = json_decode($out,true);
  $url=$pms['data']['link'];
  if($url!=""){
   echo "<h2>Uploaded Without Any Problem</h2>";
   echo "<img src='$url'/>";
  }else{
   echo "<h2>There's a Problem</h2>";
   echo $pms['data']['error'];  
  } 
 }
}
?>

Fixes

Sometimes, the code won’t work or other problems will arise. In this case, check the following :

  • Is PHP JSON extension installed ?
  • Is PHP cURL extension installed ?

If the above didn’t fix your problem, add this code just after setting the $curl variable in the code :

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

By doing this, we are disabling the SSL Certificate verification which is not good, so I recommend you do this.

Replace the content of $client_id variable with the Client ID value you got. You can get the URL of the image uploaded from $url variable.
If you have any problems / suggestions / feedback contact me through the comments.