Last active
January 23, 2020 14:57
-
-
Save skorotkiewicz/b4ec13c52209ee9853e8d7fa28611dc6 to your computer and use it in GitHub Desktop.
A simple and quick way to send a image to Imgur from Terminal. Required php-cli and php-curl.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
/** | |
* Quick info | |
* | |
* 1. Replace or use API Key provided by Bart. | |
* 2. # mv imgur-cli.php imgur | |
* 3. # chmod +x imgur | |
* 4. # mv imgur /usr/bin | |
* 5. Use it: imgur myCat.png | |
* | |
*/ | |
# API Key provided by Bart; | |
# replace with your own or specify yours as IMGUR_CLIENT_ID envionment variable | |
# to avoid limits | |
$client_id = 'c9a6efb3d7932fd'; | |
if ( empty($client_id) ) die('No client_id'); | |
if ( empty($argv[1]) ) die('Usage: imgur <image>'); | |
if ( filesize($argv[1]) > 5000000000 ) die('Image to big.'); | |
if ( explode('/',mime_content_type($argv[1]))[0] !== 'image' ) die('Invalid image type'); | |
$image = file_get_contents($argv[1]); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'https://api.imgur.com/3/image.json'); | |
curl_setopt($ch, CURLOPT_POST, TRUE); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Authorization: Client-ID $client_id" )); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, array( 'image' => base64_encode($image) )); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$reply = curl_exec($ch); | |
curl_close($ch); | |
$reply = json_decode($reply); | |
if ( !empty($reply->data->link) ) { | |
printf("Image sent successfully. \n\n%s\n\n", $reply->data->link); | |
printf("Delete page: https://imgur.com/delete/%s\n", $reply->data->deletehash); | |
} else { | |
echo "Upload failed."; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment