Skip to content

Commit

Permalink
Add support for webp and optimizeForSpeed option (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
nesty92 authored Dec 7, 2023
1 parent cc6726d commit 5177edb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,9 @@ $navigation->waitForNavigation();

// take a screenshot
$screenshot = $page->screenshot([
'format' => 'jpeg', // default to 'png' - possible values: 'png', 'jpeg',
'quality' => 80, // only if format is 'jpeg' - default 100
'format' => 'jpeg', // default to 'png' - possible values: 'png', 'jpeg', 'webp'
'quality' => 80, // only when format is 'jpeg' or 'webp' - default 100
'optimizeForSpeed' => true // default to 'false' - Optimize image encoding for speed, not for resulting size
]);

// save the screenshot
Expand Down Expand Up @@ -470,7 +471,7 @@ $navigation->waitForNavigation();
$screenshot = $page->screenshot([
'captureBeyondViewport' => true,
'clip' => $page->getFullPageClip(),
'format' => 'jpeg', // default to 'png' - possible values: 'png', 'jpeg',
'format' => 'jpeg', // default to 'png' - possible values: 'png', 'jpeg', 'webp'
]);

// save the screenshot
Expand Down
22 changes: 16 additions & 6 deletions src/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,8 @@ public function getFullPageClip(int $timeout = null): Clip
* $screenshot = $page->screenshot([
* 'captureBeyondViewport' => true,
* 'clip' => $page->getFullPageClip(),
* 'format' => 'jpeg', // default to 'png' - possible values: 'png', 'jpeg',
* 'format' => 'jpeg', // default to 'png' - possible values: 'png', 'jpeg', 'webp'
* 'optimizeForSpeed' => true, // default to false - Optimize image encoding for speed, not for resulting size
* ]);
*
* // save the screenshot
Expand Down Expand Up @@ -624,15 +625,15 @@ public function screenshot(array $options = []): PageScreenshot
}

// make sure format is valid
if (!\in_array($screenshotOptions['format'], ['png', 'jpeg'])) {
throw new \InvalidArgumentException('Invalid options "format" for page screenshot. Format must be "png" or "jpeg".');
if (!\in_array($screenshotOptions['format'], ['png', 'jpeg', 'webp'])) {
throw new \InvalidArgumentException('Invalid options "format" for page screenshot. Format must be "png", "jpeg" or "webp".');
}

// get quality
if (\array_key_exists('quality', $options)) {
// quality requires type to be jpeg
if ('jpeg' !== $screenshotOptions['format']) {
throw new \InvalidArgumentException('Invalid options "quality" for page screenshot. Quality requires the image format to be "jpeg".');
// quality requires type to be jpeg or webp
if (!\in_array($screenshotOptions['format'], ['jpeg', 'webp'])) {
throw new \InvalidArgumentException('Invalid options "quality" for page screenshot. Quality requires the image format to be "jpeg" or "webp".');
}

// quality must be an integer
Expand Down Expand Up @@ -666,6 +667,15 @@ public function screenshot(array $options = []): PageScreenshot
];
}

// optimize for speed
if (\array_key_exists('optimizeForSpeed', $options)) {
if (!\is_bool($options['optimizeForSpeed'])) {
throw new \InvalidArgumentException('Invalid options "optimizeForSpeed" for page screenshot. OptimizeForSpeed must be a boolean value.');
}

$screenshotOptions['optimizeForSpeed'] = $options['optimizeForSpeed'];
}

// request screenshot
$responseReader = $this->getSession()
->sendMessage(new Message('Page.captureScreenshot', $screenshotOptions));
Expand Down

0 comments on commit 5177edb

Please sign in to comment.