wnx / sidecar-browsershot
在Lambda上运行Browsershot的Sidecar功能。
Requires
- php: ^8.2
- hammerstone/sidecar: ^0.4 || ^0.5 || ^0.6
- illuminate/contracts: ^10.0 || ^11.0
- spatie/browsershot: ^4.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- ext-imagick: *
- larastan/larastan: ^1.0|^2.0
- laravel/pint: ^1.13
- league/flysystem-aws-s3-v3: ^1.0|^2.0|^3.0
- nunomaduro/collision: ^7.0|^8.0
- orchestra/testbench: ^8.0|^9.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^10 | ^11.0
- spatie/image: ^3.3
- spatie/pixelmatch-php: dev-main
- dev-main
- v2.3.3
- v2.3.2
- v2.3.1
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.0
- v1.13.1
- v1.13.0
- v1.12.0
- v1.11.1
- v1.11.0
- v1.10.0
- v1.9.1
- v1.9.0
- v1.8.1
- v1.8.0
- v1.7.0
- v1.6.4
- v1.6.3
- v1.6.2
- v1.6.1
- v1.6.0
- v1.5.0
- v1.4.1
- v1.4.0
- v1.3.0
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.1
- v1.0.0
- v0.2.0
- v0.1.0
- dev-upgrade-layers
- dev-fix/deprecated-node-methods
This package is auto-updated.
Last update: 2024-09-09 12:02:03 UTC
README
此包允许您通过Sidecar在AWS Lambda上运行Browsershot。
您无需在服务器上安装Node、Puppeteer或Google Chrome。启动无头Google Chrome实例的重负载在AWS Lambda上执行。
要求
此包要求在您的Laravel应用程序中安装spatie/browsershot
和hammerstone/sidecar
。
遵循他们的安装和配置说明。(尽管如此,您可以跳过Browsershot的puppeteer和Google Chrome的安装。)
安装
您可以通过composer安装此包。
composer require wnx/sidecar-browsershot
您可以使用以下命令发布配置文件:
php artisan vendor:publish --tag="sidecar-browsershot-config"
在您的sidecar.php
配置文件中注册BrowsershotFunction::class
。
/* * All of your function classes that you'd like to deploy go here. */ 'functions' => [ \Wnx\SidecarBrowsershot\Functions\BrowsershotFunction::class, ],
通过运行以下命令部署Lambda函数:
php artisan sidecar:deploy --activate
有关详细信息,请参阅Sidecar文档。
使用
您可以使用BrowsershotLambda
,就像来自Spatie包的默认Browsershot
类一样。您只需将Browsershot
替换为BrowsershotLambda
即可。
use Wnx\SidecarBrowsershot\BrowsershotLambda; // an image will be saved BrowsershotLambda::url('https://example.com')->save($pathToImage); // a pdf will be saved BrowsershotLambda::url('https://example.com')->save('example.pdf'); // save your own HTML to a PDF BrowsershotLambda::html('<h1>Hello world!!</h1>')->save('example.pdf'); // Get HTML of a URL and store it on a given disk $html = BrowsershotLambda::url('https://example.com')->bodyHtml(); Storage::disk('s3')->put('example.html', $html);
预热
sidecar-browsershot支持预热以实现更快的执行。
要启用此功能,请将SIDECAR_BROWSERSHOT_WARMING_INSTANCES
变量设置为Sidecar为您预热的实例数。
SIDECAR_BROWSERSHOT_WARMING_INSTANCES=5
或者,您可以发布sidecar-browsershot.php
配置文件并自行更改预热设置。
从S3读取源代码
您可以在AWS S3上存储一个HTML文件,并将路径传递给Lambda以创建PDF或图像。这对于大型源文件是必要的,以避免Lambda请求大小的限制。
use Wnx\SidecarBrowsershot\BrowsershotLambda; // Use an HTML file from S3 to generate a PDF BrowsershotLambda::readHtmlFromS3('html/example.html')->save('example.pdf'); // You can also pass a disk name if required (default: 's3') BrowsershotLambda::readHtmlFromS3('html/example.html', 's3files')->save('example.pdf');
直接保存到S3
如果您想保留文件在S3上,或者为了避免Lambda响应的大小限制,您可以直接将文件存储在AWS S3上。
您只需将路径和可选的磁盘名称(默认为's3')传递给saveToS3
方法即可。
- 您必须在config/filesystems.php中定义S3磁盘。
- 您必须为您的sidecar-execution-role授予S3写入权限。
use Wnx\SidecarBrowsershot\BrowsershotLambda; // an image will be saved on S3 BrowsershotLambda::url('https://example.com')->saveToS3('example.jpg'); // a pdf will be saved on S3 BrowsershotLambda::url('https://example.com')->saveToS3('example.pdf'); // save your own html to a PDF on S3 BrowsershotLambda::html('<h1>Hello world!!</h1>')->saveToS3('example.pdf', 'example-store');
图像处理
与原始Browsershot包一样,您可以调整图像大小和格式。
要对截图执行图像处理,您需要安装可选依赖项spatie/image
。需要v3或更高版本。
注意
如果您使用fit()
与saveToS3
结合使用,则图像将从S3下载到您的本地磁盘,然后进行处理,最后再上传回S3。
// Take screenshot at 1920x1080 and scale it down to fit 200x200 BrowsershotLambda::url('https://example.com') ->windowSize(1920, 1080) ->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(\Spatie\Image\Enums\Fit::Contain, 200, 200) ->saveToS3('example.jpg');
注册自定义字体
默认情况下,sidecar-browsershot 包含 "Noto Color Emoji" 字体,以确保表情符号正确渲染。如果您想使用自定义字体,可以将它们全部放入 resources/sidecar-browsershot/fonts
文件夹中。(您可以在 sidecar-browsershot.fonts
配置中自定义该文件夹的位置)
sidecar-browsershot 在部署 Lambda 函数时将包含该文件夹中的所有文件,并自动为您在 Chromium 中注册它们。
测试
测试套件会连接到 AWS 并运行部署的 Lambda 函数。要运行测试套件,您需要一个有效的 AWS 账户。
我们可以使用本地的 sidecar:configure
artisan 命令为 Sidecar 创建必要的 AWS 凭据。首先将 testbench.example.yaml
文件复制到 testbench.yaml
。然后运行 ./vendor/bin/testbench sidecar:configure
以启动 Sidecar 设置过程。(您只需要设置一次)
cp testbench.example.yaml testbench.yaml cp .env.example .env ./vendor/bin/testbench sidecar:configure
完成 Sidecar 设置过程后,您将收到几个 SIDECAR_*
环境变量。将这些凭据添加到 .env
。
现在我们可以将本地的 BrowsershotFunction
部署到 AWS Lambda。在执行测试套件之前,在您的终端中运行以下命令。
./vendor/bin/testbench sidecar-browsershot:setup
部署成功后,您可以运行测试套件。
composer test
变更日志
有关最近更改的更多信息,请参阅 变更日志。
贡献
有关详细信息,请参阅 贡献指南。
安全漏洞
有关如何报告安全漏洞的详细信息,请参阅 我们的安全策略。
致谢
许可
MIT 许可证(MIT)。有关更多信息,请参阅 许可文件。