Polygon.php
1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace Grafika\Gd\DrawingObject;
use Grafika\DrawingObject\Polygon as Base;
use Grafika\DrawingObjectInterface;
use Grafika\Gd\Editor;
/**
* Class Rectangle
* @package Grafika
*/
class Polygon extends Base implements DrawingObjectInterface
{
public function draw($image)
{
if(function_exists('imageantialias')){
imageantialias($image->getCore(), true);
}
$points = $this->points();
$count = count($this->points);
// Create filled polygon
if( null !== $this->fillColor){
list($r, $g, $b, $alpha) = $this->getFillColor()->getRgba();
$fillColorResource = imagecolorallocatealpha(
$image->getCore(), $r, $g, $b,
Editor::gdAlpha($alpha)
);
imagefilledpolygon($image->getCore(), $points,
$count,
$fillColorResource
);
}
// Create polygon borders. It will be placed on top of the filled polygon (if present)
if ( 0 < $this->getBorderSize() and null !== $this->borderColor) { // With border > 0 AND borderColor !== null
list($r, $g, $b, $alpha) = $this->getBorderColor()->getRgba();
$borderColorResource = imagecolorallocatealpha(
$image->getCore(), $r, $g, $b,
Editor::gdAlpha($alpha)
);
imagepolygon($image->getCore(), $points,
$count,
$borderColorResource
);
}
return $image;
}
protected function points(){
$points = array();
foreach($this->points as $point){
$points[] = $point[0];
$points[] = $point[1];
}
if( count($points) < 6 ){
throw new \Exception('Polygon needs at least 3 points.');
}
return $points;
}
}