PageRenderTime 51ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/site/sites/all/modules/click_heatmap/clickheat/classes/HeatmapFromFile.class.php

https://bitbucket.org/kaerast/ppl
PHP | 124 lines | 79 code | 8 blank | 37 comment | 14 complexity | 53f945aa45e289319b64f8a34f508b9c MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0
  1. <?php
  2. /**
  3. * ClickHeat : Classe de génération des cartes depuis un fichier de coordonnées X,Y / Maps generation class from a X,Y coords file
  4. *
  5. * Cette classe est VOLONTAIREMENT écrite pour PHP 4
  6. * This class is VOLUNTARILY written for PHP 4
  7. *
  8. * Utilisation : jettez un oeil au répertoire /examples/
  9. * Usage: have a look into /examples/ directory
  10. *
  11. * @author Yvan Taviaud - LabsMedia - www.labsmedia.com
  12. * @since 19/05/2007
  13. **/
  14. class HeatmapFromFile extends Heatmap
  15. {
  16. /** @var array $files Fichiers de suivi à étudier / Logfiles to use */
  17. var $files = array();
  18. /** @var string $regular Expression régulière pour lire les enregistrements du fichier / Regular expression to read file entries */
  19. var $regular = '/^(\d+),(\d+)$/m';
  20. /**
  21. * Add a file to parse and check existence
  22. *
  23. * @param string $file
  24. */
  25. function addFile($file)
  26. {
  27. if (file_exists($file))
  28. {
  29. $this->files[] = $file;
  30. }
  31. }
  32. /**
  33. * Do some tasks before drawing (database connection...)
  34. **/
  35. function startDrawing()
  36. {
  37. return true;
  38. }
  39. /**
  40. * Find pixels coords and draw these on the current image
  41. *
  42. * @param integer $image Number of the image (to be used with $this->height)
  43. * @return boolean Success
  44. **/
  45. function drawPixels($image)
  46. {
  47. if (count($this->files) === 0)
  48. {
  49. return $this->raiseError('No files to be used');
  50. }
  51. for ($file = 0; $file < count($this->files); $file++)
  52. {
  53. /** Read clicks in the log file */
  54. $f = @fopen($this->files[$file], 'r');
  55. if ($f === false)
  56. {
  57. return $this->raiseError('Can\'t open file: '.$this->files[$file]);
  58. }
  59. $buffer = '';
  60. $count = 0;
  61. while (true)
  62. {
  63. $buffer .= fgets($f, 1024);
  64. /** Grouping by 1000 clicks */
  65. if (feof($f) === false && $count++ !== 1000)
  66. {
  67. continue;
  68. }
  69. /** Do a regular match (faster and easier for large volume of data) */
  70. preg_match_all($this->regular, $buffer, $clicks);
  71. $buffer = '';
  72. for ($i = 0, $max = count($clicks[1]); $i < $max; $i++)
  73. {
  74. $x = (int) $clicks[1][$i];
  75. $y = (int) ($clicks[2][$i] - $image * $this->height);
  76. if ($x < 0 || $x >= $this->width)
  77. {
  78. continue;
  79. }
  80. if ($y >= 0 && $y < $this->height)
  81. {
  82. /** Apply a calculus for the step, with increases the speed of rendering : step = 3, then pixel is drawn at x = 2 (center of a 3x3 square) */
  83. $x -= $x % $this->step - $this->startStep;
  84. $y -= $y % $this->step - $this->startStep;
  85. /** Add 1 to the current color of this pixel (color which represents the sum of clicks on this pixel) */
  86. $color = imagecolorat($this->image, $x, $y) + 1;
  87. imagesetpixel($this->image, $x, $y, $color);
  88. $this->maxClicks = max($this->maxClicks, $color);
  89. }
  90. if ($image === 0)
  91. {
  92. /** Looking for the maximum height of click */
  93. $this->maxY = max($y, $this->maxY);
  94. }
  95. }
  96. unset($clicks);
  97. if ($count !== 1001)
  98. {
  99. break;
  100. }
  101. $count = 0;
  102. }
  103. fclose($f);
  104. }
  105. return true;
  106. }
  107. /**
  108. * Do some cleaning or ending tasks (close database, reset array...)
  109. **/
  110. function finishDrawing()
  111. {
  112. $this->files = array();
  113. return true;
  114. }
  115. }
  116. ?>