Skip to content

Instantly share code, notes, and snippets.

@arleslie
Created December 16, 2015 00:37
Show Gist options
  • Save arleslie/ccfcd8dbc8f63448467e to your computer and use it in GitHub Desktop.
Save arleslie/ccfcd8dbc8f63448467e to your computer and use it in GitHub Desktop.
[PHP] Grab random images from imgur
<?php
$includeNSFW = !empty($_GET['NSFW']);
if (empty($_GET['images'])) {
$_GET['images'] = 50;
}
$totalImages = intval($_GET['images']);
$processedImages = 0;
$pages = ceil($totalImages/60);
$results = [];
for ($i = 0; $i < $pages + 1; $i++) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Client-ID <imgur API>']); // CHANGE THIS LINE
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, "https://api.imgur.com/3/gallery/random/random/{$i}.json");
foreach (json_decode(curl_exec($curl))->data as $key => $result) {
if (!$result->is_album) {
if (!$result->nsfw || $result->nsfw && $includeNSFW) {
if ($processedImages >= $totalImages) {
break 2;
}
echo "<img src='{$result->link}' width='50' height='50'>";
$processedImages++;
continue;
}
}
$totalImages++;
}
if ($i >= $pages) {
$pages++;
}
curl_close($curl);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment