mtownsend / remove-bg
一个用于与 remove.bg API 接口的 PHP 包。
Requires
- php: ~7.0|~8.0
- guzzlehttp/guzzle: ^7
Requires (Dev)
- phpunit/phpunit: ^6.4
README
使用 remove.bg API 从您的图片中编程式移除背景。
安装
通过 Composer 安装
composer require mtownsend/remove-bg
此包旨在与任何 PHP 7.0+ 应用程序一起使用,但特别支持 Laravel。
注册服务提供者(Laravel 用户)
对于 Laravel 5.4 及以下版本,将以下行添加到您的 config/app.php
/* * Package Service Providers... */ Mtownsend\RemoveBg\Providers\RemoveBgServiceProvider::class,
对于 Laravel 5.5 及以上版本,包将自动为您注册提供者。
使用 Lumen
要注册服务提供者,将以下行添加到 app/bootstrap/app.php
$app->register(Mtownsend\RemoveBg\Providers\RemoveBgServiceProvider::class);
发布配置文件(Laravel 用户)
php artisan vendor:publish --provider="Mtownsend\RemoveBg\Providers\RemoveBgServiceProvider"
一旦您的 removebg.php
已发布到您的配置文件夹,添加从 Remove.bg 获得的 API 密钥。如果您使用 Laravel 并将 remove.bg API 密钥放入配置文件,Laravel 将在每次通过助手或外观实例化类时自动设置您的 API 密钥。
快速开始
使用类
use Mtownsend\RemoveBg\RemoveBg; $absoluteUrl = 'https://yoursite.com/images/photo.jpg'; $pathToFile = 'images/avatar.jpg'; $base64EncodedFile = base64_encode(file_get_contents($pathToFile)); $removebg = new RemoveBg($apiKey); // Directly saving files $removebg->url($absoluteUrl)->save('path/to/your/file.png'); $removebg->file($pathToFile)->save('path/to/your/file2.png'); $removebg->base64($base64EncodedFile)->save('path/to/your/file3.png'); // Getting the file's raw contents to save or do something else with $rawUrl = $removebg->url($absoluteUrl)->get(); $rawFile = $removebg->file($pathToFile)->get(); $rawBase64 = $removebg->base64($base64EncodedFile)->get(); file_put_contents('path/to/your/file4.png', $rawUrl); // etc... // Getting the file's base64 encoded contents from the api $base64Url = $removebg->url($absoluteUrl)->getBase64(); $base64File = $removebg->file($pathToFile)->getBase64(); $base64Base64 = $removebg->base64($base64EncodedFile)->getBase64(); file_put_contents('path/to/your/file5.png', base64_decode($base64Url)); // etc... // Please note: remove.bg returns all images in .png format, so you should be saving all files received from the api as .png.
高级用法
Remove.bg 为每个 API 调用提供多个请求体参数。对于最新的列表,您应该始终检查 remove.bg API 文档。
以下是一个配置了特定请求体参数的 API 调用的示例。
$removebg = new RemoveBg($apiKey); // Directly saving files $removebg->url($absoluteUrl) ->body([ 'size' => '4k', // regular, medium, hd, 4k, auto 'bg_color' => '#CBD5E0', 'add_shadow' => true, // primarily used for automotive photos as of the time this documentation was written 'channels' => 'rgba', // rgba, alpha ]) ->save('path/to/your/file.png');
您还可以直接指定请求头参数。截至目前,这似乎不会在 Remove.bg API 如何消费这些头方面提供很多功能,但我们认为暴露此功能很重要。以下是一个示例:
$removebg = new RemoveBg($apiKey); // Directly saving files $removebg->url($absoluteUrl) ->headers([ 'X-Foo-Header' => 'Some Bar Value', 'X-Foo-Header-2' => 'Some Bar Value 2', ]) ->save('path/to/your/file.png');
账户详情
Remove.bg API 提供了一个端点来检查您的账户信用余额和免费 API 调用使用情况。如果您的应用程序在处理图片之前需要检查您的可用信用,此包可以让您轻松完成任务!
以下代码示例演示了如何以编程方式检查您的账户信息。请注意,account
方法有一个可选参数:$getResponseAsObject = true
。默认情况下,您的响应将作为对象返回。您可以通过将 false
传递给 account(false)
方法将响应作为关联数组返回。
$removebg = new RemoveBg($apiKey); $account = $removebg->account(); // $account will be something like this: { "data": { "attributes": { "credits": { "total": 200, "subscription": 150, "payg": 50 }, "api": { "free_calls": 50, "sizes": "all" } } } }
要访问您的总信用额,您可以这样做:$account->data->attributes->credits->total
。
一个实际的示例可能如下所示
$removebg = new RemoveBg($apiKey); $account = $removebg->account(); if ($account->data->attributes->credits->total >= 1) { $removebg->url($absoluteUrl)->save('path/to/your/file.png'); }
使用全局助手(Laravel 用户)
如果您使用 Laravel,此包提供了一个方便的全局可访问的助手函数。
removebg()->url($absoluteUrl)->save(public_path('path/to/your/file.png'));
使用外观(Laravel 用户)
如果您使用 Laravel,此包提供了一个外观。要注册外观,请将以下行添加到 config/app.php
中的 aliases
键下。
'RemoveBg' => Mtownsend\RemoveBg\Facades\RemoveBg::class,
use RemoveBg; RemoveBg::file($pathToFile)->save(public_path('path/to/your/file.png'));
信用
- Mark Townsend
- 所有贡献者
测试
测试即将推出...
您可以使用以下方式运行测试:
./vendor/bin/phpunit
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件。