|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tohidhabiby\HtmlToImage; |
|
4
|
|
|
|
|
5
|
|
|
class HtmlToImage implements HtmlToImageInterface |
|
6
|
|
|
{ |
|
7
|
|
|
protected $url; |
|
8
|
|
|
protected $path; |
|
9
|
|
|
protected $shellExec; |
|
10
|
|
|
protected $height; |
|
11
|
|
|
protected $width; |
|
12
|
|
|
protected $coordinateX; |
|
13
|
|
|
protected $coordinateY; |
|
14
|
|
|
protected $command = 'wkhtmltoimage'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* HtmlToImage constructor. |
|
18
|
|
|
* @param null|ShellExec $shellExec |
|
19
|
|
|
* @param string|null $command |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(?ShellExec $shellExec = null, ?string $command = null) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->shellExec = $shellExec ?? new ShellExec; |
|
24
|
|
|
if (!empty($command)) { |
|
25
|
|
|
$this->command = $command; |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* set html url |
|
31
|
|
|
* @param string $url |
|
32
|
|
|
* @return HtmlToImageInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
public function url(string $url): HtmlToImageInterface |
|
35
|
|
|
{ |
|
36
|
|
|
$this->url = '\'' . $url . '\''; |
|
37
|
|
|
|
|
38
|
|
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* get html url |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getUrl(): string |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->url; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* set html path |
|
52
|
|
|
* @param string $path |
|
53
|
|
|
* @return HtmlToImageInterface |
|
54
|
|
|
*/ |
|
55
|
|
|
public function path(string $path): HtmlToImageInterface |
|
56
|
|
|
{ |
|
57
|
|
|
$this->path = '\'' . $path . '\''; |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* get html path |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getPath(): string |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->path; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* generate image |
|
73
|
|
|
* |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
public function generate(): string |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->shellExec->exec($this->getCommand()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* get bash command |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getCommand() : string |
|
87
|
|
|
{ |
|
88
|
|
|
return sprintf( |
|
89
|
|
|
'%s%s%s%s%s %s %s', |
|
90
|
|
|
$this->command, |
|
91
|
|
|
$this->getCropHeight() ? sprintf(' --crop-h %s', $this->height) : '', |
|
92
|
|
|
$this->getCropWidth() ? sprintf(' --crop-w %s', $this->width) : '', |
|
93
|
|
|
$this->getCoordinateX() ? sprintf(' --crop-x %s', $this->coordinateX) : '', |
|
94
|
|
|
$this->getCoordinateY() ? sprintf(' --crop-y %s', $this->coordinateY) : '', |
|
95
|
|
|
$this->url, |
|
96
|
|
|
$this->path |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param int $value |
|
102
|
|
|
* @return HtmlToImageInterface |
|
103
|
|
|
*/ |
|
104
|
|
|
public function cropHeight(int $value) : HtmlToImageInterface |
|
105
|
|
|
{ |
|
106
|
|
|
$this->height = $value; |
|
107
|
|
|
|
|
108
|
|
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* get height crop |
|
113
|
|
|
* |
|
114
|
|
|
* @return int|null |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getCropHeight(): ?int |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->height; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* set width for cropping |
|
123
|
|
|
* |
|
124
|
|
|
* @param int $value |
|
125
|
|
|
* @return HtmlToImageInterface |
|
126
|
|
|
*/ |
|
127
|
|
|
public function cropWidth(int $value): HtmlToImageInterface |
|
128
|
|
|
{ |
|
129
|
|
|
$this->width = $value; |
|
130
|
|
|
|
|
131
|
|
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* get width crop |
|
136
|
|
|
* |
|
137
|
|
|
* @return int|null |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getCropWidth(): ?int |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->width; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* set X coordinate |
|
146
|
|
|
* |
|
147
|
|
|
* @param int $value |
|
148
|
|
|
* @return HtmlToImageInterface |
|
149
|
|
|
*/ |
|
150
|
|
|
public function coordinateX(int $value): HtmlToImageInterface |
|
151
|
|
|
{ |
|
152
|
|
|
$this->coordinateX = $value; |
|
153
|
|
|
|
|
154
|
|
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* get X coordinate |
|
159
|
|
|
* |
|
160
|
|
|
* @return int|null |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getCoordinateX(): ?int |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->coordinateX; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* set Y coordinate |
|
169
|
|
|
* |
|
170
|
|
|
* @param int $value |
|
171
|
|
|
* @return HtmlToImageInterface |
|
172
|
|
|
*/ |
|
173
|
|
|
public function coordinateY(int $value): HtmlToImageInterface |
|
174
|
|
|
{ |
|
175
|
|
|
$this->coordinateY = $value; |
|
176
|
|
|
|
|
177
|
|
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* get Y coordinate |
|
182
|
|
|
* |
|
183
|
|
|
* @return int|null |
|
184
|
|
|
*/ |
|
185
|
|
|
public function getCoordinateY(): ?int |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->coordinateY; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|