Skip to content

Instantly share code, notes, and snippets.

@tomoe-mami
Created September 8, 2012 07:38
Show Gist options
  • Save tomoe-mami/3672594 to your computer and use it in GitHub Desktop.
Save tomoe-mami/3672594 to your computer and use it in GitHub Desktop.
php imgur uploader
#!/usr/bin/env php
<?php
define('IMGUR_SCRIPT_VERSION', '0.1');
define('IMGUR_API_BASE_URL', 'http://api.imgur.com');
define('IMGUR_API_VERSION', 2);
define('IMGUR_CURL_TIMEOUT', 3000);
$api_key = getenv('API_KEY');
if (!empty($api_key))
define('IMGUR_API_KEY', $api_key);
else
define('IMGUR_API_KEY', 'YOUR_API_KEY_HERE');
function imgur_build_url($action, array $params = null)
{
$url = sprintf('%s/%u/%s.json',
IMGUR_API_BASE_URL,
IMGUR_API_VERSION,
$action);
if (!empty($params))
{
$query = http_build_query($params);
if (strlen($query)) $url .= '?' . $query;
}
return $url;
}
function imgur_shutdown($curlh)
{
if (is_resource($curlh) && get_resource_type($curlh) == 'curl')
curl_close($curlh);
}
function imgur_curl_exec($curlh)
{
$response = curl_exec($curlh);
$errno = curl_errno($curlh);
if ($errno)
{
fprintf(STDERR, "cURL Error: %s\n", curl_error($curlh));
exit($errno);
}
return $response;
}
function imgur_show_help()
{
echo <<<EOF
Upload image to Imgur.
Usage: imgur [option]|[filename]
Option:
-c, --credit Show credit info
-h, --help Show this help
-v, --version Show script version
If filename is not specified, STDIN will be used.
EOF;
}
function imgur_show_version()
{
echo IMGUR_SCRIPT_VERSION . "\n";
}
function imgur_show_credit_info($curlh)
{
curl_setopt(
$curlh,
CURLOPT_URL,
imgur_build_url('credits', array('key' => IMGUR_API_KEY))
);
curl_setopt($curlh, CURLOPT_RETURNTRANSFER, 1);
$response = imgur_curl_exec($curlh);
if (strlen($response))
{
$info = json_decode($response);
if (is_object($info))
{
if (!empty($info->error))
{
fprintf(STDERR, "Error: %s\n", $info->error->message);
exit(102);
}
elseif (!empty($info->credits))
{
printf(
"Available credit: %d\n" .
"Allocation limit: %d\n" .
"Next reset: %s\n" .
"Refresh time: %d secs\n",
$info->credits->remaining,
$info->credits->limit,
gmdate('j F Y H:i:s P', $info->credits->reset),
$info->credits->refresh_in_secs);
return true;
}
else
{
fprintf(STDERR, "Error: Unknown response: %s\n", $response);
exit(103);
}
}
else
{
fprintf(STDERR, "Unable to parse response: %s\n", $response);
exit(104);
}
}
else
{
fwrite(STDERR, "No response from Imgur\n");
exit(101);
}
}
function imgur_upload($curlh, $filename = null)
{
if (strlen($filename) < 1)
{
$image_data = file_get_contents('php://stdin');
$filename = "STDIN";
}
else
{
if (!is_file($filename))
{
fprintf(STDERR, "No such file: %s\n", $filename);
return false;
}
$image_data = file_get_contents($filename);
}
if (strlen($image_data) < 1)
{
fprintf(STDERR, "Empty file: %s\n", $filename);
return false;
}
curl_setopt($curlh, CURLOPT_URL, imgur_build_url('upload'));
curl_setopt($curlh, CURLOPT_POST, 1);
curl_setopt($curlh, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(
$curlh,
CURLOPT_POSTFIELDS,
array(
'key' => IMGUR_API_KEY,
'name' => $filename,
'image' => base64_encode($image_data)
));
if (IMGUR_CURL_TIMEOUT > 0)
curl_setopt($curlh, CURLOPT_TIMEOUT, IMGUR_CURL_TIMEOUT);
$response = imgur_curl_exec($curlh);
if (strlen($response))
{
$info = json_decode($response);
if (is_object($info))
{
if (!empty($info->error))
{
fprintf(STDERR, "Error: %s\n", $info->error->message);
return false;
}
elseif (!empty($info->upload))
{
printf(
"%s (%ux%u %s)\n" .
"\tPage: %s\n" .
"\tOriginal Size: %s\n" .
"\tSmall Thumbnail: %s\n" .
"\tLarge Thumbnail: %s\n" .
"\tDelete Page: %s\n",
$info->upload->image->name,
$info->upload->image->width,
$info->upload->image->height,
$info->upload->image->type,
$info->upload->links->imgur_page,
$info->upload->links->original,
$info->upload->links->small_square,
$info->upload->links->large_thumbnail,
$info->upload->links->delete_page
);
return true;
}
else
{
fprintf(STDERR, "Error: Unknown response: %s\n", $response);
return false;
}
}
else
{
fprintf(STDERR, "Unable to parse response: %s\n", $response);
return false;
}
}
else
{
fwrite(STDERR, "No response from Imgur\n");
return false;
}
}
$curlh = curl_init();
register_shutdown_function('imgur_shutdown', $curlh);
if ($argc > 1)
{
$files = array();
array_shift($argv);
foreach ($argv as $arg)
{
if ($arg == '-c' || $arg == '--credit')
{
imgur_show_credit_info($curlh);
exit(0);
}
elseif ($arg == '-h' || $arg == '--help')
{
imgur_show_help();
exit(0);
}
elseif ($arg == '-v' || $arg == '--version')
{
imgur_show_version();
exit(0);
}
else imgur_upload($curlh, $arg);
}
}
else imgur_upload($curlh); // read image from stdin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment