[ Index ] |
|
Code source de phpMyVisites 2.3 |
1 <?php 2 /** 3 * ClickHeat : Classe de génération des cartes depuis un fichier ClickHeat / Maps generation class from a ClickHeat logfile 4 * 5 * Cette classe est VOLONTAIREMENT écrite pour PHP 4 6 * This class is VOLUNTARILY written for PHP 4 7 * 8 * @author Yvan Taviaud - LabsMedia - www.labsmedia.com 9 * @since 12/05/2007 10 **/ 11 12 class HeatmapFromClicks extends Heatmap 13 { 14 /** @var array $files Fichiers de suivi à étudier / Logfiles to use */ 15 var $files = array(); 16 /** @var string $browser Browser filter */ 17 var $browser; 18 /** @var array $layout Webpage layout */ 19 var $layout; 20 /** @var integer $minScreen Screen filter */ 21 var $minScreen; 22 /** @var integer $maxScreen Screen filter */ 23 var $maxScreen; 24 25 /** 26 * Add a file to parse and check existence 27 * 28 * @param string $file 29 */ 30 function addFile($file) 31 { 32 if (file_exists($file)) 33 { 34 $this->files[] = $file; 35 } 36 } 37 38 /** 39 * Do some tasks before drawing (database connection...) 40 **/ 41 function startDrawing() 42 { 43 return true; 44 } 45 46 /** 47 * Find pixels coords and draw these on the current image 48 * 49 * @param integer $image Number of the image (to be used with $this->height) 50 * @return boolean Success 51 **/ 52 function drawPixels($image) 53 { 54 /** If it's not the first image, just use the value stored in the temp files */ 55 if ($image !== 0) 56 { 57 if (file_exists($this->cache.sprintf($this->file, $image).'_log')) 58 { 59 $f = fopen($this->cache.sprintf($this->file, $image).'_log', 'r'); 60 while (feof($f) === false) 61 { 62 $click = explode('|', trim(fgets($f, 1024))); 63 if (count($click) === 3) 64 { 65 $x = (int) $click[0]; 66 $y = (int) $click[1]; 67 $c = (bool) $click[2]; 68 if ($this->heatmap === true) 69 { 70 /** 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) */ 71 $x -= $x % $this->step - $this->startStep; 72 $y -= $y % $this->step - $this->startStep; 73 /** Add 1 to the current color of this pixel (color which represents the sum of clicks on this pixel) */ 74 $color = imagecolorat($this->image, $x, $y) + 1; 75 imagesetpixel($this->image, $x, $y, $color); 76 $this->maxClicks = max($this->maxClicks, $color); 77 } 78 else 79 { 80 /** Put a red or green cross at the click location */ 81 $color = $c === true ? 0xFF0000 : 0x00DC00; 82 imageline($this->image, $x - 2, $y - 2, $x + 2, $y + 2, $color); 83 imageline($this->image, $x + 2, $y - 2, $x - 2, $y + 2, $color); 84 } 85 } 86 } 87 fclose($f); 88 unlink($this->cache.sprintf($this->file, $image).'_log'); 89 } 90 return true; 91 } 92 93 $leftWidth = (int) $this->layout[1]; 94 $centerWidth = (int) $this->layout[2]; 95 $rightWidth = (int) $this->layout[3]; 96 if ($leftWidth !== 0 && $centerWidth !== 0 && $rightWidth === 0) 97 { 98 /** Fixed left menu and fixed center ? Fall back to a fixed left menu only */ 99 $leftWidth += $centerWidth; 100 $centerWidth = 0; 101 } 102 elseif ($leftWidth === 0 && $centerWidth !== 0 && $rightWidth !== 0) 103 { 104 /** Fixed right menu and fixed center ? Fall back to a fixed right menu only */ 105 $rightWidth += $centerWidth; 106 $centerWidth = 0; 107 } 108 elseif ($leftWidth !== 0 && $centerWidth !== 0 && $rightWidth !== 0) 109 { 110 /** Everything is fixed ?? */ 111 return $this->raiseError(LANG_ERROR_FIXED); 112 } 113 114 for ($file = 0; $file < count($this->files); $file++) 115 { 116 /** Read clicks in the log file */ 117 $f = @fopen($this->files[$file], 'r'); 118 if ($f === false) 119 { 120 return $this->raiseError(LANG_ERROR_FILE.': '.$this->files[$file]); 121 } 122 123 $buffer = ''; 124 $count = 0; 125 while (true) 126 { 127 $buffer .= fgets($f, 1024); 128 /** Grouping by 1000 clicks */ 129 if (feof($f) === false && $count++ !== 1000) 130 { 131 continue; 132 } 133 /** Do a regular match (faster and easier for large volume of data) */ 134 preg_match_all('~^(\d+)\|(\d+)\|(\d+)\|'.($this->browser === 'all' ? '[a-z]+' : $this->browser).'\|(\d)$~m', $buffer, $clicks); 135 $buffer = ''; 136 137 for ($i = 0, $max = count($clicks[1]); $i < $max; $i++) 138 { 139 $x = (int) $clicks[1][$i]; // X 140 $y = (int) $clicks[2][$i]; // Y 141 $w = (int) $clicks[3][$i]; // display width 142 $c = ($clicks[4][$i] < 3); // left click 143 /** X is not in the range of sizes, or right click for heatmap, or X is greater than screen size, the website is too large for the window, so we don't know where the click is... ignore those clicks */ 144 if ($x <= $this->minScreen || $x > $this->maxScreen || $this->heatmap === true && $c === false || $x > $w) 145 { 146 continue; 147 } 148 /** Correction of X for liquid and/or fixed layouts */ 149 if ($leftWidth !== 0 && $centerWidth === 0 && $rightWidth === 0) 150 { 151 /** Left fixed menu */ 152 if ($x <= $leftWidth) 153 { 154 /** Click in the left menu : X is good */ 155 } 156 else 157 { 158 /** Apply a percentage on the rest of the screen */ 159 $x = $leftWidth + ceil(($this->width - $leftWidth) * ($x - $leftWidth) / ($w - $leftWidth)); 160 } 161 } 162 elseif ($leftWidth === 0 && $centerWidth === 0 && $rightWidth !== 0) 163 { 164 /** Right fixed menu */ 165 if ($w - $x <= $rightWidth) 166 { 167 /** Click in the right menu : X is good, but relative to the right border */ 168 $x = $this->width - ($w - $x); 169 } 170 else 171 { 172 /** Apply a percentage on the rest of the screen */ 173 $x = ceil(($this->width - $rightWidth) * $x / ($w - $rightWidth)); 174 } 175 } 176 elseif ($leftWidth !== 0 && $centerWidth === 0 && $rightWidth !== 0) 177 { 178 /** Left and right fixed menus */ 179 if ($w - $x <= $rightWidth) 180 { 181 /** Click in the right menu : X is good, but relative to the right border */ 182 $x = $this->width - ($w - $x); 183 } 184 elseif ($x <= $leftWidth) 185 { 186 /** Click in the left menu : X is good */ 187 } 188 else 189 { 190 /** Apply a percentage on the rest of the screen */ 191 $x = $leftWidth + ceil(($this->width - $leftWidth - $rightWidth) * ($x - $leftWidth) / ($w - $leftWidth - $rightWidth)); 192 } 193 } 194 elseif ($leftWidth === 0 && $centerWidth !== 0 && $rightWidth === 0) 195 { 196 /** Fixed centered content */ 197 if (abs($x - $w / 2) <= $centerWidth / 2) 198 { 199 /** Click is in the centered content */ 200 $x = ($this->width - $w) / 2 + $x; 201 } 202 elseif ($x < $w / 2) 203 { 204 /** Click is at the left of the centered content */ 205 $x = ($this->width - $centerWidth) / ($w - $centerWidth) * $x; 206 } 207 else 208 { 209 /** Click is at the right of the centered content */ 210 $x = ($this->width + $centerWidth) / 2 + ($this->width - $centerWidth) / ($w - $centerWidth) * ($x - ($w + $centerWidth) / 2); 211 } 212 } 213 else 214 { 215 /** Layout 100% */ 216 $x = $this->width / $w * $x; 217 } 218 $x = (int) $x; 219 $y = (int) $y; 220 if ($x < 0 || $x >= $this->width) 221 { 222 continue; 223 } 224 if ($y >= 0 && $y < $this->height) 225 { 226 if ($this->heatmap === true) 227 { 228 /** 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) */ 229 $x -= $x % $this->step - $this->startStep; 230 $y -= $y % $this->step - $this->startStep; 231 /** Add 1 to the current color of this pixel (color which represents the sum of clicks on this pixel) */ 232 $color = imagecolorat($this->image, $x, $y) + 1; 233 imagesetpixel($this->image, $x, $y, $color); 234 $this->maxClicks = max($this->maxClicks, $color); 235 } 236 else 237 { 238 /** Put a red or green cross at the click location */ 239 $color = $c === true ? 0xFF0000 : 0x00DC00; 240 imageline($this->image, $x - 2, $y - 2, $x + 2, $y + 2, $color); 241 imageline($this->image, $x + 2, $y - 2, $x - 2, $y + 2, $color); 242 } 243 } 244 else 245 { 246 /** Append to the temporary log of this image */ 247 error_log($x.'|'.($y % $this->height).'|'.$c."\n", 3, $this->cache.sprintf($this->file, ceil($y / $this->height) - 1).'_log'); 248 } 249 /** Looking for the maximum height of click */ 250 $this->maxY = max($y, $this->maxY); 251 } 252 unset($clicks); 253 254 if ($count !== 1001) 255 { 256 break; 257 } 258 $count = 0; 259 } 260 fclose($f); 261 } 262 return true; 263 } 264 265 /** 266 * Do some cleaning or ending tasks (close database, reset array...) 267 **/ 268 function finishDrawing() 269 { 270 $this->files = array(); 271 return true; 272 } 273 } 274 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 14:10:01 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |