DataTrait.php
2 years ago
FormatTrait.php
6 months ago
Picture.php
6 months ago
PointTrait.php
2 years ago
StyleTrait.php
2 years ago
Picture.php
132 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Davaxi\Sparkline; |
| 4 | |
| 5 | /** |
| 6 | * Class PictureTrait. |
| 7 | */ |
| 8 | class Picture |
| 9 | { |
| 10 | const DOT_RADIUS_TO_WIDTH = 2; |
| 11 | /** |
| 12 | * @var resource |
| 13 | */ |
| 14 | protected $resource; |
| 15 | /** |
| 16 | * @var int |
| 17 | */ |
| 18 | protected $height; |
| 19 | /** |
| 20 | * @var int |
| 21 | */ |
| 22 | protected $width; |
| 23 | /** |
| 24 | * Picture constructor. |
| 25 | * @param int $width |
| 26 | * @param int $height |
| 27 | */ |
| 28 | public function __construct(int $width, int $height) |
| 29 | { |
| 30 | $this->width = $width; |
| 31 | $this->height = $height; |
| 32 | $this->resource = imagecreatetruecolor($width, $height); |
| 33 | } |
| 34 | /** |
| 35 | * @param array $setColor |
| 36 | * |
| 37 | * @return int |
| 38 | */ |
| 39 | protected function getBackground(array $setColor = []) : int |
| 40 | { |
| 41 | if ($setColor) { |
| 42 | return imagecolorallocate($this->resource, $setColor[0], $setColor[1], $setColor[2]); |
| 43 | } |
| 44 | return imagecolorallocatealpha($this->resource, 0, 0, 0, 127); |
| 45 | } |
| 46 | /** |
| 47 | * @param array $lineColor |
| 48 | * |
| 49 | * @return int |
| 50 | */ |
| 51 | public function getLineColor(array $lineColor) : int |
| 52 | { |
| 53 | return imagecolorallocate($this->resource, $lineColor[0], $lineColor[1], $lineColor[2]); |
| 54 | } |
| 55 | /** |
| 56 | * @param array $backgroundColor |
| 57 | */ |
| 58 | public function applyBackground(array $backgroundColor) |
| 59 | { |
| 60 | imagesavealpha($this->resource, \true); |
| 61 | imagefill($this->resource, 0, 0, $this->getBackground($backgroundColor)); |
| 62 | } |
| 63 | /** |
| 64 | * @param int $lineThickness |
| 65 | */ |
| 66 | public function applyThickness(int $lineThickness) |
| 67 | { |
| 68 | imagesetthickness($this->resource, $lineThickness); |
| 69 | } |
| 70 | /** |
| 71 | * @param array $polygon |
| 72 | * @param array $fillColor |
| 73 | * @param int $count |
| 74 | */ |
| 75 | public function applyPolygon(array $polygon, array $fillColor, int $count) |
| 76 | { |
| 77 | if (!$fillColor) { |
| 78 | return; |
| 79 | } |
| 80 | $fillColor = imagecolorallocate($this->resource, $fillColor[0], $fillColor[1], $fillColor[2]); |
| 81 | if (version_compare(\PHP_VERSION, '8.1.0') === -1) { |
| 82 | imagefilledpolygon($this->resource, $polygon, $count + 2, $fillColor); |
| 83 | } else { |
| 84 | imagefilledpolygon($this->resource, $polygon, $fillColor); |
| 85 | } |
| 86 | } |
| 87 | /** |
| 88 | * @param array $line |
| 89 | * @param array $lineColor |
| 90 | */ |
| 91 | public function applyLine(array $line, array $lineColor) |
| 92 | { |
| 93 | $lineColor = $this->getLineColor($lineColor); |
| 94 | foreach ($line as $coordinates) { |
| 95 | list($pictureX1, $pictureY1, $pictureX2, $pictureY2) = $coordinates; |
| 96 | imageline($this->resource, $pictureX1, $pictureY1, $pictureX2, $pictureY2, $lineColor); |
| 97 | } |
| 98 | } |
| 99 | /** |
| 100 | * @param int $positionX |
| 101 | * @param int $positionY |
| 102 | * @param float $radius |
| 103 | * @param array $color |
| 104 | */ |
| 105 | public function applyDot(int $positionX, int $positionY, float $radius, array $color) |
| 106 | { |
| 107 | if (!$color || !$radius) { |
| 108 | return; |
| 109 | } |
| 110 | $minimumColor = imagecolorallocate($this->resource, $color[0], $color[1], $color[2]); |
| 111 | $dotDiameter = (int) round($radius * static::DOT_RADIUS_TO_WIDTH); |
| 112 | imagefilledellipse($this->resource, $positionX, $positionY, $dotDiameter, $dotDiameter, $minimumColor); |
| 113 | } |
| 114 | /** |
| 115 | * @param int $width |
| 116 | * @param int $height |
| 117 | * |
| 118 | * @return resource |
| 119 | */ |
| 120 | public function generate(int $width, int $height) |
| 121 | { |
| 122 | $sparkline = imagecreatetruecolor($width, $height); |
| 123 | imagealphablending($sparkline, \false); |
| 124 | imagecopyresampled($sparkline, $this->resource, 0, 0, 0, 0, $width, $height, $this->width, $this->height); |
| 125 | imagesavealpha($sparkline, \true); |
| 126 | if (\PHP_VERSION < 80000) { |
| 127 | imagedestroy($this->resource); |
| 128 | } |
| 129 | return $sparkline; |
| 130 | } |
| 131 | } |
| 132 |