Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdankharchenko committed Mar 10, 2021
0 parents commit a48cc1a
Show file tree
Hide file tree
Showing 20 changed files with 1,071 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Path-based git attributes
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/.scrutinizer.yml export-ignore
/tests export-ignore
/.editorconfig export-ignore
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
build
composer.lock
docs
vendor
coverage
/.idea/
.phpunit.result.cache
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Bogdan Kharchenko

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
### [capturely.app](https://capturely.app) web/html to pdf/png/jpeg converter

### Install

```composer
composer require capturely/capturely-laravel
```

#### config/services.php

```php
'capturely' => [
'token' => env('CAPTURELY_TOKEN', 'INSERT_TOKEN'),
],
```

### Usage PDF

```php
<?php

use Capturely\Capturely;

// Returns Response with URL & File Size
$capture = Capturely::url('https://google.com')->screenshot()->capture();
echo $capture->url;

// OR Stream Response
$capture = Capturely::url('https://google.com')->pdf()->stream();

return $capture;
```

### Usage Screenshot

```php
<?php

use Capturely\Capturely;
use Capturely\Structures\Screenshot;

$capture = Capturely::url('https://google.com')
->screenshot(function(Screenshot $screenshot) {
return $screenshot
->fullPage()
->png()
->base64();
})
->capture();

echo $capture->url;
```

### Additional Options

```php
<?php

use Capturely\Capturely;
use Capturely\Structures\Pdf;
use Capturely\Structures\Viewport;

$capture = Capturely::url('https://google.com')
->pdf(function(Pdf $pdf) {
return $pdf->letter();
})
->viewport(function(Viewport $viewport){
return $viewport
->windowSize(800,600)
->isLandscape();
})
->extraHttpHeaders([
'Custom-Header-Name' => 'secret',
])
->authentication('user', 'pass')
->userAgent('My Custom User Agent')
->emulateMediaType('print')
->toS3('my-bucket', 'path')
->capture();

echo $capture->url;
```
53 changes: 53 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "capturely/capturely-laravel",
"description": "Laravel Package for capturely.app html/url to pdf/png/jpeg conversion service",
"keywords": [
"capturely",
"capturely.app",
"pptr converter"
],
"homepage": "https://github.com/capturely/capturely-laravel",
"license": "MIT",
"type": "library",
"authors": [
{
"name": "Bogdan Kharchenko",
"email": "[email protected]",
"role": "Developer"
}
],
"require": {
"ext-json": "*",
"php": "^7.4|^8.0",
"guzzlehttp/guzzle": "^7.2",
"illuminate/support": "^8.0"
},
"require-dev": {
"orchestra/testbench": "^v6.4.0",
"phpunit/phpunit": "^9.0"
},
"autoload": {
"psr-4": {
"Capturely\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Capturely\\Tests\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"Capturely\\CapturelyServiceProvider"
]
}
}
}
24 changes: 24 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
<logging>
</logging>
</phpunit>
115 changes: 115 additions & 0 deletions src/Api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Capturely;

use Capturely\Exceptions\CapturelyTokenNotSet;
use Capturely\Responses\ApiErrorException;
use Capturely\Responses\ConversionResponse;
use Capturely\Structures\Screenshot;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Response as ResponseFacade;

class Api
{
protected const API = 'https://api.capturely.app';

protected array $data = [];

public function __construct(array $data)
{
$this->data = $data;
}

/**
* @return ConversionResponse|\Symfony\Component\HttpFoundation\StreamedResponse|null
* @throws ApiErrorException
*/
public function post()
{
return $this->parseResponse($this->http()->post(static::API, $this->data));
}

/**
* @return ConversionResponse|\Symfony\Component\HttpFoundation\StreamedResponse|null
* @throws ApiErrorException
*/
protected function parseResponse(Response $response)
{
if (!$response->successful()) {
throw new ApiErrorException($response->body());
}

if ($this->isStream()) {
return ResponseFacade::stream(function() use ($response) {
echo $response->body();
}, 200, [
'Content-Type' => $response->header('Content-Type'),
]);
}

return new ConversionResponse(json_decode($response->body(), true));
}

protected function http() : PendingRequest
{
return Http::withHeaders([
'Accept' => $this->acceptHeader(),
'Content-Type' => 'application/json',
'Authorization' => $this->token(),
]);
}

protected function token()
{
$token = config('services.capturely.token');

if (empty($token)) {
throw new CapturelyTokenNotSet('capturely token not set');
}

return $token;
}

protected function isPdf() : bool
{
return Arr::has($this->data, 'options.pdf');
}

protected function isScreenshot() : bool
{
return Arr::has($this->data, 'options.screenshot');
}

protected function isScreenshotBinary() : bool
{
return $this->isScreenshot()
&& Arr::get($this->data, 'options.screenshot.encoding', Screenshot::BINARY) === Screenshot::BINARY;
}

protected function isStream() : bool
{
return Arr::get($this->data, 'stream', false);
}

protected function acceptHeader() : string
{
if ($this->isStream()) {

if ($this->isPdf()) {
return 'application/pdf';
}

if ($this->isScreenshotBinary()) {
$type = Arr::get($this->data, 'screenshot.type', Screenshot::PNG);

return "image/{$type}";
}

}

return 'application/json';
}
}
Loading

0 comments on commit a48cc1a

Please sign in to comment.