imgcarto.inc.php 10.5 KB
Newer Older
Skia's avatar
Skia committed
1
2
<?php

3
/*
4
 * @brief Classe de traçage d'objets géographiques.
5
6
7
8
9
 *
 */
/* Copyright 2007
 * - Pierre Mauduit <pierre POINT mauduit CHEZ utbm POINT fr>
 *
pedrov's avatar
pedrov committed
10
 * Ce fichier fait partie du site de l'Association des étudiants de
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 * l'UTBM, http://ae.utbm.fr.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

class imgcarto
{
  /* objets graphiques à ajouter à l'image */
pedrov's avatar
pedrov committed
32
  var $texts  = array();
33
34

  var $lines = array();
pedrov's avatar
pedrov committed
35

36
37
38
39
40
41
42
43
44
45
46
  var $polygons = array();

  var $points = array();

  /* ressource image GD */
  var $imgres = null;
  /* Couleurs */
  var $colors = array();

  /* un facteur de division d'échelle */
  var $factor = 1.0;
47

48
49
50
  /* une valeur en pixels de décalage */
  var $offset = 10;

pedrov's avatar
pedrov committed
51
52
53
54
  /* points minis / maxi */
  var $minx, $miny;
  var $maxx, $maxy;

55
56
57
58
  /* dimensions */
  var $dimx, $dimy;

  var $errmsg;
59

pedrov's avatar
pedrov committed
60
  var $calculated = false;
61

pedrov's avatar
pedrov committed
62
  function imgcarto($dimx, $offset)
63
  {
pedrov's avatar
pedrov committed
64
65
    $this->dimx = $dimx;
    $this->offset = $offset;
66

pedrov's avatar
pedrov committed
67
    /* quelques couleurs de base */
68
69
70
71
    $this->addcolor("black", 0,0,0);
    $this->addcolor("red", 255, 0,0);
    $this->addcolor("blue", 0,0,255);
    $this->addcolor("white", 255,255,255);
pedrov's avatar
pedrov committed
72

73
74
    return;
  }
75

76
77
  function addcolor($def, $r,$g,$b)
  {
pedrov's avatar
plouf    
pedrov committed
78
79
80
    /* on ne génère pas encore la couleur via imagecolorallocate()
     * étant donné que les dimensions de l'image ne sont pas encore
     * connues, et que l'image n'est pas encore créée.
81
82
     */

pedrov's avatar
plouf    
pedrov committed
83
    $this->colors[$def] = array($r,$g,$b);
84
  }
85

86

87
  function addtext($size, $angle, $x, $y, $color, $text, $font = null, $pointed = null)
pedrov's avatar
pedrov committed
88
89
90
91
  {
    global $topdir;

    if ($font == null)
pedrov's avatar
pedrov committed
92
      $font =  $topdir . "font/verdana.ttf";
93

pedrov's avatar
pedrov committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    if (!isset($this->minx))
      $this->minx = $x;
    if (!isset($this->miny))
      $this->miny = $y;


    if ($this->minx > $x)
      $this->minx = $x;
    if ($this->maxx < $x)
      $this->maxx = $x;

    if ($this->miny > $y)
      $this->miny = $y;
    if ($this->maxy < $y)
      $this->maxy = $y;
pedrov's avatar
pedrov committed
109

110

111
    $this->texts[] = array($size, $angle, $x, $y, $color, $font, $text, $pointed);
pedrov's avatar
pedrov committed
112
113
114

  }

pedrov's avatar
legend    
pedrov committed
115
116
117
  function addpointwithlegend($x, $y, $r, $pcolor, $size, $angle, $text, $tcolor, $font = null)
  {
    $this->addpoint($x, $y, $r, $color);
118
    $this->addtext($size, $angle, $x, $y, $tcolor, $text, $font, $r);
pedrov's avatar
legend    
pedrov committed
119
120
  }

121
122
  function addpoint($x, $y, $r, $color)
  {
pedrov's avatar
pedrov committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

    if (!isset($this->minx))
      $this->minx = $x;
    if (!isset($this->miny))
      $this->miny = $y;

    if ($this->minx > $x)
      $this->minx = $x;
    if ($this->maxx < $x)
      $this->maxx = $x;

    if ($this->miny > $y)
      $this->miny = $y;
    if ($this->maxy < $y)
      $this->maxy = $y;

139
140
141
142
143
    $this->points[] = array($x,$y,$r, $color);
  }

  function addline($x, $y, $fx, $fy, $color)
  {
pedrov's avatar
pedrov committed
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
    if (!isset($this->minx))
      $this->minx = $x;
    if (!isset($this->miny))
      $this->miny = $y;

    if ($this->minx > $x)
      $this->minx = $x;
    if ($this->maxx < $x)
      $this->maxx = $x;

    if ($this->miny > $y)
      $this->miny = $y;
    if ($this->maxy < $y)
      $this->maxy = $y;

    if ($this->minx > $fx)
      $this->minx = $fx;
    if ($this->maxx < $fx)
      $this->maxx = $fx;

    if ($this->miny > $fy)
      $this->miny = $fy;
    if ($this->maxy < $fy)
      $this->maxy = $fy;

pedrov's avatar
plouf    
pedrov committed
169
    $this->lines[] = array($x,$y,$fx,$fy,$color);
170
  }
pedrov's avatar
map    
pedrov committed
171
172
173
174
175
176
177

  /*
   * Précisions sur la variable facultative $mapdatas : contient
   * éventuellement un tableau associatif avec une clé id désignant
   * l'identifiant unique de "l'objet", ainsi qu'une URL pour l'action.
   */
  function addpolygon($plg, $color, $filled = false, $mapdatas = null)
178
  {
pedrov's avatar
pedrov committed
179
180
    if (count($plg) <= 0)
      return;
181

pedrov's avatar
pedrov committed
182
    for ($i = 0; $i < count($plg); $i +=2)
183
      {
pedrov's avatar
pedrov committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
	$x = $plg[$i];
	$y = $plg[$i+1];
	if (!isset($this->minx))
	  $this->minx = $x;
	if (!isset($this->miny))
	  $this->miny = $y;

	if ($this->minx > $x)
	  $this->minx = $x;
	if ($this->maxx < $x)
	  $this->maxx = $x;

	if ($this->miny > $y)
	  $this->miny = $y;
	if ($this->maxy < $y)
	  $this->maxy = $y;
Ayolo's avatar
Ayolo committed
200
      }
pedrov's avatar
plouf    
pedrov committed
201

pedrov's avatar
pedrov committed
202
203
    $this->polygons[] = array($plg, $color, $filled, $mapdatas);
  }
204

pedrov's avatar
pedrov committed
205
206
207
208
209
  /* passe les coordonnées des objets en positif */
  function setpositivecoords()
  {
    /* pour passer en positif, il suffit de retrancher aux coordonnées
     * le minimum */
210

pedrov's avatar
pedrov committed
211
    /* textes */
pedrov's avatar
pedrov committed
212
213
    if (count($this->texts))
      {
pedrov's avatar
pedrov committed
214
	foreach ($this->texts as &$text)
pedrov's avatar
pedrov committed
215
	  {
pedrov's avatar
pedrov committed
216
217
	    $text[2] -= $this->minx;
	    $text[3] -= $this->miny;
pedrov's avatar
pedrov committed
218
219
	  }
      }
pedrov's avatar
pedrov committed
220
    /* points */
221
222
    if (count($this->points))
      {
pedrov's avatar
pedrov committed
223
224
225
226
227
	foreach($this->points as &$point)
	  {
	    $point[0] -= $this->minx;
	    $point[1] -= $this->miny;
	  }
228
      }
pedrov's avatar
pedrov committed
229
    /* lignes */
230
231
    if (count($this->lines))
      {
pedrov's avatar
pedrov committed
232
233
234
235
236
237
238
	foreach($this->lines as &$line)
	  {
	    $line[0] -= $this->minx;
	    $line[1] -= $this->miny;
	    $line[2] -= $this->minx;
	    $line[3] -= $this->miny;
	  }
239
      }
pedrov's avatar
pedrov committed
240
    /* polygones */
241
242
    if (count($this->polygons))
      {
pedrov's avatar
pedrov committed
243
244
245
246
247
248
249
	foreach ($this->polygons as &$polygon)
	  {
	    for ($i = 0; $i < count($polygon[0]); $i+=2)
	      {
		$polygon[0][$i] -= $this->minx;
		$polygon[0][$i+1] -= $this->miny;
	      }
250

pedrov's avatar
pedrov committed
251
	  }
252
      }
pedrov's avatar
pedrov committed
253
  }
pedrov's avatar
plouf    
pedrov committed
254

pedrov's avatar
pedrov committed
255
256
257
258
259
260
261
262
263
264
  /* calcul des dimensions */
  function calculatedimensions($invert_y = true)
  {
    $this->factor = (($this->dimx - 2 * $this->offset) / ($this->maxx - $this->minx));
    $this->dimy = ($this->maxy - $this->miny) * $this->factor + 2 * $this->offset;
    /* on repasse en revue les objets afin de leur donner
     * une taille en adéquation avec l'image de sortie
     */
    /* textes */
    if (count($this->texts))
pedrov's avatar
plouf    
pedrov committed
265
      {
pedrov's avatar
pedrov committed
266
267
	foreach ($this->texts as &$text)
	  {
pedrov's avatar
legend    
pedrov committed
268
	    $text[2] = $text[2] * $this->factor + $this->offset;
pedrov's avatar
pedrov committed
269
	    $text[3] = $text[3] * $this->factor + $this->offset;
270

271
272
273
274
	    /* point avec légende */
	    if ($text[7] != null)
	      {
		$text[2] = $text[2] + 1.5 * $text[7];
pedrov's avatar
pedrov committed
275
		$text[3] = $text[3] - $text[7] / 2;
276
277
	      }

278

279

pedrov's avatar
pedrov committed
280
281
282
	    if ($invert_y)
	      $text[3] = $this->dimy - $text[3];
	  }
Ayolo's avatar
Ayolo committed
283
      }
pedrov's avatar
pedrov committed
284
    /* points */
pedrov's avatar
plouf    
pedrov committed
285
286
    if (count($this->points))
      {
pedrov's avatar
pedrov committed
287
288
289
290
291
292
293
	foreach($this->points as &$point)
	  {
	    $point[0] = $point[0] * $this->factor + $this->offset;
	    $point[1] = $point[1] * $this->factor + $this->offset;
	    if ($invert_y)
	      $point[1] = $this->dimy - $point[1];
	  }
Ayolo's avatar
Ayolo committed
294
      }
pedrov's avatar
pedrov committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
    /* lignes */
    if (count($this->lines))
      {
	foreach($this->lines as &$line)
	  {
	    $line[0] = $line[0] * $this->factor + $this->offset;
	    $line[1] = $line[1] * $this->factor + $this->offset;
	    $line[2] = $line[2] * $this->factor + $this->offset;
	    $line[3] = $line[3] * $this->factor + $this->offset;

	    if ($invert_y)
	      {
		$line[1] = $this->dimy - $line[1];
		$line[3] = $this->dimy - $line[3];
	      }
	  }
      }
    /* polygones */
pedrov's avatar
plouf    
pedrov committed
313
314
    if (count($this->polygons))
      {
pedrov's avatar
pedrov committed
315
316
317
318
319
320
	foreach ($this->polygons as &$polygon)
	  {
	    for ($i = 0; $i < count($polygon[0]); $i+=2)
	      {
		$polygon[0][$i]    = $polygon[0][$i] * $this->factor + $this->offset;
		$polygon[0][$i+1]  = $polygon[0][$i+1] * $this->factor + $this->offset;
321

pedrov's avatar
pedrov committed
322
323
324
325
		if ($invert_y)
		  $polygon[0][$i+1] = $this->dimy - $polygon[0][$i+1];
	      }
	  }
Ayolo's avatar
Ayolo committed
326
      }
pedrov's avatar
pedrov committed
327
    $this->calculated = true;
328
329
330
331
332
  }

  function draw()
  {

pedrov's avatar
pedrov committed
333
334
335
336
337
    if ($this->calculated == false)
      {
	$this->setpositivecoords();
	$this->calculatedimensions();
      }
338

pedrov's avatar
plouf    
pedrov committed
339
    $this->imgres = imagecreatetruecolor($this->dimx, $this->dimy);
340

341
342
    /* allocate colors */
    if (count($this->colors))
Ayolo's avatar
Ayolo committed
343
344
    {
      foreach ($this->colors as $key => $color)
345
      {
Ayolo's avatar
Ayolo committed
346
347
348
349
        $this->colors[$key]['gd'] = imagecolorallocate($this->imgres,
                                                       $color[0],
                                                       $color[1],
                                                       $color[2]);
350
      }
Ayolo's avatar
Ayolo committed
351
    }
352

pedrov's avatar
plouf    
pedrov committed
353
354
355
356
    imagefill($this->imgres, 0,0, $this->colors['white']['gd']);

    /* draw polygons */
    if (count($this->polygons))
Ayolo's avatar
Ayolo committed
357
358
    {
      foreach ($this->polygons as $polygon)
359
      {
Ayolo's avatar
Ayolo committed
360
361
362
363
        if ($polygon[2] == false)
          imagepolygon($this->imgres, $polygon[0], count($polygon[0]) / 2, $this->colors[$polygon[1]]['gd']);
        else
          imagefilledpolygon($this->imgres, $polygon[0], count($polygon[0]) / 2, $this->colors[$polygon[1]]['gd']);
364
      }
Ayolo's avatar
Ayolo committed
365
    }
pedrov's avatar
plouf    
pedrov committed
366

367
368
    /* draw lines */
    if (count($this->lines))
Ayolo's avatar
Ayolo committed
369
370
    {
      foreach ($this->lines as $line)
371
      {
Ayolo's avatar
Ayolo committed
372
373
374
375
376
377
        imageline ($this->imgres,
                   $line[0],
                   $line[1],
                   $line[2],
                   $line[3],
                   $this->colors[$line[4]]['gd']);
378
      }
Ayolo's avatar
Ayolo committed
379
    }
pedrov's avatar
plouf    
pedrov committed
380
381
    /* draw points */
    if (count($this->points))
Ayolo's avatar
Ayolo committed
382
383
    {
      foreach ($this->points as $point)
384
      {
Ayolo's avatar
Ayolo committed
385
386
387
388
389
390
        imagefilledellipse ($this->imgres,
                            $point[0],
                            $point[1],
                            $point[2],
                            $point[2],
                            $this->colors[$point[3]]['gd']);
391

392
      }
Ayolo's avatar
Ayolo committed
393
    }
pedrov's avatar
pedrov committed
394
395
396
397
398
399
400
401
402
403
404
    /* draw texts */
    if (count($this->texts))
      {
	foreach ($this->texts as $text)
	  {
	    imagettftext ($this->imgres,
			  $text[0],
			  $text[1],
			  $text[2],
			  $text[3],
			  $this->colors[$text[4]]['gd'],
pedrov's avatar
pedrov committed
405
406
			  $text[5],
			  $text[6]);
pedrov's avatar
pedrov committed
407
408
	  }
      }
409
410
411
  }
  function saveas($path)
  {
pedrov's avatar
plouf    
pedrov committed
412
413
    if ($this->imgres)
      imagepng($this->imgres, $path);
414
  }
pedrov's avatar
plouf    
pedrov committed
415

416
417
  function output()
  {
pedrov's avatar
plouf    
pedrov committed
418
    if ($this->imgres)
Ayolo's avatar
Ayolo committed
419
420
421
422
    {
      header("Content-Type: image/png");
      imagepng($this->imgres);
    }
423
424
425
426
427
428
429
430
  }

  function destroy()
  {
    if ($imgres)
      imagedestroy($imgres);
  }

431
432
433
  function map_area($mapname="map")
  {

pedrov's avatar
pedrov committed
434
435
436
437
438
439
    if ($this->calculated == false)
      {
	$this->setpositivecoords();
	$this->calculatedimensions();
      }

440
441
    if (count($this->polygons))
    {
Ayolo's avatar
bleh    
Ayolo committed
442
      $map = "<map name=\"".$mapname."\">\n";
443
444
445
446
      $pol_n=0;
      foreach ($this->polygons as $polygon)
      {
        $map .="<area shape=\"poly\" coords=\"";
pedrov's avatar
pedrov committed
447

pedrov's avatar
map    
pedrov committed
448
449
450
451
452
453
454
455
	$values = array();

	foreach ($polygon[0] as $elem)
	  $values[] = intval($elem);


	$map .= implode (",", $values);

pedrov's avatar
map    
pedrov committed
456
457

	$map .= "\" href=\"".$polygon[3]['url']."\" alt=\"notset\" />\n";
458
        $pol_n++;
pedrov's avatar
map    
pedrov committed
459

460
      }
Ayolo's avatar
bleh    
Ayolo committed
461
      $map .= "</map>\n";
462
463
464
465
466

      return $map;
    }
    return "";
  }
467
468
469
}

?>