Skip to content

Commit 22207e7

Browse files
committed
Adding drawPolygon method
1 parent 8fbb3c4 commit 22207e7

File tree

4 files changed

+97
-33
lines changed

4 files changed

+97
-33
lines changed

docs/classes/DantSu/PHPImageEditor/Image.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ DantSu\PHPImageEditor\Image is PHP library to easily edit image with GD extensio
5959
- [writeText](#-writetext)
6060
- [writeTextAndGetBoundingBox](#-writetextandgetboundingbox)
6161
- [drawRectangle](#-drawrectangle)
62+
- [drawPolygon](#-drawpolygon)
6263
- [drawLine](#-drawline)
6364
- [drawLineWithAngle](#-drawlinewithangle)
6465
- [drawArrowWithAngle](#-drawarrowwithangle)
@@ -894,6 +895,33 @@ Draw a rectangle.
894895

895896

896897

898+
---
899+
### ->drawPolygon
900+
901+
Draw a polygon.
902+
903+
904+
905+
906+
907+
908+
909+
910+
#### Parameters:
911+
912+
| Parameter | Type | Description |
913+
|-----------|------|-------------|
914+
| `points` | **int[]** | Array of polygon's points [x1, y1, x2, y2, x3, y3...] |
915+
| `color` | **string** | Hexadecimal string color |
916+
| `antialias` | **mixed** | |
917+
918+
919+
#### Return Value:
920+
921+
**$this** : Fluent interface
922+
923+
924+
897925
---
898926
### ->drawLine
899927

@@ -1376,4 +1404,4 @@ Get image GIF base64 data for <img src=""> tag.
13761404

13771405

13781406
---
1379-
> Automatically generated from source code comments on 2022-05-11 using [phpDocumentor](http://www.phpdoc.org/)
1407+
> Automatically generated from source code comments on 2022-05-24 using [phpDocumentor](http://www.phpdoc.org/)

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ This is an automatically generated documentation for **PHP Image Editor**.
1919

2020

2121
---
22-
> Automatically generated from source code comments on 2022-05-11 using [phpDocumentor](http://www.phpdoc.org/)
22+
> Automatically generated from source code comments on 2022-05-24 using [phpDocumentor](http://www.phpdoc.org/)

src/Image.php

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
* @access public
1111
* @see https://github.com/DantSu/php-image-editor Github page of this project
1212
*/
13-
1413
class Image
1514
{
1615
const ALIGN_LEFT = 'left';
@@ -664,10 +663,10 @@ public function pasteOn(Image $image, $posX = Image::ALIGN_CENTER, $posY = Image
664663
if (!$this->isImageDefined() || !$image->isImageDefined()) {
665664
return $this;
666665
}
667-
666+
668667
return $this->pasteGdImageOn($image->getImage(), $image->getWidth(), $image->getHeight(), $posX, $posY);
669668
}
670-
669+
671670
/**
672671
* Paste the image at $posX and $posY position (You can use `Image::ALIGN_...`).
673672
*
@@ -683,7 +682,7 @@ public function pasteGdImageOn($image, int $imageWidth, int $imageHeight, $posX
683682
if (!$this->isImageDefined() || !static::isGdImage($image)) {
684683
return $this;
685684
}
686-
685+
687686
$posX = $this->convertPosX($posX, $imageWidth);
688687
$posY = $this->convertPosY($posY, $imageHeight);
689688

@@ -939,6 +938,33 @@ public function drawRectangle(int $left, int $top, int $right, int $bottom, stri
939938
return $this;
940939
}
941940

941+
/**
942+
* Draw a polygon.
943+
*
944+
* @param int[] $points Array of polygon's points [x1, y1, x2, y2, x3, y3...]
945+
* @param string $color Hexadecimal string color
946+
* @return $this Fluent interface
947+
*/
948+
public function drawPolygon(array $points, string $color = '#000000', $antialias = false): Image
949+
{
950+
if (!$this->isImageDefined()) {
951+
return $this;
952+
}
953+
954+
$color = $this->colorAllocate($color);
955+
956+
if ($color === false) {
957+
return $this;
958+
}
959+
960+
if($antialias) {
961+
\imageantialias($this->image, true);
962+
\imagepolygon($this->image, $points, \count($points) / 2, $color);
963+
}
964+
\imagefilledpolygon($this->image, $points, \count($points) / 2, $color);
965+
966+
return $this;
967+
}
942968

943969
/**
944970
* Draw a Line from `$originX, $originY` to `$dstX, $dstY`.
@@ -974,39 +1000,27 @@ public function drawLine(int $originX, int $originY, int $dstX, int $dstY, int $
9741000
*/
9751001
public function drawLineWithAngle(int $originX, int $originY, float $angle, float $length, int $weight, string $color = '#000000'): Image
9761002
{
977-
if (!$this->isImageDefined()) {
978-
return $this;
979-
}
980-
981-
$color = $this->colorAllocate($color);
982-
983-
if ($color === false) {
984-
return $this;
985-
}
986-
9871003
$angle = Geometry2D::degrees0to360($angle);
9881004

9891005
$points1 = Geometry2D::getDstXY($originX, $originY, Geometry2D::degrees0to360($angle - 90), \floor($weight / 2));
9901006
$points2 = Geometry2D::getDstXY($points1['x'], $points1['y'], $angle, $length);
9911007
$points4 = Geometry2D::getDstXY($originX, $originY, Geometry2D::degrees0to360($angle + 90), \floor($weight / 2));
9921008
$points3 = Geometry2D::getDstXY($points4['x'], $points4['y'], $angle, $length);
9931009

994-
$points = [
995-
\round($points1['x']),
996-
\round($points1['y']),
997-
\round($points2['x']),
998-
\round($points2['y']),
999-
\round($points3['x']),
1000-
\round($points3['y']),
1001-
\round($points4['x']),
1002-
\round($points4['y'])
1003-
];
1004-
1005-
\imageantialias($this->image, true);
1006-
\imagepolygon($this->image, $points, 4, $color);
1007-
\imagefilledpolygon($this->image, $points, 4, $color);
1008-
1009-
return $this;
1010+
return $this->drawPolygon(
1011+
[
1012+
\round($points1['x']),
1013+
\round($points1['y']),
1014+
\round($points2['x']),
1015+
\round($points2['y']),
1016+
\round($points3['x']),
1017+
\round($points3['y']),
1018+
\round($points4['x']),
1019+
\round($points4['y'])
1020+
],
1021+
$color,
1022+
true
1023+
);
10101024
}
10111025

10121026
/**
@@ -1020,7 +1034,7 @@ public function drawLineWithAngle(int $originX, int $originY, float $angle, floa
10201034
* @param string $color Hexadecimal string color
10211035
* @return $this Fluent interface
10221036
*/
1023-
public function drawArrowWithAngle(int $originX, int $originY, float $angle, float $length, int $weight, string $color = '#000000'): Image
1037+
public function drawArrowWithAngle(int $originX, int $originY, float $angle, float $length, int $weight, string $color = '#000000'): Image
10241038
{
10251039
if (!$this->isImageDefined()) {
10261040
return $this;

src/samples/sample4.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
4+
require_once '../Geometry2D.php';
5+
require_once '../Image.php';
6+
7+
use \DantSu\PHPImageEditor\Image;
8+
9+
\header('Content-type: image/png');
10+
11+
Image::fromPath(__DIR__ . '/resources/photo.jpg')
12+
->downscaleAndCrop(1920, 1080, Image::ALIGN_CENTER, Image::ALIGN_BOTTOM)
13+
->drawPolygon([110, 500, 240, 250, 400, 140, 650, 280, 400, 400, 800, 510, 400, 950, 180, 620, 230, 540], '#88229988')
14+
->drawCircle(450, 600, 200, '#FFFFFF88')
15+
->pasteOn(
16+
Image::newCanvas(1920, 1080)
17+
->drawPolygon([1110, 500, 1240, 250, 1400, 140, 1650, 280, 1400, 400, 1800, 510, 1400, 950, 1180, 620, 1230, 540], '#88229988')
18+
->drawCircle(1450, 600, 200, '#FFFFFF88'),
19+
0,
20+
0
21+
)
22+
->displayPNG();

0 commit comments

Comments
 (0)