Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.10] Add Support for WebP Format and OptimizeForSpeed Option #565

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading