Skip to content

Instantly share code, notes, and snippets.

@mattvh
Created October 26, 2013 03:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattvh/7165014 to your computer and use it in GitHub Desktop.
Save mattvh/7165014 to your computer and use it in GitHub Desktop.
This is the original code from my tutorial on how to use the Imgur API with Tweetbot for Mac. Imgur has since iterated their API, which requires a new script. I'm just archiving the old one here, while I update the post for version 3. http://www.webmaster-source.com/2012/10/20/adding-imgur-support-to-tweetbot-for-mac/
<?php
class TweetbotImgurEndpoint {
private $api_key;
private $api_url = "http://api.imgur.com/2/upload.json";
private $file_temp_location;
private $file_name;
function __construct($api_key, $file_temp_location, $file_name) {
$this->api_key = $api_key;
$this->file_temp_location = $file_temp_location;
$this->file_name = $file_name;
$this->upload();
}
private function upload() {
$image_info = getimagesize($this->file_temp_location);
if ($image_info !== false && in_array($image_info['mime'], array("image/png", "image/jpeg", "image/pjpeg", "image/gif"))) {
$fh = fopen($this->file_temp_location, "rb");
$file_contents = fread($fh, filesize($this->file_temp_location));
fclose($fh);
$data = array("key" => $this->api_key, "image" => base64_encode($file_contents));
$options = array('http' => array('method' => 'POST','content' => http_build_query($data)));
$context = stream_context_create($options);
$result = file_get_contents($this->api_url, false, $context);
$result = json_decode($result);
$url = $result->upload->links->original;
echo json_encode(array('url' => $url));
} else {
echo "Could not upload image: invalid format.";
}
}
}
if ($_POST) {
$imgur_api_key = 'YOUR_IMGUR_API_KEY'; //replace with your Imgur API key
$imgur = new TweetbotImgurEndpoint($imgur_api_key, $_FILES['media']['tmp_name'], $_FILES['media']['name']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment