Return to Snippet

Revision: 65616
at January 1, 2014 06:03 by wyattstorch42


Initial Code
<?php
// Enable robust error reporting to catch errors
error_reporting(E_ALL);

// Generate an image if the form has been submitted
if (!empty($_GET)) {
	// $side is the width and height of the resulting image
	$side = intval($_GET['side']);
	// $color is a hexadecimal color (000000 to FFFFFF)
	$color = $_GET['color'];
	// If the $color begins with a # sign, strip it off
	$color = substr($color, 0, 1) == '#' ? substr($color, 1) : $color;
	// Split the color into the hex value for red, green, and blue
	list($r, $g, $b) = str_split($color, 2);
	// Convert the hexadecimal strings to decimal numbers (00=00, FF=255)
	$r = hexdec($r);
	$g = hexdec($g);
	$b = hexdec($b);

	// Create the canvas
	$image = imagecreatetruecolor($side, $side);
	// Preserve transparency
	imagesavealpha($image, true);
	// Fill it with a transparent background
	imagefill($image, 0, 0, imagecolorallocatealpha($image, 0, 0, 0, 127));

	// Allocate the bar color
	$color = imagecolorallocate($image, $r, $g, $b);

	// Draw 3 rectangles on the canvas from (x1, x2) to (y1, y2) and fill
	// them with the bar color
	$x1 = $side/7;
	$x2 = $side-$x1;
	for ($y1 = $x1; $y1 < $x2; $y1 += $x1*2) {
		imagefilledrectangle($image, $x1, $y1, $x2-1, $y1+$x1-1, $color);
	}

	// Output the PNG if no errors were printed during this process
	if (!headers_sent()) {
		header('Content-Type: image/png');
		imagepng($image);
		imagedestroy($image);
	}
	// Terminate
	exit;
}
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>Hamburger Icon Generator</title>
	</head>
	<body>
		<h1>Hamburger Icon Generator</h1>
		<form method="get" target="_blank">
			<p><label>Size <input type="number" name="side" min="7" max="700" step="7" value="49" onchange="document.getElementById('side').innerHTML = this.value;">&times;<span id="side">49</span> (must be a multiple of 7)</label></p>
			<p><label>Bar Color #<input type="text" name="color" value="000000"> (hex values only)</label></p>
			<p><input type="submit" value="Generate"></p>
		</form>
	</body>
</html>

Initial URL

                                

Initial Description
Save this script on your localhost and run it whenever you need to generate a "hamburger icon" of a specific size and color. Just tell it the dimensions and bar color and it will output a mathematically perfect hamburger icon as a transparent PNG, which you can just download.

Initial Title
PHP Hamburger Generator

Initial Tags
php

Initial Language
PHP