Skip to content

Instantly share code, notes, and snippets.

@andrelashley
Created February 15, 2012 23:15
Show Gist options
  • Save andrelashley/1839943 to your computer and use it in GitHub Desktop.
Save andrelashley/1839943 to your computer and use it in GitHub Desktop.
<?php
/**
* Convert color from hex in XXXXXX (eg. FFFFFF, 000000, FF0000) to array(R, G, B)
* of integers (0-255).
*
* name: rgb2array
* author: Yetty
* @param $color hex in XXXXXX (eg. FFFFFF, 000000, FF0000)
* @return string; array(R, G, B) of integers (0-255)
*/
function rgb2array($rgb) {
return array(
base_convert(substr($rgb, 0, 2), 16, 10),
base_convert(substr($rgb, 2, 2), 16, 10),
base_convert(substr($rgb, 4, 2), 16, 10),
);
}
function square($canvasSize, $fgcolor, $bgcolor, $location, $size){
// Output the header telling the browser this is a png image.
header ('Content-type: image/png');
// create the empty image.
$im = imagecreatetruecolor($canvasSize, $canvasSize) or die('Cannot Initialize new GD image stream');
$fg = rgb2array($fgcolor);
$bg = rgb2array($bgcolor);
/* Define the foreground and background colours and fill the image with the bacground colour.
The foreground colour is passed as a parameter, the background colour is white */
$fgcolor = imagecolorallocate($im, $fg[0], $fg[1], $fg[2]);
$bgcolor = imagecolorallocate($im, $bg[0], $bg[1], $bg[2]);
imagefill($im, 0, 0, $bgcolor);
// Draw a rectangle. The upper left coordinate is 0,0. The lower right is size-1, size-1 since we started counting at 0.
imagerectangle($im, $location[0], $canvasSize - $location[1] - $size, $location[0] + $size, $canvasSize - $location[1], $fgcolor);
/* Write the image to standard out - in the case of the web server to the network to the browser
and free up any memory used by the image */
imagepng($im);
imagedestroy($im);
}
square(200, "E00000", "000000", array(20, 20), 50);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment