I need to edite on a HeatMap Generator ! [PHP Script]

Dear all , how are you ,
I need to show my coordinate as a HeatMap !
i did found a script doing Just what i need , but it has some performance problem i need you to help me to edit on this file since i am not that php expert , but i can understand how things works !

The problem :

I note that the performance of the script is base on how many Images that it Generate base on the Y coordinate.
Let say we have screen res. 1280x800 width and height , and up to 100K coordinates .
but all of these coordinates located between 0 and 800 Y.
The result will be 1 image , generated in less than 2 sec. which is very good .

The problem is when we have coordinate grater than 800 on Y. in this case the script will loop and generate the next images.
This will slow down the speed ,
Say i have some clicks on the Y 5000. and the rest of the clicks on the top , 800 Y , the script will generate all Y. which is very slow .

My idea is to generate the images on demand . by pissing the Y range or Image Number.

Eg : i want to show the 1st image “the top of my web site” , via ajax we pass generate Image :: 1.
The script will generate just one image.
if user scroll down he will see a BIG button to generate the HeatMap for the current place , let say it is the image 2.

In this way we will speed up the proses .

I tried to edit the class but i did not know where to edit , please kindly help me in this.

i did attach the script :

please run the /examples/fromfile.php
thank you for your help