-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from SynergiTech/screenshot
feat: Add screenshot support
- Loading branch information
Showing
11 changed files
with
244 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
namespace SynergiTech\ChromePDF\Browserless; | ||
|
||
use GuzzleHttp\Psr7\StreamWrapper; | ||
|
||
trait Client | ||
{ | ||
/** | ||
* @var \GuzzleHttp\Client | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
private $apiKey; | ||
|
||
/** | ||
* @param \GuzzleHttp\Client $client custom Guzzle client | ||
*/ | ||
public function __construct( | ||
string $apiKey = null, | ||
EndpointsEnum|string $endpoint = EndpointsEnum::Default, | ||
$client = null | ||
) { | ||
if ($client === null) { | ||
// @codeCoverageIgnoreStart | ||
$client = new \GuzzleHttp\Client([ | ||
'base_uri' => ($endpoint instanceof EndpointsEnum) ? $endpoint->value : $endpoint, | ||
]); | ||
// @codeCoverageIgnoreEnd | ||
} | ||
$this->client = $client; | ||
if ($apiKey !== null) { | ||
$this->setApiKey($apiKey); | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves the browserless.io API key | ||
* | ||
* @return string|null | ||
*/ | ||
public function getApiKey(): ?string | ||
{ | ||
return $this->apiKey; | ||
} | ||
|
||
/** | ||
* @param array<mixed> $json | ||
* | ||
* @return resource | ||
*/ | ||
protected function request(string $endpoint, array $json) | ||
{ | ||
try { | ||
$response = $this->client->post($endpoint, [ | ||
'query' => [ | ||
'token' => $this->getApiKey(), | ||
], | ||
'json' => $json, | ||
]); | ||
} catch (\GuzzleHttp\Exception\ClientException $e) { | ||
$message = 'No response'; | ||
|
||
$response = $e->getResponse(); | ||
|
||
/** | ||
* You could use $e->hasResponse() but that is not accurate enough, | ||
* as phpstan will be analysing against method signatures from guzzle 6 & 7 | ||
*/ | ||
if ($response !== null) { | ||
$message = $response->getBody(); | ||
|
||
$json = json_decode($message); | ||
if (json_last_error() === JSON_ERROR_NONE) { | ||
$messages = []; | ||
foreach ($json as $error) { | ||
$messages[] = $error->message; | ||
} | ||
$message = implode(', ', $messages); | ||
} | ||
} | ||
|
||
throw new APIException("Failed to render from Browserless: {$message}", $e->getCode(), $e); | ||
} catch (\Exception $e) { | ||
throw new APIException("Failed to render from Browserless: {$e->getMessage()}", $e->getCode(), $e); | ||
} | ||
|
||
return StreamWrapper::getResource($response->getBody()); | ||
} | ||
|
||
/** | ||
* Sets the browserless API key | ||
* | ||
* @param string $apiKey | ||
* @return self | ||
*/ | ||
private function setApiKey(string $apiKey): self | ||
{ | ||
$this->apiKey = $apiKey; | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace SynergiTech\ChromePDF\Browserless; | ||
|
||
enum EndpointsEnum: string | ||
{ | ||
case Default = 'https://chrome.browserless.io'; | ||
case London = 'https://production-lon.browserless.io'; | ||
case SanFrancisco = 'https://production-sfo.browserless.io'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace SynergiTech\ChromePDF\Browserless; | ||
|
||
class Screenshot | ||
{ | ||
use Client; | ||
|
||
/** | ||
* @param array<string,mixed> $options see https://www.browserless.io/docs/screenshot#custom-options | ||
* | ||
* @return resource | ||
*/ | ||
public function render(string $url, array $options = []) | ||
{ | ||
$options = array_merge([ | ||
'type' => 'png', | ||
'fullPage' => false, | ||
], $options); | ||
|
||
if ($options['type'] === 'jpeg' && ! isset($options['quality'])) { | ||
$options['quality'] = 75; | ||
} | ||
|
||
return $this->request( | ||
endpoint: '/screenshot', | ||
json: [ | ||
'url' => $url, | ||
'options' => $options, | ||
], | ||
); | ||
} | ||
} |
Oops, something went wrong.