-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Image.php
196 lines (166 loc) · 4.48 KB
/
Image.php
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
namespace SimonHamp\TheOg;
use Intervention\Image\Colors\Rgb\Color;
use Intervention\Image\Encoders\PngEncoder;
use Intervention\Image\Image as RenderedImage;
use Intervention\Image\Interfaces\EncoderInterface;
use SimonHamp\TheOg\Background as BuiltInBackground;
use SimonHamp\TheOg\Interfaces\Background;
use SimonHamp\TheOg\Interfaces\Layout;
use SimonHamp\TheOg\Interfaces\Theme;
use SimonHamp\TheOg\Layout\Layouts\Standard;
use SimonHamp\TheOg\Theme as BuiltInTheme;
use SimonHamp\TheOg\Theme\BackgroundPlacement;
use SimonHamp\TheOg\Theme\Picture;
class Image
{
public Layout $layout;
public Theme $theme;
public readonly string $callToAction;
public readonly string $description;
public readonly Picture $picture;
public readonly string $title;
public readonly string $url;
public readonly Picture $watermark;
public function __construct()
{
$this->layout(new Standard);
$this->theme(BuiltInTheme::Light);
}
/**
* The call to action text
*/
public function callToAction(string $content): self
{
$this->callToAction = $content;
return $this;
}
/**
* The description text
*/
public function description(string $description): self
{
$this->description = $description;
return $this;
}
/**
* The picture to display
*/
public function picture(string|Picture $picture): self
{
if (is_string($picture)) {
$picture = new Picture($picture);
}
$this->picture = $picture;
return $this;
}
/**
* The title text
*/
public function title(string $title): self
{
$this->title = $title;
return $this;
}
/**
* The URL
*/
public function url(string $url): self
{
$this->url = $url;
return $this;
}
/**
* The watermark image
*/
public function watermark(string|Picture $watermark): self
{
if (is_string($watermark)) {
$watermark = new Picture($watermark);
}
$this->watermark = $watermark;
return $this;
}
/**
* The layout to use
*/
public function layout(Layout $layout): self
{
$this->layout = $layout;
return $this;
}
/**
* The theme to use
*/
public function theme(Theme|BuiltInTheme $theme): self
{
if ($theme instanceof BuiltInTheme) {
$theme = $theme->load();
}
$this->theme = $theme;
return $this;
}
/**
* Override the theme's default accent color
*/
public function accentColor(string $color): self
{
$this->theme->accentColor($color);
return $this;
}
/**
* Override the theme's default background
*/
public function background(Background|BuiltInBackground $background, ?float $opacity = null, ?BackgroundPlacement $placement = null): self
{
if ($background instanceof BuiltInBackground) {
$background = $background->load();
}
if (isset($opacity)) {
$background->setOpacity($opacity);
}
if (isset($placement)) {
$background->setPlacement($placement);
}
$this->theme->background($background);
return $this;
}
/**
* Override the theme's default background color
*/
public function backgroundColor(string $backgroundColor): self
{
$this->theme->backgroundColor($backgroundColor);
return $this;
}
/**
* Override the layout's default border
*/
public function border(?BorderPosition $position = null, ?Color $color = null, ?int $width = null): self
{
$this->layout->border(
(new Border())
->position($position ?? $this->layout->getBorderPosition())
->color($color ?? $this->theme->getBorderColor())
->width($width ?? $this->layout->getBorderWidth())
);
return $this;
}
public function render(): RenderedImage
{
return $this->layout->render($this);
}
public function save(string $path, EncoderInterface $encoder = new PngEncoder()): self
{
$this->render()
->encode($encoder)
->save($path);
return $this;
}
public function toString(EncoderInterface $encoder = new PngEncoder): string
{
return $this->render()
->encode($encoder)
->toString();
}
}