@@ -42,25 +42,32 @@ class Target
42
42
* @var int the number of ring
43
43
*/
44
44
private $ ringCount ;
45
-
46
- /**
47
- * Target constructor.
48
- *
49
- * @param float $diameter10
50
- * @param float|null $diameterInner10
51
- * @param float $ringSpacing
52
- * @param int $firstInnerRing
53
- * @param int $ringCount
54
- * @param Hit[] $hits
55
- */
56
- public function __construct ($ diameter10 = 0.5 , $ diameterInner10 = 0.5 , $ ringSpacing = 2.5 , $ firstInnerRing = 4 , $ ringCount = 10 , $ hits = [])
45
+
46
+ /**
47
+ * @var array some options like color settings
48
+ */
49
+ private $ options ;
50
+
51
+ /**
52
+ * Target constructor.
53
+ *
54
+ * @param float $diameter10
55
+ * @param float|null $diameterInner10
56
+ * @param float $ringSpacing
57
+ * @param int $firstInnerRing
58
+ * @param int $ringCount
59
+ * @param Hit[] $hits
60
+ * @param array $options
61
+ */
62
+ public function __construct ($ diameter10 = 0.5 , $ diameterInner10 = 0.5 , $ ringSpacing = 2.5 , $ firstInnerRing = 4 , $ ringCount = 10 , $ hits = [], $ options = [])
57
63
{
58
64
$ this ->diameter10 = $ diameter10 ;
59
65
$ this ->diameterInner10 = $ diameterInner10 ;
60
66
$ this ->ringSpacing = $ ringSpacing ;
61
67
$ this ->firstInnerRing = $ firstInnerRing ;
62
68
$ this ->ringCount = $ ringCount ;
63
69
$ this ->setHits ($ hits );
70
+ $ this ->setOptions ($ options );
64
71
}
65
72
66
73
/**
@@ -87,7 +94,7 @@ public function __construct($diameter10 = 0.5, $diameterInner10 = 0.5, $ringSpac
87
94
*/
88
95
public function draw ($ unit = 20 , $ type = self ::DRAW_TYPE_PNG , $ font = 5 , $ filename = null , $ quality = null , $ filters = null )
89
96
{
90
- $ size = (( $ this ->diameter10 / 2 ) + (( $ this -> ringCount - 1 ) * $ this -> ringSpacing )) * 2 * $ unit ;
97
+ $ size = $ this ->getTargetSize () * $ unit ;
91
98
$ image = imagecreatetruecolor ($ size , $ size );
92
99
93
100
$ transparent = imagecolorallocatealpha ($ image , 0 , 0 , 0 , 127 );
@@ -98,18 +105,18 @@ public function draw($unit = 20, $type = self::DRAW_TYPE_PNG, $font = 5, $filena
98
105
$ white = imagecolorallocate ($ image , 255 , 255 , 255 );
99
106
$ red = imagecolorallocate ($ image , 255 , 0 , 0 );
100
107
101
- /*
108
+ /**
102
109
* Rings
103
110
*/
104
111
for ($ x = $ this ->ringCount - 1 ; $ x >= 0 ; $ x --) {
105
112
$ diameter = (($ x * $ this ->ringSpacing * 2 ) + $ this ->diameter10 ) * $ unit ;
106
113
107
- imagefilledellipse ($ image , $ size / 2 , $ size / 2 , $ diameter , $ diameter , $ x > ($ this ->ringCount - $ this ->firstInnerRing ) ? $ black : $ white );
108
- imagefilledellipse ($ image , $ size / 2 , $ size / 2 , $ diameter - 3 , $ diameter - 3 , $ x > ($ this ->ringCount - $ this ->firstInnerRing ) ? $ white : $ black );
114
+ imagefilledellipse ($ image , $ size / 2 , $ size / 2 , $ diameter , $ diameter , $ x > ($ this ->ringCount - $ this ->firstInnerRing ) ? $ this -> hexColorAllocate ( $ image , $ this -> getOption ( ' outer_ring_border_color ' )) : $ this -> hexColorAllocate ( $ image , $ this -> getOption ( ' inner_ring_border_color ' )) );
115
+ imagefilledellipse ($ image , $ size / 2 , $ size / 2 , $ diameter - 3 , $ diameter - 3 , $ x > ($ this ->ringCount - $ this ->firstInnerRing ) ? $ this -> hexColorAllocate ( $ image , $ this -> getOption ( ' outer_ring_color ' )) : $ this -> hexColorAllocate ( $ image , $ this -> getOption ( ' inner_ring_color ' )) );
109
116
110
117
if ($ this ->ringCount - $ x < 9 ) {
111
118
$ text = $ this ->ringCount - $ x ;
112
- $ color = $ x > ($ this ->ringCount - $ this ->firstInnerRing ) ? $ black : $ white ;
119
+ $ color = $ x > ($ this ->ringCount - $ this ->firstInnerRing ) ? $ this -> hexColorAllocate ( $ image , $ this -> getOption ( ' outer_ring_text_color ' )) : $ this -> hexColorAllocate ( $ image , $ this -> getOption ( ' inner_ring_text_color ' )) ;
113
120
114
121
if (is_numeric ($ font )) {
115
122
$ width = (imagefontwidth ($ font ) * strlen ($ text )) / 2 ;
@@ -144,20 +151,19 @@ public function draw($unit = 20, $type = self::DRAW_TYPE_PNG, $font = 5, $filena
144
151
* Inner 10.
145
152
*/
146
153
$ diameter = ($ this ->diameterInner10 / 2 ) * 2 * $ unit ;
147
- imagefilledellipse ($ image , $ size / 2 , $ size / 2 , $ diameter , $ diameter , $ white );
154
+ imagefilledellipse ($ image , $ size / 2 , $ size / 2 , $ diameter , $ diameter , $ this -> hexColorAllocate ( $ image , $ this -> getOption ( ' inner_ring_border_color ' )) );
148
155
if (($ this ->diameterInner10 / 2 ) * 2 >= 3 ) {
149
- imagefilledellipse ($ image , $ size / 2 , $ size / 2 , $ diameter - 2 , $ diameter - 2 , $ black );
156
+ imagefilledellipse ($ image , $ size / 2 , $ size / 2 , $ diameter - 2 , $ diameter - 2 , $ this -> hexColorAllocate ( $ image , $ this -> getOption ( ' inner_ring_color ' )) );
150
157
}
151
158
152
159
foreach ($ this ->hits as $ number => $ hit ) {
153
160
$ x = $ hit ->getX () * 0.01 * $ unit ;
154
161
$ y = $ hit ->getY () * 0.01 * $ unit ;
155
162
$ text = $ hit ->getLabel () ?: ($ number + 1 );
156
- $ color = $ hit ->getColor () !== null ? $ this ->hexColorAllocate ($ image , $ hit ->getColor ()) : $ red ;
157
- $ rgbColor = $ this ->hexColorToRGB ($ hit ->getColor ());
158
- $ borderColor = $ white ;
163
+ $ color = $ hit ->getColor () !== null ? $ this ->hexColorAllocate ($ image , $ hit ->getColor ()) : $ this ->hexColorAllocate ($ image , $ this ->getOption ('hit_color ' ));
164
+ $ rgbColor = $ this ->hexColorToRGB ($ color );
159
165
160
- imagefilledellipse ($ image , $ size / 2 + $ x , $ size / 2 - $ y , 4.5 * $ unit , 4.5 * $ unit , $ borderColor );
166
+ imagefilledellipse ($ image , $ size / 2 + $ x , $ size / 2 - $ y , 4.5 * $ unit , 4.5 * $ unit , $ this -> hexColorAllocate ( $ image , $ this -> getOption ( ' hit_border_color ' )) );
161
167
imagefilledellipse ($ image , $ size / 2 + $ x , $ size / 2 - $ y , 4.5 * $ unit - 3 , 4.5 * $ unit - 3 , $ color );
162
168
163
169
if (is_numeric ($ font )) {
@@ -182,6 +188,18 @@ public function draw($unit = 20, $type = self::DRAW_TYPE_PNG, $font = 5, $filena
182
188
183
189
return imagepng ($ image , $ filename , $ quality , $ filters );
184
190
}
191
+
192
+ /**
193
+ * Get the target size in mm
194
+ *
195
+ * @param bool $asDiameter return the diameter if true else the whole size
196
+ *
197
+ * @return float
198
+ */
199
+ public function getTargetSize ($ asDiameter = false )
200
+ {
201
+ return (($ this ->diameter10 / 2 ) + (($ this ->ringCount - 1 ) * $ this ->ringSpacing )) * ($ asDiameter ? 1 : 2 );
202
+ }
185
203
186
204
/**
187
205
* Get the hits array.
@@ -259,4 +277,52 @@ private function hexColorToRGB($hex)
259
277
$ a + $ b + $ c ,
260
278
];
261
279
}
280
+
281
+ /**
282
+ * @return array
283
+ */
284
+ public function getOptions ()
285
+ {
286
+ return $ this ->options ;
287
+ }
288
+
289
+ /**
290
+ * Get an option
291
+ *
292
+ * @param string $key the key to get
293
+ * @param mixed $default the default value
294
+ *
295
+ * @return mixed if key exist then this or else the default value
296
+ */
297
+ public function getOption ($ key , $ default = null )
298
+ {
299
+ $ key = strtolower ($ key );
300
+ return array_key_exists ($ key , $ this ->options ) ? $ this ->options [$ key ] : $ default ;
301
+ }
302
+
303
+ /**
304
+ * @param array $options
305
+ */
306
+ public function setOptions ($ options )
307
+ {
308
+ $ this ->options = array_merge ($ this ->getDefaultOptions (), $ options );
309
+ }
310
+
311
+ /**
312
+ * @return array default options
313
+ */
314
+ private function getDefaultOptions ()
315
+ {
316
+ return array (
317
+ 'outer_ring_color ' => '#fff ' ,
318
+ 'outer_ring_text_color ' => '#000 ' ,
319
+ 'outer_ring_border_color ' => '#000 ' ,
320
+ 'inner_ring_color ' => '#000 ' ,
321
+ 'inner_ring_text_color ' => '#fff ' ,
322
+ 'inner_ring_border_color ' => '#fff ' ,
323
+ 'ring_texts ' => true ,
324
+ 'hit_color ' => '#ff0000 ' ,
325
+ 'hit_border_color ' => '#fff '
326
+ );
327
+ }
262
328
}
0 commit comments