|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AhmadMayahi\Vision\Support; |
|
6
|
|
|
|
|
7
|
|
|
use AhmadMayahi\Vision\Exceptions\FileException; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
use GdImage; |
|
10
|
|
|
|
|
11
|
|
|
class Image |
|
12
|
|
|
{ |
|
13
|
|
|
protected GdImage $gdImage; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(File $file) |
|
16
|
|
|
{ |
|
17
|
|
|
$contents = $file->getContents(); |
|
18
|
|
|
|
|
19
|
|
|
if (! $contents) { |
|
20
|
|
|
throw new FileException('Invalid file!'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
$this->gdImage = imagecreatefromstring($contents); |
|
24
|
|
|
|
|
25
|
|
|
return $this; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getImage(): GdImage |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->gdImage; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getWidth(): int |
|
34
|
|
|
{ |
|
35
|
|
|
return imagesx($this->gdImage); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getHeight(): int |
|
39
|
|
|
{ |
|
40
|
|
|
return imagesy($this->gdImage); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function drawRectangle(int $x1, int $y1, int $x2, int $y2, int $color): void |
|
44
|
|
|
{ |
|
45
|
|
|
imagerectangle($this->gdImage, $x1, $y1, $x2, $y2, $color); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function cropImage($x, $y, $width, $height) |
|
49
|
|
|
{ |
|
50
|
|
|
$crop = imagecrop($this->gdImage, ['x' => $x, 'y' => $y, 'width' => $width, 'height' => $height]); |
|
51
|
|
|
|
|
52
|
|
|
if (false === $crop) { |
|
53
|
|
|
throw new Exception('Could not crop the image'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->gdImage = $crop; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function drawPolygon(array $points, $numberOfPoints, $color) |
|
60
|
|
|
{ |
|
61
|
|
|
imagepolygon($this->gdImage, $points, $numberOfPoints, $color); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function writeText(string $text, string $fontFile, int $color, int $fontSize, int $x, int $y) |
|
65
|
|
|
{ |
|
66
|
|
|
imagettftext( |
|
67
|
|
|
$this->gdImage, |
|
68
|
|
|
$fontSize, |
|
69
|
|
|
0, |
|
70
|
|
|
$x, |
|
71
|
|
|
$y, |
|
72
|
|
|
$color, |
|
73
|
|
|
$this->getFontPath($fontFile), |
|
74
|
|
|
$text, |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function toJpeg(string $outputFileName): bool |
|
79
|
|
|
{ |
|
80
|
|
|
return imagejpeg($this->gdImage, $outputFileName); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function toPng(string $outputFileName): bool |
|
84
|
|
|
{ |
|
85
|
|
|
return imagepng($this->gdImage, $outputFileName); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function toGif(string $outputFileName): bool |
|
89
|
|
|
{ |
|
90
|
|
|
return imagegif($this->gdImage, $outputFileName); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function toBmp(string $outputFileName): bool |
|
94
|
|
|
{ |
|
95
|
|
|
return imagebmp($this->gdImage, $outputFileName); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function getFontPath(string $font): string |
|
99
|
|
|
{ |
|
100
|
|
|
return dirname(__DIR__, 2) . '/fonts/' . $font; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|