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
|
66 |
|
public function __construct($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000) |
88
|
|
|
{ |
89
|
66 |
|
parent::__construct($url, $method, $timeout); |
90
|
|
|
|
91
|
66 |
|
$this->rectTop = 0; |
92
|
66 |
|
$this->rectLeft = 0; |
93
|
66 |
|
$this->rectWidth = 0; |
94
|
66 |
|
$this->rectHeight = 0; |
95
|
66 |
|
$this->format = 'jpeg'; |
96
|
66 |
|
$this->quality = 75; |
97
|
66 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get request type. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
9 |
|
public function getType() |
105
|
|
|
{ |
106
|
9 |
|
if (!$this->type) { |
107
|
8 |
|
return RequestInterface::REQUEST_TYPE_CAPTURE; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return $this->type; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set request type. |
115
|
|
|
* |
116
|
|
|
* @param string $type |
117
|
|
|
* |
118
|
|
|
* @return \JonnyW\PhantomJs\Http\AbstractRequest |
119
|
|
|
*/ |
120
|
2 |
|
public function setType($type) |
121
|
|
|
{ |
122
|
2 |
|
$this->type = $type; |
123
|
|
|
|
124
|
2 |
|
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
|
5 |
|
public function setCaptureDimensions($width, $height, $top = 0, $left = 0) |
138
|
|
|
{ |
139
|
5 |
|
$this->rectWidth = (int) $width; |
140
|
5 |
|
$this->rectHeight = (int) $height; |
141
|
5 |
|
$this->rectTop = (int) $top; |
142
|
5 |
|
$this->rectLeft = (int) $left; |
143
|
|
|
|
144
|
5 |
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get rect top. |
149
|
|
|
* |
150
|
|
|
* @return int |
151
|
|
|
*/ |
152
|
14 |
|
public function getRectTop() |
153
|
|
|
{ |
154
|
14 |
|
return (int) $this->rectTop; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get rect left. |
159
|
|
|
* |
160
|
|
|
* @return int |
161
|
|
|
*/ |
162
|
14 |
|
public function getRectLeft() |
163
|
|
|
{ |
164
|
14 |
|
return (int) $this->rectLeft; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get rect width. |
169
|
|
|
* |
170
|
|
|
* @return int |
171
|
|
|
*/ |
172
|
14 |
|
public function getRectWidth() |
173
|
|
|
{ |
174
|
14 |
|
return (int) $this->rectWidth; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get rect height. |
179
|
|
|
* |
180
|
|
|
* @return int |
181
|
|
|
*/ |
182
|
14 |
|
public function getRectHeight() |
183
|
|
|
{ |
184
|
14 |
|
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
|
12 |
|
public function setOutputFile($file) |
197
|
|
|
{ |
198
|
12 |
|
if (!is_writable(dirname($file))) { |
199
|
1 |
|
throw new NotWritableException(sprintf('Output file is not writeable by PhantomJs: %s', $file)); |
200
|
|
|
} |
201
|
|
|
|
202
|
11 |
|
$this->outputFile = $file; |
203
|
|
|
|
204
|
11 |
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get output file. |
209
|
|
|
* |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
15 |
|
public function getOutputFile() |
213
|
|
|
{ |
214
|
15 |
|
return $this->outputFile; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get image format of the capture. |
219
|
|
|
* |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
7 |
|
public function getFormat() |
223
|
|
|
{ |
224
|
7 |
|
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
|
7 |
|
public function getQuality() |
246
|
|
|
{ |
247
|
7 |
|
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
@return
annotation 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.