Skip to content

Instantly share code, notes, and snippets.

@williamlang
Last active May 6, 2022 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save williamlang/69ab6649c19af0bbb5ce9212c35b274f to your computer and use it in GitHub Desktop.
Save williamlang/69ab6649c19af0bbb5ce9212c35b274f to your computer and use it in GitHub Desktop.
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