Skip to content

Commit 8de85db

Browse files
committed
Screenshot clip [ci skip]
1 parent 445cac5 commit 8de85db

File tree

4 files changed

+183
-7
lines changed

4 files changed

+183
-7
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ Once the page has completed the navigation you can evaluate arbitrary script on
206206
// wait for the page to be loaded
207207
$navigation->waitForNavigation();
208208

209-
// evaluate script in the browser
209+
// take a screenshot
210210
$screenshot = $page->screenshot([
211211
'format' => 'jpeg', // default to 'png' - possible values: 'png', 'jpeg',
212212
'quality' => 80 // only if format is 'jpeg' - default 100
@@ -216,6 +216,36 @@ Once the page has completed the navigation you can evaluate arbitrary script on
216216
$screenshot->saveToFile('/some/place/file.jpg');
217217
```
218218

219+
**choose an area**
220+
221+
You can use the option "clip" in order to choose an area for the screenshot.
222+
223+
```php
224+
225+
use HeadlessChromium\Clip;
226+
227+
// navigate
228+
$navigation = $page->navigate('http://example.com');
229+
230+
// wait for the page to be loaded
231+
$navigation->waitForNavigation();
232+
233+
// create a rectangle by specifying to left corner coordinates + width and height
234+
$x = 10;
235+
$y = 10;
236+
$width = 100;
237+
$height = 100;
238+
$clip = new Clip($x, $y, $width, $height);
239+
240+
// take the screenshot
241+
$screenshot = $page->screenshot([
242+
'clip' => $clip'
243+
]);
244+
245+
// save the screenshot
246+
$screenshot->saveToFile('/some/place/file.jpg');
247+
```
248+
219249

220250

221251
------------------------------------------------------------------------------------------------------------------------

src/Clip.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* @license see LICENSE
4+
*/
5+
6+
namespace HeadlessChromium;
7+
8+
9+
class Clip
10+
{
11+
12+
protected $x;
13+
protected $y;
14+
protected $height;
15+
protected $width;
16+
protected $scale;
17+
18+
/**
19+
* Clip constructor.
20+
* @param $x
21+
* @param $y
22+
* @param $height
23+
* @param $width
24+
* @param $scale
25+
*/
26+
public function __construct($x, $y, $width, $height, $scale = 1)
27+
{
28+
$this->x = $x;
29+
$this->y = $y;
30+
$this->height = $height;
31+
$this->width = $width;
32+
$this->scale = $scale;
33+
}
34+
35+
/**
36+
* @return mixed
37+
*/
38+
public function getX()
39+
{
40+
return $this->x;
41+
}
42+
43+
/**
44+
* @return mixed
45+
*/
46+
public function getY()
47+
{
48+
return $this->y;
49+
}
50+
51+
/**
52+
* @return mixed
53+
*/
54+
public function getHeight()
55+
{
56+
return $this->height;
57+
}
58+
59+
/**
60+
* @return mixed
61+
*/
62+
public function getWidth()
63+
{
64+
return $this->width;
65+
}
66+
67+
/**
68+
* @return mixed
69+
*/
70+
public function getScale()
71+
{
72+
return $this->scale;
73+
}
74+
75+
/**
76+
* @param mixed $x
77+
*/
78+
public function setX($x)
79+
{
80+
$this->x = $x;
81+
}
82+
83+
/**
84+
* @param mixed $y
85+
*/
86+
public function setY($y)
87+
{
88+
$this->y = $y;
89+
}
90+
91+
/**
92+
* @param mixed $height
93+
*/
94+
public function setHeight($height)
95+
{
96+
$this->height = $height;
97+
}
98+
99+
/**
100+
* @param mixed $width
101+
*/
102+
public function setWidth($width)
103+
{
104+
$this->width = $width;
105+
}
106+
107+
/**
108+
* @param mixed $scale
109+
*/
110+
public function setScale($scale)
111+
{
112+
$this->scale = $scale;
113+
}
114+
115+
}

src/Page.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ public function hasLifecycleEvent(string $event): bool
139139

140140
/**
141141
*
142+
* Example:
143+
*
144+
* ```php
145+
* $page->screenshot()->saveToFile();
146+
* ```
147+
*
148+
* @param array $options
149+
* @return PageScreenshot
150+
* @throws CommunicationException
142151
*/
143152
public function screenshot(array $options = []): PageScreenshot
144153
{
@@ -185,13 +194,27 @@ public function screenshot(array $options = []): PageScreenshot
185194
$screenshotOptions['quality'] = $options['quality'];
186195
}
187196

188-
// TODO document clip
189-
// TODO validate clip/use viewport object
197+
// clip
190198
if (array_key_exists('clip', $options)) {
191-
$screenshotOptions['clip'] = $options['clip'];
199+
200+
// make sure it's a Clip instance
201+
if (!($options['clip'] instanceof Clip)) {
202+
throw new \InvalidArgumentException(
203+
sprintf('Invalid options "clip" for page screenshot, it must be a %s instance.', Clip::class)
204+
);
205+
}
206+
207+
// add to params
208+
$screenshotOptions['clip'] = [
209+
'x' => $options['clip']->getX(),
210+
'y' => $options['clip']->getY(),
211+
'width' => $options['clip']->getWidth(),
212+
'height' => $options['clip']->getHeight(),
213+
'scale' => $options['clip']->getScale()
214+
];
192215
}
193216

194-
// request screen shot
217+
// request screenshot
195218
$responseReader = $this->getSession()
196219
->sendMessage(new Message('Page.captureScreenshot', $screenshotOptions));
197220

src/PageUtils/PageScreenshot.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ public function __construct(ResponseReader $responseReader)
2525
$this->responseReader = $responseReader;
2626
}
2727

28+
/**
29+
* @return ResponseReader
30+
*/
31+
public function getResponseReader(): ResponseReader
32+
{
33+
return $this->responseReader;
34+
}
35+
2836
/**
2937
* Get base64 representation of the file
3038
* @return mixed
@@ -49,10 +57,10 @@ public function getBase64()
4957
* @throws FilesystemException
5058
* @throws ScreenshotFailed
5159
*/
52-
public function saveToFile(string $path)
60+
public function saveToFile(string $path, int $timeout = 5000)
5361
{
5462

55-
$response = $this->responseReader->waitForResponse();
63+
$response = $this->responseReader->waitForResponse($timeout);
5664

5765
if (!$response->isSuccessful()) {
5866
throw new ScreenshotFailed(

0 commit comments

Comments
 (0)