From 93f6ee7fd3e60973afe56716cd5df9694ff2a3c9 Mon Sep 17 00:00:00 2001 From: nesty92 Date: Thu, 7 Dec 2023 09:26:29 +0100 Subject: [PATCH] Add support for webp and optimizeForSpeed option --- README.md | 7 ++++--- src/Page.php | 22 ++++++++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e763d07..5e08f93 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/src/Page.php b/src/Page.php index 82ce911..0ee6659 100644 --- a/src/Page.php +++ b/src/Page.php @@ -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 @@ -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 @@ -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));