Skip to content

Commit

Permalink
Support Browsershot v4 (#107)
Browse files Browse the repository at this point in the history
* support browsershot v4

* Apply suggestions from code review

Co-authored-by: Julius Kiekbusch <[email protected]>

* Add spatie/image as a dev-dependency

* Update Reference Image

* Fix how ImageManipulations are run for S3 images

---------

Co-authored-by: Julius Kiekbusch <[email protected]>
Co-authored-by: Stefan Zweifel <[email protected]>
  • Loading branch information
3 people authored Dec 29, 2023
1 parent debe350 commit b26536f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 9 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ BrowsershotLambda::html('<h1>Hello world!!</h1>')->saveToS3('example.pdf', 'exam
```

## Image Manipulation
Like the original Browsershot package, you can [manipulate the image](https://spatie.be/docs/browsershot/v2/usage/creating-images#content-sizing-the-image) size and format.
Like the original Browsershot package, you can [manipulate the image](https://spatie.be/docs/browsershot/v4/usage/creating-images#content-sizing-the-image) size and format.

To perform image manipulations on the screenshot, you need to install the optional dependency `spatie/image`. v3 or higher is required.

> **Note**
> If you're using `fit()` in combination with `saveToS3`, the image will be downloaded from S3 to your local disc, manipulated and then uploaded back to S3.
Expand All @@ -128,15 +130,15 @@ Like the original Browsershot package, you can [manipulate the image](https://sp
// Take screenshot at 1920x1080 and scale it down to fit 200x200
BrowsershotLambda::url('https://example.com')
->windowSize(1920, 1080)
->fit(Manipulations::FIT_CONTAIN, 200, 200)
->fit(\Spatie\Image\Enums\Fit::Contain, 200, 200)
->save('example.jpg');

// Take screenshot at 1920x1080 and scale it down to fit 200x200 and save it on S3
// Note: To do the image manipulation, BrowsershotLambda will download the image
// from S3 to the local disc of your app, manipulate it and then upload it back to S3.
BrowsershotLambda::url('https://example.com')
->windowSize(1920, 1080)
->fit(Manipulations::FIT_CONTAIN, 200, 200)
->fit(\Spatie\Image\Enums\Fit::Contain, 200, 200)
->saveToS3('example.jpg');
```

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"php": "^8.2",
"hammerstone/sidecar": "^0.4.0",
"illuminate/contracts": "^10.0",
"spatie/browsershot": "^3.60",
"spatie/browsershot": "^4.0",
"spatie/laravel-package-tools": "^1.9.2"
},
"require-dev": {
Expand All @@ -36,7 +36,8 @@
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^10"
"phpunit/phpunit": "^10",
"spatie/image": "^3.3"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/BrowsershotLambda.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function applyManipulationsOnS3(string $imagePath, string $disk = 's3'):

$localPath = Storage::disk('local')->path($imagePath);

$this->applyManipulations($localPath);
$this->imageManipulations->apply($localPath);

// Upload the manipulated image back to S3 and delete the temporary file.
Storage::disk($disk)->put($imagePath, Storage::disk('local')->get($imagePath));
Expand Down
6 changes: 3 additions & 3 deletions tests/BrowsershotLambdaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use Hammerstone\Sidecar\Exceptions\LambdaExecutionException;
use Illuminate\Support\Facades\Storage;
use Spatie\Browsershot\Exceptions\CouldNotTakeBrowsershot;
use Spatie\Image\Enums\Fit;
use Spatie\Image\Image;
use Spatie\Image\Manipulations;
use Wnx\SidecarBrowsershot\BrowsershotLambda;

beforeEach(function () {
Expand Down Expand Up @@ -122,7 +122,7 @@

BrowsershotLambda::url('https://example.com')
->windowSize(1920, 1080)
->fit(Manipulations::FIT_CONTAIN, 200, 200)
->fit(Fit::Contain, 200, 200)
->save('example.jpg');

$image = new Image('example.jpg');
Expand All @@ -135,7 +135,7 @@
// Create screenshot from example.com and resize it to 200x200
BrowsershotLambda::url('https://example.com')
->windowSize(1920, 1080)
->fit(Manipulations::FIT_CONTAIN, 200, 200)
->fit(Fit::Contain, 200, 200)
->saveToS3('example.jpg');

$this->assertTrue(Storage::disk('s3')->exists('example.jpg'));
Expand Down
Binary file modified tests/references/hello-world-with-smileys.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b26536f

Please sign in to comment.