1<?php
2 /**
3  * Color icon generator
4  */
5error_reporting(0);
6if (isset($_SERVER['HTTP_REFERER'])) {
7    // eliminate external use
8    $isSameHost = strcasecmp($_SERVER['HTTP_HOST'], parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST)) === 0;
9} else {
10    // assume same host if HTTP_REFERER is not available.
11    $isSameHost = True;
12}
13$isColorSet = array_key_exists('color', $_GET);
14
15if ($isSameHost && $isColorSet) {
16
17    if (function_exists('imagecreatetruecolor')) {
18        // render PNG image using PHP GD library
19        list($red, $green, $blue) = str_split($_GET['color'], 2);
20        $img = imagecreatetruecolor(16, 16);
21        imagefill($img, 0, 0, imagecolorallocate($img, hexdec($red), hexdec($green), hexdec($blue)));
22        header('Content-type: image/png');
23        header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 60*60*24) . ' GMT');
24        imagepng($img);
25        imagedestroy($img);
26    } else {
27        // render SVG image
28        $color = '#'.strtolower($_GET['color']);
29        header('Content-type: image/svg+xml');
30        header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 60*60*24) . ' GMT');
31        echo '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16">';
32        echo '<rect width="100%" height="100%" fill="'.$color.'"/>';
33        echo '</svg>';
34    }
35}
36