Skip to content

Commit

Permalink
feat(chromium): add clip options to screenshot routes
Browse files Browse the repository at this point in the history
  • Loading branch information
gulien committed Apr 25, 2024
1 parent e088257 commit b47aacb
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,9 @@ use Gotenberg\Gotenberg;

$request = Gotenberg::chromium($apiUrl)
->screenshot()
->width(1280)
->height(800)
->clip()
->png()
->optimizeForSpeed()
->url('https://my.url');
Expand Down
31 changes: 31 additions & 0 deletions src/Modules/ChromiumScreenshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,37 @@ class ChromiumScreenshot
{
use ChromiumMultipartFormDataModule;

/**
* The device screen width in pixels.
*/
public function width(int $width): self
{
$this->formValue('width', $width);

return $this;
}

/**
* The device screen height in pixels.
*/
public function height(int $height): self
{
$this->formValue('height', $height);

return $this;
}

/**
* Defines whether to clip the screenshot according to the device
* dimensions.
*/
public function clip(): self
{
$this->formValue('clip', true);

return $this;
}

/**
* PNG as image compression format.
*/
Expand Down
57 changes: 57 additions & 0 deletions tests/Modules/ChromiumScreenshotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
function (
string $url,
int|null $width = null,
int|null $height = null,
bool $clip = false,
string|null $format = null,
int|null $quality = null,
bool $optimizeForSpeed = false,
Expand All @@ -35,6 +38,9 @@ function (
$chromium = Gotenberg::chromium('')->screenshot();
$chromium = hydrateChromiumScreenshotFormData(
$chromium,
$width,
$height,
$clip,
$format,
$quality,
$optimizeForSpeed,
Expand All @@ -58,6 +64,9 @@ function (

expectChromiumScreenshotOptions(
$body,
$width,
$height,
$clip,
$format,
$quality,
$optimizeForSpeed,
Expand All @@ -77,6 +86,9 @@ function (
['https://my.url'],
[
'https://my.url',
1280,
800,
true,
'png',
100,
true,
Expand Down Expand Up @@ -111,6 +123,9 @@ function (
*/
function (
Stream $index,
int|null $width = null,
int|null $height = null,
bool $clip = false,
string|null $format = null,
int|null $quality = null,
bool $optimizeForSpeed = false,
Expand All @@ -128,6 +143,9 @@ function (
$chromium = Gotenberg::chromium('')->screenshot();
$chromium = hydrateChromiumScreenshotFormData(
$chromium,
$width,
$height,
$clip,
$format,
$quality,
$optimizeForSpeed,
Expand All @@ -153,6 +171,9 @@ function (

expectChromiumScreenshotOptions(
$body,
$width,
$height,
$clip,
$format,
$quality,
$optimizeForSpeed,
Expand All @@ -172,6 +193,9 @@ function (
[Stream::string('my.html', 'HTML content')],
[
Stream::string('my.html', 'HTML content'),
1280,
800,
true,
'jpeg',
100,
true,
Expand Down Expand Up @@ -208,6 +232,9 @@ function (
function (
Stream $index,
array $markdowns,
int|null $width = null,
int|null $height = null,
bool $clip = false,
string|null $format = null,
int|null $quality = null,
bool $optimizeForSpeed = false,
Expand All @@ -225,6 +252,9 @@ function (
$chromium = Gotenberg::chromium('')->screenshot();
$chromium = hydrateChromiumScreenshotFormData(
$chromium,
$width,
$height,
$clip,
$format,
$quality,
$optimizeForSpeed,
Expand Down Expand Up @@ -255,6 +285,9 @@ function (

expectChromiumScreenshotOptions(
$body,
$width,
$height,
$clip,
$format,
$quality,
$optimizeForSpeed,
Expand Down Expand Up @@ -283,6 +316,9 @@ function (
Stream::string('my.md', 'Markdown content'),
Stream::string('my_second.md', 'Second Markdown content'),
],
1280,
800,
true,
'webp',
100,
true,
Expand Down Expand Up @@ -315,6 +351,9 @@ function (
*/
function hydrateChromiumScreenshotFormData(
ChromiumScreenshot $chromium,
int|null $width,
int|null $height,
bool $clip,
string|null $format = null,
int|null $quality = null,
bool $optimizeForSpeed = false,
Expand All @@ -329,6 +368,18 @@ function hydrateChromiumScreenshotFormData(
bool $skipNetworkIdleEvent = false,
array $assets = [],
): ChromiumScreenshot {
if ($width !== null) {
$chromium->width($width);
}

if ($height !== null) {
$chromium->height($height);
}

if ($clip) {
$chromium->clip();
}

if ($format === 'png') {
$chromium->png();
}
Expand Down Expand Up @@ -404,6 +455,9 @@ function hydrateChromiumScreenshotFormData(
*/
function expectChromiumScreenshotOptions(
string $body,
int|null $width,
int|null $height,
bool $clip,
string|null $format,
int|null $quality,
bool $optimizeForSpeed,
Expand All @@ -418,6 +472,9 @@ function expectChromiumScreenshotOptions(
bool $skipNetworkIdleEvent,
array $assets,
): void {
expect($body)->unless($width === null, fn ($body) => $body->toContainFormValue('width', $width . ''));
expect($body)->unless($height === null, fn ($body) => $body->toContainFormValue('height', $height . ''));
expect($body)->unless($clip === false, fn ($body) => $body->toContainFormValue('clip', '1'));
expect($body)->unless($format === null, fn ($body) => $body->toContainFormValue('format', $format));
expect($body)->unless($quality === null, fn ($body) => $body->toContainFormValue('quality', $quality . ''));
expect($body)->unless($optimizeForSpeed === false, fn ($body) => $body->toContainFormValue('optimizeForSpeed', '1'));
Expand Down

0 comments on commit b47aacb

Please sign in to comment.