Skip to content

Commit a8cba20

Browse files
committed
Beta launch
0 parents  commit a8cba20

11 files changed

+681
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.lock
2+
vendor
3+
.DS_Store

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Mark Townsend <mtownsend5512@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
Programmatically remove backgrounds from your images using the remove.bg api.
2+
3+
**Remove.bg is in private beta. This package is subject to change. Production use is not recommended until remove.bg's api leaves beta.**
4+
5+
[See the remove.bg docs for more details](https://developer.remove.bg/docs).
6+
7+
## Installation
8+
9+
Install via composer:
10+
11+
```
12+
composer require mtownsend/remove-bg
13+
```
14+
15+
*This package is designed to work with any PHP 7.0+ application but has special support for Laravel.*
16+
17+
### Registering the service provider (Laravel)
18+
19+
For Laravel 5.4 and lower, add the following line to your ``config/app.php``:
20+
21+
```php
22+
/*
23+
* Package Service Providers...
24+
*/
25+
Mtownsend\RemoveBg\Providers\RemoveBgServiceProvider::class,
26+
```
27+
28+
For Laravel 5.5 and greater, the package will auto register the provider for you.
29+
30+
### Using Lumen
31+
32+
To register the service provider, add the following line to ``app/bootstrap/app.php``:
33+
34+
```php
35+
$app->register(Mtownsend\RemoveBg\Providers\RemoveBgServiceProvider::class,);
36+
```
37+
38+
### Publishing the config file (Laravel)
39+
40+
````
41+
php artisan vendor:publish --provider="Mtownsend\RemoveBg\Providers\RemoveBgServiceProvider"
42+
````
43+
44+
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.
45+
46+
## Quick start
47+
48+
### Using the class
49+
50+
```php
51+
use Mtownsend\RemoveBg\RemoveBg;
52+
53+
$url = 'https://yoursite.com/images/photo.jpg';
54+
$pathToFile = 'images/avatar.jpg';
55+
$base64EncodedFile = base64_encode(file_get_contents($pathToFile));
56+
57+
$removebg = new RemoveBg($apiKey);
58+
59+
// Directly saving files
60+
$removebg->url($absoluteUrl)->save('path/to/your/file.png');
61+
$removebg->file($pathToFile)->save('path/to/your/file2.png');
62+
$removebg->base64($base64EncodedFile)->save('path/to/your/file3.png');
63+
64+
// Getting the file's raw contents to save or do something else with
65+
$rawUrl = $removebg->url($absoluteUrl)->get();
66+
$rawFile = $removebg->file($pathToFile)->get();
67+
$rawBase64 = $removebg->base64($base64EncodedFile)->get();
68+
69+
file_put_contents('path/to/your/file4.png', $rawUrl);
70+
// etc...
71+
72+
// Getting the file's base64 encoded contents from the api
73+
$base64Url = $removebg->url($absoluteUrl)->getBase64();
74+
$base64File = $removebg->file($pathToFile)->getBase64();
75+
$base64Base64 = $removebg->base64($base64EncodedFile)->getBase64();
76+
77+
file_put_contents('path/to/your/file5.png', base64_decode($base64Url));
78+
// etc...
79+
80+
// Please note: remove.bg returns all images in .png format, so you should be saving all files received from the api as .png.
81+
```
82+
83+
### Using the global helper (Laravel)
84+
85+
If you are using Laravel, this package provides a convenient helper function which is globally accessible.
86+
87+
```php
88+
removebg()->url($absoluteUrl)->save('path/to/your/file.png');
89+
```
90+
91+
### Using the facade (Laravel)
92+
93+
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.
94+
95+
````php
96+
'RemoveBg' => Mtownsend\RemoveBg\Facades\RemoveBg::class,
97+
````
98+
99+
```php
100+
use RemoveBg;
101+
102+
RemoveBg::file($pathToFile)->save('path/to/your/file.png');
103+
```
104+
105+
## Credits
106+
107+
- Mark Townsend
108+
- [All Contributors](../../contributors)
109+
110+
## Testing
111+
112+
*Tests coming soon...*
113+
114+
You can run the tests with:
115+
116+
```bash
117+
./vendor/bin/phpunit
118+
```
119+
120+
## License
121+
122+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"name": "mtownsend/remove-bg",
3+
"description": "A PHP package to interface with the remove.bg api.",
4+
"keywords": [
5+
"remove",
6+
"bg",
7+
"background",
8+
"image",
9+
"php",
10+
"transparent",
11+
"image",
12+
"photo",
13+
"api"
14+
],
15+
"license": "MIT",
16+
"authors": [
17+
{
18+
"name": "Mark Townsend",
19+
"email": "mtownsend5512@gmail.com",
20+
"role": "Developer"
21+
}
22+
],
23+
"autoload": {
24+
"psr-4": {
25+
"Mtownsend\\RemoveBg\\": "src"
26+
},
27+
"files": [
28+
"src/helpers.php"
29+
]
30+
},
31+
"require": {
32+
"php": "~7.0",
33+
"guzzlehttp/guzzle": "^6.0"
34+
},
35+
"require-dev": {
36+
"phpunit/phpunit": "^6.4"
37+
},
38+
"autoload-dev": {
39+
"psr-4": {
40+
"Mtownsend\\RemoveBg\\Test\\": "tests/"
41+
}
42+
},
43+
"extra": {
44+
"laravel": {
45+
"providers": [
46+
"Mtownsend\\RemoveBg\\Providers\\RemoveBgServiceProvider"
47+
]
48+
}
49+
},
50+
"minimum-stability": "stable"
51+
}

phpunit.xml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="MyPackage Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

src/Facades/RemoveBg.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Mtownsend\RemoveBg\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class RemoveBg extends Facade
8+
{
9+
/**
10+
* Get the registered name of the component.
11+
*
12+
* @return string
13+
*/
14+
protected static function getFacadeAccessor()
15+
{
16+
return 'removebg';
17+
}
18+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Mtownsend\RemoveBg\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use Mtownsend\RemoveBg\RemoveBg;
7+
8+
class RemoveBgServiceProvider extends ServiceProvider
9+
{
10+
/**
11+
* Bootstrap any application services.
12+
*
13+
* @return void
14+
*/
15+
public function boot()
16+
{
17+
$this->publishes([
18+
__DIR__ . '/../config/removebg.php' => config_path('removebg.php')
19+
], 'config');
20+
}
21+
22+
/**
23+
* Register any application services.
24+
*
25+
* @return void
26+
*/
27+
public function register()
28+
{
29+
$this->app->bind('removebg', function ($app, $data) {
30+
if (!isset($data['api_key'])) {
31+
$data['api_key'] = config('removebg.api_key');
32+
}
33+
if (!isset($data['headers'])) {
34+
$data['headers'] = [];
35+
}
36+
return new RemoveBg($data['api_key'], $data['headers']);
37+
});
38+
}
39+
}

0 commit comments

Comments
 (0)