PHPackages                             mtownsend/remove-bg - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. mtownsend/remove-bg

ActiveLibrary[API Development](/categories/api)

mtownsend/remove-bg
===================

A PHP package to interface with the remove.bg api.

2.0.1(2y ago)183315.4k↑72.4%30[1 PRs](https://github.com/mtownsend5512/remove-bg/pulls)MITPHPPHP ~7.0|~8.0CI failing

Since Jan 1Pushed 2mo ago12 watchersCompare

[ Source](https://github.com/mtownsend5512/remove-bg)[ Packagist](https://packagist.org/packages/mtownsend/remove-bg)[ RSS](/packages/mtownsend-remove-bg/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (2)Versions (10)Used By (0)

Programmatically remove backgrounds from your images using the remove.bg api.

[![](https://camo.githubusercontent.com/91a104e03e603bc32f0c541652ef2c1f3928637dc9770d985f629a365e73c1b6/68747470733a2f2f692e696d6775722e636f6d2f52726d7573756f2e706e67)](https://camo.githubusercontent.com/91a104e03e603bc32f0c541652ef2c1f3928637dc9770d985f629a365e73c1b6/68747470733a2f2f692e696d6775722e636f6d2f52726d7573756f2e706e67)

- [Register an account and obtain your api key](https://www.remove.bg/users/sign_up)
- [Remove.bg Developer Documentation](https://www.remove.bg/api)

Installation
------------

[](#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 users)

[](#registering-the-service-provider-laravel-users)

For Laravel 5.4 and lower, add the following line to your `config/app.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

[](#using-lumen)

To register the service provider, add the following line to `app/bootstrap/app.php`:

```
$app->register(Mtownsend\RemoveBg\Providers\RemoveBgServiceProvider::class);
```

### Publishing the config file (Laravel users)

[](#publishing-the-config-file-laravel-users)

```
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
-----------

[](#quick-start)

### Using the class

[](#using-the-class)

```
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.
```

### Advanced usage

[](#advanced-usage)

Remove.bg offers several request body parameters for each api call. For an up to date list, you should always check the [remove.bg api documentation](https://www.remove.bg/api).

Here is an example of an api call configured with specific request body parameters.

```
$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');
```

You may also directly specify request header parameters. As of right now this does not appear to offer much functionality in terms of how the Remove.bg api will consume these headers, but we thought it was important to expose this functionality. Consider the following example:

```
$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');
```

### Account details

[](#account-details)

The Remove.bg api offers an endpoint to check your account's credit balance and free api call usage. If your application needs to check your available credits before processing images this package makes it an absolute breeze!

The following code example is how you can programmatically check your account information. Note, the `account` method has one optional argument: `$getResponseAsObject = true`. By default your response will be returned as an object. You can return the response as an associative array by passing `false` to the `account(false)` method.

```
$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"
      }
    }
  }
}
```

To access your total credits you could do so like this: `$account->data->attributes->credits->total`.

A practical example could look something like the following:

```
$removebg = new RemoveBg($apiKey);

$account = $removebg->account();

if ($account->data->attributes->credits->total >= 1) {
	$removebg->url($absoluteUrl)->save('path/to/your/file.png');
}
```

### Using the global helper (Laravel users)

[](#using-the-global-helper-laravel-users)

If you are using Laravel, this package provides a convenient helper function which is globally accessible.

```
removebg()->url($absoluteUrl)->save(public_path('path/to/your/file.png'));
```

### Using the facade (Laravel users)

[](#using-the-facade-laravel-users)

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.

```
'RemoveBg' => Mtownsend\RemoveBg\Facades\RemoveBg::class,
```

```
use RemoveBg;

RemoveBg::file($pathToFile)->save(public_path('path/to/your/file.png'));
```

Credits
-------

[](#credits)

- Mark Townsend
- [All Contributors](../../contributors)

Testing
-------

[](#testing)

*Tests coming soon...*

You can run the tests with:

```
./vendor/bin/phpunit
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

55

—

FairBetter than 98% of packages

Maintenance57

Moderate activity, may be stable

Popularity54

Moderate usage in the ecosystem

Community19

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 90.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~202 days

Recently: every ~347 days

Total

9

Last Release

1075d ago

Major Versions

0.0.1 → 1.0.02019-01-25

1.3.0 → 2.0.02021-04-05

PHP version history (2 changes)0.0.1PHP ~7.0

1.3.0PHP ~7.0|~8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/812987?v=4)[mtownsend](/maintainers/mtownsend)[@mtownsend](https://github.com/mtownsend)

---

Top Contributors

[![mtownsend5512](https://avatars.githubusercontent.com/u/4945553?v=4)](https://github.com/mtownsend5512 "mtownsend5512 (19 commits)")[![skalero01](https://avatars.githubusercontent.com/u/2976641?v=4)](https://github.com/skalero01 "skalero01 (1 commits)")[![tistre](https://avatars.githubusercontent.com/u/3024544?v=4)](https://github.com/tistre "tistre (1 commits)")

---

Tags

apibackgroundbglaravelphppngremovetransparentphpapiimagebackgroundphotoremovebgtransparent

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mtownsend-remove-bg/health.svg)

```
[![Health](https://phpackages.com/badges/mtownsend-remove-bg/health.svg)](https://phpackages.com/packages/mtownsend-remove-bg)
```

###  Alternatives

[openai-php/laravel

OpenAI PHP for Laravel is a supercharged PHP API client that allows you to interact with the Open AI API

3.7k7.6M74](/packages/openai-php-laravel)[hubspot/api-client

Hubspot API client

23914.2M16](/packages/hubspot-api-client)[theodo-group/llphant

LLPhant is a library to help you build Generative AI applications.

1.5k311.5k5](/packages/theodo-group-llphant)[resend/resend-php

Resend PHP library.

574.7M21](/packages/resend-resend-php)[checkout/checkout-sdk-php

Checkout.com SDK for PHP

553.3M7](/packages/checkout-checkout-sdk-php)[dantsu/php-osm-static-api

PHP library to easily get static image from OpenStreetMap (OSM), add markers and draw lines.

97141.0k1](/packages/dantsu-php-osm-static-api)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
