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

Add setScreenshotType() method + Update Readme #180

Merged
merged 4 commits into from
Apr 12, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,19 @@ Here's the easiest way to create an image of a webpage:
Browsershot::url('https://example.com')->save($pathToImage);
```

#### Formatting the image
By default the screenshot's type will be a `png`. (According to [Puppeteer's Config](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagescreenshotoptions))
But you can change it to `jpeg` with quality option.

```php
Browsershot::url('https://example.com')
->setScreenshotType('jpeg', 100)
->save($pathToImage);
```

#### Sizing the image

By default the screenshot will be a `png` and it's size will match the resolution you use for your desktop. Want another size of screenshot? No problem!
By default the screenshot's size will match the resolution you use for your desktop. Want another size of screenshot? No problem!

```php
Browsershot::url('https://example.com')
Expand Down Expand Up @@ -402,7 +412,7 @@ Browsershot::url('https://example.com')
#### Changing the language of the browser

You can use `setOption` to change the language of the browser.
In order to load a page in a specific language for example.
In order to load a page in a specific language for example.

```php
Browsershot::url('https://example.com')
Expand Down
22 changes: 21 additions & 1 deletion src/Browsershot.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Browsershot
protected $proxyServer = '';
protected $showBackground = false;
protected $showScreenshotBackground = true;
protected $screenshotType = 'png';
protected $screenshotQuality = null;
protected $temporaryHtmlDirectory;
protected $timeout = 60;
protected $url = '';
Expand Down Expand Up @@ -218,6 +220,17 @@ public function hideBackground()
return $this;
}

public function setScreenshotType(string $type, int $quality = null)
{
$this->screenshotType = $type;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an empty line after this one.


if (! is_null($quality)) {
$this->screenshotQuality = $quality;
}

return $this;
}

public function ignoreHttpsErrors()
{
return $this->setOption('ignoreHttpsErrors', true);
Expand Down Expand Up @@ -408,11 +421,18 @@ public function createScreenshotCommand($targetPath = null): array
{
$url = $this->html ? $this->createTemporaryHtmlFile() : $this->url;

$options = [];
$options = [
'type' => $this->screenshotType,
];

if ($targetPath) {
$options['path'] = $targetPath;
}

if ($this->screenshotQuality) {
$options['quality'] = $this->screenshotQuality;
}

$command = $this->createCommand($url, 'screenshot', $options);

if (! $this->showScreenshotBackground) {
Expand Down