This script generates a PNG image that shows the total download count of a GitHub repository. It also includes caching to reduce API calls and optimizes performance.
I implemented this because existing shield options wasnt showing the total release when you have too many and old releases it doestn give real total count. But this csripts gives the total count and you can increase the “curl_setopt($ch, CURLOPT_URL, “$apiUrl?per_page=200&page=$page”);” if you have very very very big number of releases.
Implementation simple just put this php file on your server. It only needs curl, gd lib and json ext thats it all servers have this addons usualy.
After that you can get your png download count banner like this “https://sinanisler.com/tool/git-banner/download_count.php/download_count.png” easy…
GITHUB BUG FIX: I was hoping this code will solve the bug but github resisting. But there is solution.If you have too many pages and releases total download count just add your old release downloads manualy you can see it on Insights tab and possible to add manualy to $totalDownloads = 0; variable and then realtime download count will be added top of that manual download count.
Enjoy.
<?php // Configuration $cacheFile = __DIR__ . '/cache.json'; $cacheDuration = 1200; $owner = 'username'; $repo = 'reponame'; $apiUrl = "https://api.github.com/repos/$owner/$repo/releases"; // Check cache if (file_exists($cacheFile)) { $cache = json_decode(file_get_contents($cacheFile), true); if (time() - $cache['timestamp'] < $cacheDuration) { $totalDownloads = $cache['totalDownloads']; } else { $totalDownloads = fetchAndCacheDownloadCount($apiUrl, $cacheFile); } } else { $totalDownloads = fetchAndCacheDownloadCount($apiUrl, $cacheFile); } // Generate PNG header('Content-Type: image/png'); if (!isset($_GET['force']) && isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '.png') !== false) { header('Content-Disposition: inline; filename="download_count.png"'); } $imageWidth = 200; $imageHeight = 40; $image = imagecreate($imageWidth, $imageHeight); $backgroundColor = imagecolorallocate($image, 34, 34, 34); // Dark background $textColor = imagecolorallocate($image, 255, 255, 255); // White text $fontPath = __DIR__ . '/arial.ttf'; // Ensure you have an Arial or similar TTF font in this path $text = "Download Count: $totalDownloads"; // Add text to the image if (file_exists($fontPath)) { imagettftext($image, 14, 0, 10, 30, $textColor, $fontPath, $text); } else { imagestring($image, 5, 10, 15, $text, $textColor); } imagepng($image); imagedestroy($image); /** * Fetches download count and caches it. * * @param string $apiUrl * @param string $cacheFile * @return int */ function fetchAndCacheDownloadCount($apiUrl, $cacheFile) { $totalDownloads = 0; $page = 1; do { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "$apiUrl?per_page=100&page=$page"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'User-Agent: PHP-Script' ]); $response = curl_exec($ch); curl_close($ch); if ($response === false) { break; } $data = json_decode($response, true); if (empty($data)) { break; } foreach ($data as $release) { foreach ($release['assets'] as $asset) { $totalDownloads += $asset['download_count']; } } $page++; } while (count($data) === 100); // Cache the result file_put_contents($cacheFile, json_encode([ 'timestamp' => time(), 'totalDownloads' => $totalDownloads ])); return $totalDownloads; }