Skip to content

Instantly share code, notes, and snippets.

@williamlang williamlang/heatmap.php Secret
Last active Jul 8, 2018

Embed
What would you like to do?
php heatmap
<?php
$width = 300;
$height = 100;
$gradientColours = [
'blue' => [
'red' => 0,
'green' => 0,
'blue' => 255,
],
'green' => [
'red' => 0,
'green' => 255,
'blue' => 0,
],
'yellow' => [
'red' => 255,
'green' => 255,
'blue' => 0
],
'red' => [
'red' => 255,
'green' => 0,
'blue' => 0
],
'white' => [
'red' => 255,
'green' => 255,
'blue' => 255
]
];
$transitions = [
[
'start' => 0,
'end' => 128,
'startColour' => $gradientColours['blue'],
'endColour' => $gradientColours['green']
],
[
'start' => 128,
'end' => 192,
'startColour' => $gradientColours['green'],
'endColour' => $gradientColours['yellow']
],
[
'start' => 192,
'end' => 240,
'startColour' => $gradientColours['yellow'],
'endColour' => $gradientColours['red']
],
[
'start' => 240,
'end' => 256,
'startColour' => $gradientColours['red'],
'endColour' => $gradientColours['white']
]
];
foreach ($transitions as $transition) {
$start = $transition['start'];
$end = $transition['end'];
$steps = $end - $start;
$colourOne = $transition['startColour'];
$colourTwo = $transition['endColour'];
for ($i = 0; $i < $steps; $i++) {
$t = $i / $steps;
$r = $colourTwo['red'] * $t + $colourOne['red'] * (1 - $t);
$g = $colourTwo['green'] * $t + $colourOne['green'] * (1 - $t);
$b = $colourTwo['blue'] * $t + $colourOne['blue'] * (1 - $t);
$a = 127 - (($i + $start) / 255 * 127);
$gradients[$i + $start] = [
'red' => $r,
'green' => $g,
'blue' => $b,
'alpha' => $a
];
}
}
$heatmapAlpha = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($heatmapAlpha, 255, 255, 255);
imagefilledrectangle($heatmapAlpha, 0, 0, $width - 1, $height - 1, $white);
// 8% opaque point
$black = imagecolorallocatealpha($heatmapAlpha, 0, 0, 0, 127 * 0.92);
$radius = 15;
// this will create black circles that have some transparency
// the idea is many of these transparent circles on top of each other
// will slowly create a more opaque spot (a more intense spot)
foreach ($points as $point) {
for ($r = $radius; $r > 0; $r--) {
imagefilledellipse($heatmapAlpha, $point['x'], $point['y'], $r, $r, $black);
}
}
$heatmap = imagecreatetruecolor($width, $height);
imagesavealpha($heatmap, true);
for ($x = 0; $x < $width; $x++) {
for ($y = 0; $y < $height; $y++) {
// get intensity from heatmapAlpha
$intensity = imagecolorsforindex($heatmapAlpha, imagecolorat($heatmapAlpha, $x, $y));
// our mask colour, continue to next pixel
if ($intensity['red'] == 255 && $intensity['green'] == 255 && $intensity['blue'] == 255) {
continue;
}
// using the intensity get the colour
$gradientColour = $gradients[255 - $intensity['red']];
// create the colour
$colour = imagecolorallocatealpha($heatmap, $gradientColour['red'], $gradientColour['green'], $gradientColour['blue'], $gradientColour['alpha']);
// set the colour at that pixel
imagesetpixel($heatmap, $x, $y, $colour);
}
}
imagedestroy($heatmapAlpha);
imagepng($heatmap, '/save/heatmap/to/file.png');
imagedestroy($heatmap);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.