|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the php-phantomjs. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace JonnyW\PhantomJs\Http; |
|
11
|
|
|
|
|
12
|
|
|
use JonnyW\PhantomJs\Exception\NotWritableException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* PHP PhantomJs. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Jon Wenmoth <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class CaptureRequest extends AbstractRequest |
|
20
|
|
|
implements CaptureRequestInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Request type. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $type; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* File to save output. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $outputFile; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Rect top. |
|
38
|
|
|
* |
|
39
|
|
|
* @var int |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $rectTop; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Rect left. |
|
45
|
|
|
* |
|
46
|
|
|
* @var int |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $rectLeft; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Rect width. |
|
52
|
|
|
* |
|
53
|
|
|
* @var int |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $rectWidth; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Rect height. |
|
59
|
|
|
* |
|
60
|
|
|
* @var int |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $rectHeight; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Capture Format. |
|
66
|
|
|
* |
|
67
|
|
|
* @var string |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $format; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Capture Quality. |
|
73
|
|
|
* |
|
74
|
|
|
* @var int |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $quality; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Internal constructor. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $url (default: null) |
|
82
|
|
|
* @param string $method (default: RequestInterface::METHOD_GET) |
|
83
|
|
|
* @param int $timeout (default: 5000) |
|
84
|
|
|
* |
|
85
|
|
|
* @return \JonnyW\PhantomJs\Http\CaptureRequest |
|
|
|
|
|
|
86
|
|
|
*/ |
|
87
|
|
|
public function __construct($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000) |
|
88
|
|
|
{ |
|
89
|
|
|
parent::__construct($url, $method, $timeout); |
|
90
|
|
|
|
|
91
|
|
|
$this->rectTop = 0; |
|
92
|
|
|
$this->rectLeft = 0; |
|
93
|
|
|
$this->rectWidth = 0; |
|
94
|
|
|
$this->rectHeight = 0; |
|
95
|
|
|
$this->format = 'jpeg'; |
|
96
|
|
|
$this->quality = 75; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get request type. |
|
101
|
|
|
* |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getType() |
|
105
|
|
|
{ |
|
106
|
|
|
if (!$this->type) { |
|
107
|
|
|
return RequestInterface::REQUEST_TYPE_CAPTURE; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $this->type; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Set request type. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $type |
|
117
|
|
|
* |
|
118
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
|
119
|
|
|
*/ |
|
120
|
|
|
public function setType($type) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->type = $type; |
|
123
|
|
|
|
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Set viewport size. |
|
129
|
|
|
* |
|
130
|
|
|
* @param int $width |
|
131
|
|
|
* @param int $height |
|
132
|
|
|
* @param int $top (default: 0) |
|
133
|
|
|
* @param int $left (default: 0) |
|
134
|
|
|
* |
|
135
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
|
136
|
|
|
*/ |
|
137
|
|
|
public function setCaptureDimensions($width, $height, $top = 0, $left = 0) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->rectWidth = (int) $width; |
|
140
|
|
|
$this->rectHeight = (int) $height; |
|
141
|
|
|
$this->rectTop = (int) $top; |
|
142
|
|
|
$this->rectLeft = (int) $left; |
|
143
|
|
|
|
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Get rect top. |
|
149
|
|
|
* |
|
150
|
|
|
* @return int |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getRectTop() |
|
153
|
|
|
{ |
|
154
|
|
|
return (int) $this->rectTop; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Get rect left. |
|
159
|
|
|
* |
|
160
|
|
|
* @return int |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getRectLeft() |
|
163
|
|
|
{ |
|
164
|
|
|
return (int) $this->rectLeft; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get rect width. |
|
169
|
|
|
* |
|
170
|
|
|
* @return int |
|
171
|
|
|
*/ |
|
172
|
|
|
public function getRectWidth() |
|
173
|
|
|
{ |
|
174
|
|
|
return (int) $this->rectWidth; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Get rect height. |
|
179
|
|
|
* |
|
180
|
|
|
* @return int |
|
181
|
|
|
*/ |
|
182
|
|
|
public function getRectHeight() |
|
183
|
|
|
{ |
|
184
|
|
|
return (int) $this->rectHeight; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Set file to save output. |
|
189
|
|
|
* |
|
190
|
|
|
* @param string $file |
|
191
|
|
|
* |
|
192
|
|
|
* @throws \JonnyW\PhantomJs\Exception\NotWritableException |
|
193
|
|
|
* |
|
194
|
|
|
* @return \JonnyW\PhantomJs\Http\CaptureRequest |
|
195
|
|
|
*/ |
|
196
|
|
|
public function setOutputFile($file) |
|
197
|
|
|
{ |
|
198
|
|
|
if (!is_writable(dirname($file))) { |
|
199
|
|
|
throw new NotWritableException(sprintf('Output file is not writeable by PhantomJs: %s', $file)); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$this->outputFile = $file; |
|
203
|
|
|
|
|
204
|
|
|
return $this; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Get output file. |
|
209
|
|
|
* |
|
210
|
|
|
* @return string |
|
211
|
|
|
*/ |
|
212
|
|
|
public function getOutputFile() |
|
213
|
|
|
{ |
|
214
|
|
|
return $this->outputFile; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Get image format of the capture. |
|
219
|
|
|
* |
|
220
|
|
|
* @return string |
|
221
|
|
|
*/ |
|
222
|
|
|
public function getFormat() |
|
223
|
|
|
{ |
|
224
|
|
|
return $this->format; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Set image format of capture. |
|
229
|
|
|
* options: pdf, png, jpeg, bmp, ppm, gif. |
|
230
|
|
|
* |
|
231
|
|
|
* @param string $format |
|
232
|
|
|
*/ |
|
233
|
|
|
public function setFormat($format) |
|
234
|
|
|
{ |
|
235
|
|
|
$this->format = $format; |
|
236
|
|
|
|
|
237
|
|
|
return $this; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Get quality of capture. |
|
242
|
|
|
* |
|
243
|
|
|
* @return string |
|
244
|
|
|
*/ |
|
245
|
|
|
public function getQuality() |
|
246
|
|
|
{ |
|
247
|
|
|
return $this->quality; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Set quality of the capture. |
|
252
|
|
|
* example: 0 - 100. |
|
253
|
|
|
* |
|
254
|
|
|
* @param int $format |
|
|
|
|
|
|
255
|
|
|
*/ |
|
256
|
|
|
public function setQuality($quality) |
|
257
|
|
|
{ |
|
258
|
|
|
$this->quality = (int) $quality; |
|
259
|
|
|
|
|
260
|
|
|
return $this; |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.