1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smindel\GIS\Service; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Injector\Injectable; |
6
|
|
|
use SilverStripe\Core\Config\Configurable; |
7
|
|
|
|
8
|
|
|
class GDRenderer |
9
|
|
|
{ |
10
|
|
|
use Injectable; |
11
|
|
|
|
12
|
|
|
use Configurable; |
13
|
|
|
|
14
|
|
|
const LIB_NAME = 'GD'; |
15
|
|
|
|
16
|
|
|
protected $width; |
17
|
|
|
|
18
|
|
|
protected $height; |
19
|
|
|
|
20
|
|
|
protected $list; |
21
|
|
|
|
22
|
|
|
protected $image; |
23
|
|
|
|
24
|
|
|
protected $defaultStyle; |
25
|
|
|
|
26
|
|
|
public $backgroundcolor; |
27
|
|
|
public $strokecolor; |
28
|
|
|
public $fillcolor; |
29
|
|
|
|
30
|
4 |
|
public function __construct($width, $height, $defaultStyle = []) |
31
|
|
|
{ |
32
|
4 |
|
$this->width = $width; |
33
|
4 |
|
$this->height = $height; |
34
|
4 |
|
$this->defaultStyle = $defaultStyle; |
35
|
|
|
|
36
|
4 |
|
$this->image = imagecreate($this->width, $this->height); |
37
|
|
|
|
38
|
4 |
|
foreach ($defaultStyle['gd'] as $key => $val) { |
39
|
4 |
|
if ($key == 'pointradius') { |
40
|
4 |
|
continue; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
switch ($key) { |
44
|
4 |
|
case 'pointradius': |
45
|
|
|
continue; |
46
|
4 |
|
case 'backgroundcolor': |
47
|
4 |
|
case 'strokecolor': |
48
|
4 |
|
case 'fillcolor': |
49
|
4 |
|
$method = count($val) == 4 ? 'imagecolorallocatealpha' : 'imagecolorallocate'; |
50
|
4 |
|
$this->$key = $method($this->image, ...$val); |
51
|
4 |
|
break; |
52
|
|
|
default: |
53
|
4 |
|
$method = 'image' . $key; |
54
|
4 |
|
array_unshift($val, $this->image); |
55
|
4 |
|
$method(...$val); |
56
|
|
|
} |
57
|
|
|
} |
58
|
4 |
|
} |
59
|
|
|
|
60
|
|
|
public function debug($text) |
61
|
|
|
{ |
62
|
|
|
$color = imagecolorallocate($this->image, 255, 0, 0); |
63
|
|
|
imagedashedline($this->image, 0, 0, $this->width, 0, $color); |
64
|
|
|
imagedashedline($this->image, 0, 0, 0, $this->height, $color); |
65
|
|
|
imagestring($this->image, 5, 5, 5, $text, $color); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getImage() |
69
|
|
|
{ |
70
|
|
|
return $this->image; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getContentType() |
74
|
|
|
{ |
75
|
|
|
return 'image/png'; |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
public function drawPoint($coordinates) |
79
|
|
|
{ |
80
|
2 |
|
list($x, $y) = $coordinates; |
81
|
2 |
|
imagefilledarc($this->image, $x, $y, 2 * $this->defaultStyle['gd']['pointradius'], 2 * $this->defaultStyle['gd']['pointradius'], 0, 360, $this->fillcolor, IMG_ARC_PIE); |
82
|
2 |
|
imagearc($this->image, $x, $y, 2 * $this->defaultStyle['gd']['pointradius'], 2 * $this->defaultStyle['gd']['pointradius'], 0, 360, $this->strokecolor); |
83
|
2 |
|
} |
84
|
|
|
|
85
|
|
|
public function drawLineString($coordinates) |
86
|
|
|
{ |
87
|
|
|
foreach ($coordinates as $coord) { |
88
|
|
|
if (isset($prev)) { |
89
|
|
|
imageline($this->image, $prev[0], $prev[1], $coord[0], $coord[1], $this->strokecolor); |
90
|
|
|
} |
91
|
|
|
$prev = $coord; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
public function drawPolygon($coordinates) |
96
|
|
|
{ |
97
|
2 |
|
foreach ($coordinates as $coords) { |
98
|
2 |
|
$xy = []; |
99
|
2 |
|
foreach ($coords as $coord) { |
100
|
2 |
|
$xy[] = $coord[0]; |
101
|
2 |
|
$xy[] = $coord[1]; |
102
|
|
|
} |
103
|
2 |
|
imagefilledpolygon($this->image, $xy, count($xy) / 2, $this->fillcolor); |
104
|
2 |
|
imagepolygon($this->image, $xy, count($xy) / 2, $this->strokecolor); |
105
|
|
|
} |
106
|
2 |
|
} |
107
|
|
|
|
108
|
|
|
public function drawMultipoint($multicoordinates) |
109
|
|
|
{ |
110
|
|
|
foreach ($multicoordinates as $coordinates) { |
111
|
|
|
$this->drawPoint($coordinates); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function drawMultilinestring($multicoordinates) |
116
|
|
|
{ |
117
|
|
|
foreach ($multicoordinates as $coordinates) { |
118
|
|
|
$this->drawLinestring($coordinates); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function drawMultipolygon($multicoordinates) |
123
|
|
|
{ |
124
|
|
|
foreach ($multicoordinates as $coordinates) { |
125
|
|
|
$this->drawPolygon($coordinates); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
4 |
|
public function getImageBlob() |
130
|
|
|
{ |
131
|
4 |
|
ob_start(); |
132
|
4 |
|
imagepng($this->image); |
133
|
4 |
|
$blob = ob_get_clean(); |
134
|
|
|
|
135
|
4 |
|
return $blob; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|