-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a8cba20
Showing
11 changed files
with
681 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
composer.lock | ||
vendor | ||
.DS_Store |
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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) Mark Townsend <[email protected]> | ||
|
||
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. |
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,122 @@ | ||
Programmatically remove backgrounds from your images using the remove.bg api. | ||
|
||
**Remove.bg is in private beta. This package is subject to change. Production use is not recommended until remove.bg's api leaves beta.** | ||
|
||
[See the remove.bg docs for more details](https://developer.remove.bg/docs). | ||
|
||
## Installation | ||
|
||
Install via composer: | ||
|
||
``` | ||
composer require mtownsend/remove-bg | ||
``` | ||
|
||
*This package is designed to work with any PHP 7.0+ application but has special support for Laravel.* | ||
|
||
### Registering the service provider (Laravel) | ||
|
||
For Laravel 5.4 and lower, add the following line to your ``config/app.php``: | ||
|
||
```php | ||
/* | ||
* Package Service Providers... | ||
*/ | ||
Mtownsend\RemoveBg\Providers\RemoveBgServiceProvider::class, | ||
``` | ||
|
||
For Laravel 5.5 and greater, the package will auto register the provider for you. | ||
|
||
### Using Lumen | ||
|
||
To register the service provider, add the following line to ``app/bootstrap/app.php``: | ||
|
||
```php | ||
$app->register(Mtownsend\RemoveBg\Providers\RemoveBgServiceProvider::class,); | ||
``` | ||
|
||
### Publishing the config file (Laravel) | ||
|
||
```` | ||
php artisan vendor:publish --provider="Mtownsend\RemoveBg\Providers\RemoveBgServiceProvider" | ||
```` | ||
|
||
Once your ``removebg.php`` has been published your to your config folder, add the api key you obtained from [Remove.bg](https://www.remove.bg/). If you are using Laravel and put your remove.bg api key in the config file, Laravel will automatically set your api key every time you instantiate the class through the helper or facade. | ||
|
||
## Quick start | ||
|
||
### Using the class | ||
|
||
```php | ||
use Mtownsend\RemoveBg\RemoveBg; | ||
|
||
$url = '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. | ||
``` | ||
|
||
### Using the global helper (Laravel) | ||
|
||
If you are using Laravel, this package provides a convenient helper function which is globally accessible. | ||
|
||
```php | ||
removebg()->url($absoluteUrl)->save('path/to/your/file.png'); | ||
``` | ||
|
||
### Using the facade (Laravel) | ||
|
||
If you are using Laravel, this package provides a facade. To register the facade add the following line to your ``config/app.php`` under the ``aliases`` key. | ||
|
||
````php | ||
'RemoveBg' => Mtownsend\RemoveBg\Facades\RemoveBg::class, | ||
```` | ||
|
||
```php | ||
use RemoveBg; | ||
|
||
RemoveBg::file($pathToFile)->save('path/to/your/file.png'); | ||
``` | ||
|
||
## Credits | ||
|
||
- Mark Townsend | ||
- [All Contributors](../../contributors) | ||
|
||
## Testing | ||
|
||
*Tests coming soon...* | ||
|
||
You can run the tests with: | ||
|
||
```bash | ||
./vendor/bin/phpunit | ||
``` | ||
|
||
## License | ||
|
||
The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
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,51 @@ | ||
{ | ||
"name": "mtownsend/remove-bg", | ||
"description": "A PHP package to interface with the remove.bg api.", | ||
"keywords": [ | ||
"remove", | ||
"bg", | ||
"background", | ||
"image", | ||
"php", | ||
"transparent", | ||
"image", | ||
"photo", | ||
"api" | ||
], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Mark Townsend", | ||
"email": "[email protected]", | ||
"role": "Developer" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Mtownsend\\RemoveBg\\": "src" | ||
}, | ||
"files": [ | ||
"src/helpers.php" | ||
] | ||
}, | ||
"require": { | ||
"php": "~7.0", | ||
"guzzlehttp/guzzle": "^6.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^6.4" | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Mtownsend\\RemoveBg\\Test\\": "tests/" | ||
} | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Mtownsend\\RemoveBg\\Providers\\RemoveBgServiceProvider" | ||
] | ||
} | ||
}, | ||
"minimum-stability": "stable" | ||
} |
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,22 @@ | ||
<?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="MyPackage Test Suite"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src/</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
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,18 @@ | ||
<?php | ||
|
||
namespace Mtownsend\RemoveBg\Facades; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
class RemoveBg extends Facade | ||
{ | ||
/** | ||
* Get the registered name of the component. | ||
* | ||
* @return string | ||
*/ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return 'removebg'; | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
namespace Mtownsend\RemoveBg\Providers; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
use Mtownsend\RemoveBg\RemoveBg; | ||
|
||
class RemoveBgServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Bootstrap any application services. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
$this->publishes([ | ||
__DIR__ . '/../config/removebg.php' => config_path('removebg.php') | ||
], 'config'); | ||
} | ||
|
||
/** | ||
* Register any application services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->app->bind('removebg', function ($app, $data) { | ||
if (!isset($data['api_key'])) { | ||
$data['api_key'] = config('removebg.api_key'); | ||
} | ||
if (!isset($data['headers'])) { | ||
$data['headers'] = []; | ||
} | ||
return new RemoveBg($data['api_key'], $data['headers']); | ||
}); | ||
} | ||
} |
Oops, something went wrong.