Skip to content

Instantly share code, notes, and snippets.

@andrelashley
Created February 16, 2012 00:06
Show Gist options
  • Save andrelashley/1840245 to your computer and use it in GitHub Desktop.
Save andrelashley/1840245 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 triangle($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 triangle based on the location passed into the function.
imagepolygon($im, array($location[0], $canvasSize - $location[1] - $size,
$location[0], $canvasSize - $location[1], $location[0] + $size,
$canvasSize - $location[1]), 3, $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);
}
triangle(200, "CCCCCC", "AABBFF", array(70, 95), 50);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment