Skip to content

Instantly share code, notes, and snippets.

@fiskurgit
Last active December 28, 2015 00:29
Show Gist options
  • Save fiskurgit/7413847 to your computer and use it in GitHub Desktop.
Save fiskurgit/7413847 to your computer and use it in GitHub Desktop.
hexcol.net php source - includes building custom coloured favicons in code.
<?php
$uri = trim($_SERVER['REQUEST_URI'], '/');
$uri_array = explode('/', $uri);
$hexcol = $uri_array[0];
function hex2rgb($hex, $isArray) {
$hex = str_replace("#", "", $hex);
if(strlen($hex) == 3) {
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
} else if (strlen($hex) == 6){
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
} else{
return "";
}
$rgb = array($r, $g, $b);
if($isArray){
return $rgb;
}else{
return implode(",", $rgb);
}
}
function createFavicon($hex) {
$colourArray = hex2rgb($hex, TRUE);
$imageOut = imagecreate(32, 32);
$colour = imagecolorallocate($imageOut, $colourArray[0], $colourArray[1], $colourArray[2]);
imagefilledrectangle($imageOut, 0, 0, 32, 32, $colour);
return $imageOut;
}
?>
<!DOCTYPE html>
<html>
<head>
<?php
if(strlen($hexcol) == 0){
echo "<title>hexcol.net</title>";
}else{
echo "<title>" . $hexcol . "</title>\n";
$image = createFavicon($hexcol);
ob_start();
imagepng($image);
$contents = ob_get_contents();
ob_end_clean();
echo "\t\t<link href=\"data:image/x-icon;base64," . base64_encode($contents) . "\" rel=\"icon\" type=\"image/x-icon\" />\n";
}
?>
<link href="./style.css" rel="stylesheet" type="text/css" media="screen">
<style>
body {
background: #<?php echo $hexcol ?>;
}
</style>
</head>
<body>
<?php
if(strlen($hexcol) == 0){
echo "<h2>hexcol.net displays hexadecimal notation colours. eg. <a href=\"http://hexcol.net/bbcc00\">hexcol.net/bbcc00</a></h2>";
}else{
if(strtolower($hexcol) === strtolower("fff") || strtolower($hexcol) === strtolower("ffffff")){
echo "<h1><span style=\"color: #888888\">#" . $hexcol . "</span></h1>\n";
echo "\t\t<h1><span style=\"color: #888888\">0x" . $hexcol . "</span></h1>\n";
$rgb = hex2rgb($hexcol, FALSE);
echo "\t\t<h1><span style=\"color: #888888\">" . $rgb . "</span></h1>\n";
}else{
echo "<h1>#" . $hexcol . "</h1>\n";
echo "\t\t<h1>0x" . $hexcol . "</h1>\n";
$rgb = hex2rgb($hexcol, FALSE);
echo "\t\t<h1>" . $rgb . "</h1>\n";
}
}
?>
<div class="footer" id="footer">Created by <a href="http://fiskur.eu">fiskur.eu</a></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment