Skip to content

Commit

Permalink
Merge pull request null-none#1 from pkrumins/master
Browse files Browse the repository at this point in the history
add error checking. create private/public interface. improve code and readme.
  • Loading branch information
null-none committed Jul 19, 2015
2 parents 6b1d227 + 41bd337 commit 0831f09
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 40 deletions.
21 changes: 12 additions & 9 deletions README.md 100644 → 100755
@@ -1,13 +1,16 @@
# ImgurUploadPHP
Class for use imgur API.

PHP class for quickly uploading images to Imgur.

```
$imgur = new Imgur;
$imgur->clientId = 'code';
$filename = 'images/browserling.png';
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$params = array('image' => base64_encode($data));
$result = $imgur->uploadImage($params);
print_r($result);
$imgur = new ImgurUploader;
$imgur->setClientId('clientID');
$result = $imgur->uploadImage('images/browserling.png');
if (!$result) {
$error = $imgur->getError();
print "Error: $error";
}
print_r($result);
```
178 changes: 147 additions & 31 deletions index.php 100644 → 100755
@@ -1,35 +1,151 @@
<?php
class Imgur {
var $clientId;
var $timeout = 30;

function curlQuery($params, $url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $this->clientId));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
$out = curl_exec($curl);
curl_close ($curl);
return json_decode($out,true);
}

function uploadImage($params) {
$url = 'https://api.imgur.com/3/image.json';
return $this->curlQuery($params, $url);
}
}

$imgur = new Imgur;
$imgur->clientId = 'code';
$filename = 'images/browserling.png';
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$params = array('image' => base64_encode($data));
$result = $imgur->uploadImage($params);
print_r($result);

class ImgurUploader {
private $clientId;
private $error;

const TIMEOUT = 30;

public function setClientId($clientId) {
$this->clientId = $clientId;
}

public function getError () {
return $this->error;
}

public function uploadImage($filename) {
if (empty($this->clientId)) {
$this->setError("No clientID set");
return FALSE;
}

if (!file_exists($filename)) {
$this->setError("Filename '$filename' doesn't exist");
return FALSE;
}

if (!is_readable($filename)) {
$this->setError("Filename '$filename' isn't readable");
return FALSE;
}

$handle = fopen($filename, "rb");
if (!$handle) {
$this->setError("Failed opening '$filename' for reading");
return FALSE;
}

$data = file_get_contents($filename);
if (!$data) {
fclose($handle);
$this->setError("Failed reading contents of '$filename'");
return FALSE;
}
fclose($handle);

return $this->curlUpload([
'image' => base64_encode($data)
]);
}

public function uploadImageBase64($imageBase64) {
if (empty($this->clientId)) {
$this->setError("No clientID set");
return FALSE;
}

return $this->curlUpload([
'image' => $imageBase64
]);
}

private function curlUpload($params) {
$url = 'https://api.imgur.com/3/image.json';
return $this->curlQuery($params, $url);
}

private function curlQuery($params, $url) {
$curl = curl_init();
if (!$curl) {
$errstr = $this->getCurlStrError($curl);
$this->setError("Failed initializing curl: $errstr");
return FALSE;
}

$ret = curl_setopt($curl, CURLOPT_URL, $url);
if (!$ret) {
$errstr = $this->getCurlStrError($curl);
$this->setError("Failed setting CURLOPT_URL: $errstr");
return FALSE;
}

$ret = curl_setopt($curl, CURLOPT_TIMEOUT, self::TIMEOUT);
if (!$ret) {
$errstr = $this->getCurlStrError($curl);
$this->setError("Failed setting CURLOPT_TIMEOUT: $errstr");
return FALSE;
}

$ret = curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'Authorization: Client-ID ' . $this->clientId ]);
if (!$ret) {
$errstr = $this->getCurlStrError($curl);
$this->setError("Failed setting CURLOPT_HTTPHEADER: $errstr");
return FALSE;
}

$ret = curl_setopt($curl, CURLOPT_POST, 1);
if (!$ret) {
$errstr = $this->getCurlStrError($curl);
$this->setError("Failed setting CURLOPT_POST: $errstr");
return FALSE;
}

$ret = curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if (!$ret) {
$errstr = $this->getCurlStrError($curl);
$this->setError("Failed setting CURLOPT_RETURNTRANSFER: $errstr");
return FALSE;
}

$ret = curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
if (!$ret) {
$errstr = $this->getCurlStrError($curl);
$this->setError("Failed setting CURLOPT_POSTFIELDS: $errstr");
return FALSE;
}

$out = curl_exec($curl);
if (!$out) {
$errstr = $this->getCurlStrError($curl);
$this->setError("Curl session failed: $errstr");
return FALSE;
}

curl_close($curl);
return json_decode($out, true);
}

private function getCurlStrError($curl) {
$errno = curl_errno($curl);
$errstr = curl_strerror($errno);
return $errstr;
}

private function setError($error) {
$this->error = $error;
}
}

$imgur = new ImgurUploader;
$imgur->setClientId('clientID');
$result = $imgur->uploadImage('browserling.png');
if (!$result) {
$error = $imgur->getError();
print "Error: $error";
}

print_r($result);

?>

0 comments on commit 0831f09

Please sign in to comment.