More PHP 5.4 fixes
[busui.git] / lib / HeatMap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/*
*DISCLAIMER
* http://blog.gmapify.fr/create-beautiful-tiled-heat-maps-with-php-and-gd
*THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, *INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF *USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*       @author: Olivier G. <olbibigo_AT_gmail_DOT_com>
*       @version: 1.0
*       @history:
*               1.0     creation
*/
        define('PI2', 2*M_PI);
 
        class HeatMapPoint{
                public $x,$y;
                function __construct($x,$y) {
                        $this->x = $x;
                        $this->y = $y;
                }
                function __toString() {
                        return "({$this->x},{$this->y})";
                }
        }//Point
 
        class HeatMap{
                //TRANSPARENCY
                public static $WITH_ALPHA = 0;
                public static $WITH_TRANSPARENCY = 1;
                //GRADIENT STYLE
                public static $GRADIENT_CLASSIC = 'classic';
                public static $GRADIENT_FIRE = 'fire';
                public static $GRADIENT_PGAITCH = 'pgaitch';
                //GRADIENT MODE (for heatImage)
                public static $GRADIENT_NO_NEGATE_NO_INTERPOLATE = 0;
                public static $GRADIENT_NO_NEGATE_INTERPOLATE = 1;
                public static $GRADIENT_NEGATE_NO_INTERPOLATE = 2;
                public static $GRADIENT_NEGATE_INTERPOLATE = 3;
                //NOT PROCESSED PIXEL (for heatImage)
                public static $KEEP_VALUE = 0;
                public static $NO_KEEP_VALUE = 1;
                //CONSTRAINTS
                private static $MIN_RADIUS = 2;//in px
                private static $MAX_RADIUS = 50;//in px
                private static $MAX_IMAGE_SIZE = 10000;//in px
                
                //generate an $image_width by $image_height pixels heatmap image of $points
                public static function createImage($data, $image_width, $image_height, $mode=0, $spot_radius = 30, $dimming = 75, $gradient_name = 'classic'){
                        $_gradient_name = $gradient_name;
                        if(($_gradient_name != self::$GRADIENT_CLASSIC) && ($_gradient_name != self::$GRADIENT_FIRE) && ($_gradient_name != self::$GRADIENT_PGAITCH)){
                                $_gradient_name = self::$GRADIENT_CLASSIC;
                        }
                        $_image_width = min(self::$MAX_IMAGE_SIZE, max(0, intval($image_width)));
                        $_image_height = min(self::$MAX_IMAGE_SIZE, max(0, intval($image_height)));
                        $_spot_radius = min(self::$MAX_RADIUS, max(self::$MIN_RADIUS, intval($spot_radius)));
                        $_dimming = min(255, max(0, intval($dimming)));
                        if(!is_array($data)){
                                return false;
                        }
                        $im = imagecreatetruecolor($_image_width, $_image_height);
                        $white = imagecolorallocate($im, 255, 255, 255);
                        imagefill($im, 0, 0, $white);
                        if(self::$WITH_ALPHA == $mode){
                                imagealphablending($im, false);
                                imagesavealpha($im,true);
                        }
                        //Step 1: create grayscale image
                        foreach($data as $datum){
                                if( (is_array($datum) && (count($datum)==1)) || (!is_array($datum) && ('HeatMapPoint' == get_class($datum)))){//Plot points
                                        if('HeatMapPoint' != get_class($datum)){
                                                $datum = $datum[0];
                                        }
                                        self::_drawCircularGradient($im, $datum->x, $datum->y, $_spot_radius, $_dimming);
                                }else if(is_array($datum)){//Draw lines
                                        $length = count($datum)-1;
                                        for($i=0; $i < $length; ++$i){//Loop through points
                                                //Bresenham's algorithm to plot from from $datum[$i] to $datum[$i+1];
                                                self::_drawBilinearGradient($im, $datum[$i], $datum[$i+1], $_spot_radius, $_dimming);
                                        }
                                }
                        }
                        //Gaussian filter
                        if($_spot_radius >= 30){
                                imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR);
                        }
                        //Step 2: create colored image
                        if(FALSE === ($grad_rgba = self::_createGradient($im, $mode, $_gradient_name))){
                                return FALSE;
                        }
                        $grad_size = count($grad_rgba);
                        for($x=0; $x <$_image_width; ++$x){
                                for($y=0; $y <$_image_height; ++$y){
                                        $level = imagecolorat($im, $x, $y) & 0xFF;
                                        if( ($level >= 0) && ($level < $grad_size) ){
                                                imagesetpixel($im, $x, $y, $grad_rgba[imagecolorat($im, $x, $y) & 0xFF]);
                                        }
                                }
                        }
                        if(self::$WITH_TRANSPARENCY == $mode){
                                imagecolortransparent($im, $grad_rgba[count($grad_rgba)-1]);
                        }
                        return $im;
                }//createImage
 
                //Heat an image
                public static function heatImage($filepath, $gradient_name = 'classic', $mode= 0, $min_level=0, $max_level=255, $gradient_interpolate=0, $keep_value=0){
                        $_gradient_name = $gradient_name;
                        if(($_gradient_name != self::$GRADIENT_CLASSIC) && ($_gradient_name != self::$GRADIENT_FIRE) && ($_gradient_name != self::$GRADIENT_PGAITCH)){
                                $_gradient_name = self::$GRADIENT_CLASSIC;
                        }
                        $_min_level = min(255, max(0, intval($min_level)));
                        $_max_level = min(255, max(0, intval($max_level)));
 
                        //try opening jpg first then png then gif format
                        if(FALSE === ($im = @imagecreatefromjpeg($filepath))){
                                if(FALSE === ($im = @imagecreatefrompng($filepath))){
                                        if(FALSE === ($im = @imagecreatefromgif($filepath))){
                                                return FALSE;
                                        }
                                }
                        }
                        if(self::$WITH_ALPHA == $mode){
                                imagealphablending($im, false);
                                imagesavealpha($im,true);
                        }
                        $width = imagesx($im);
                        $height = imagesy($im); 
                        if(FALSE === ($grad_rgba = self::_createGradient($im, $mode, $_gradient_name))){
                                return FALSE;
                        }
                        //Convert to grayscale
                        $grad_size = count($grad_rgba);
                        $level_range = $_max_level - $_min_level;
                        for($x=0; $x <$width; ++$x){
                                for($y=0; $y <$height; ++$y){
                                        $rgb = imagecolorat($im, $x, $y);
                                        $r = ($rgb >> 16) & 0xFF;
                                        $g = ($rgb >> 8) & 0xFF;
                                        $b = $rgb & 0xFF;
                                        $gray_level = Min(255, Max(0, floor(0.33 * $r + 0.5 * $g + 0.16 * $b)));//between 0 and 255                             
                                        if( ($gray_level >= $_min_level) && ($gray_level <= $_max_level) ){
                                                switch($gradient_interpolate){
                                                        case self::$GRADIENT_NO_NEGATE_NO_INTERPOLATE:
                                                                //$_max_level takes related lowest gradient color
                                                                //$_min_level takes related highest gradient color
                                                                $value = 255 - $gray_level;
                                                                break;
                                                        case self::$GRADIENT_NEGATE_NO_INTERPOLATE:
                                                                //$_max_level takes related highest gradient color
                                                                //$_min_level takes related lowest gradient color
                                                                $value = $gray_level;
                                                                break;
                                                        case self::$GRADIENT_NO_NEGATE_INTERPOLATE:
                                                                //$_max_level takes lowest gradient color
                                                                //$_min_level takes highest gradient color
                                                                $value = 255- floor(($gray_level - $_min_level) * $grad_size / $level_range);
                                                                break;
                                                        case self::$GRADIENT_NEGATE_INTERPOLATE:
                                                                //$_max_level takes highest gradient color
                                                                //$_min_level takes lowest gradient color
                                                                $value = floor(($gray_level - $_min_level) * $grad_size / $level_range);
                                                                break;
                                                        default:
                                                }
                                                imagesetpixel($im, $x, $y, $grad_rgba[$value]);
                                        }else{
                                                if(self::$KEEP_VALUE == $keep_value){
                                                        //Do nothing
                                                }else{//self::$NO_KEEP_VALUE
                                                        imagesetpixel($im, $x, $y, imagecolorallocatealpha($im,0,0,0,0));
                                                }
                                        }
                                }
                        }