All Projects → mtownsend5512 → Remove Bg

mtownsend5512 / Remove Bg

Licence: mit
Programmatically remove backgrounds from your images using the remove.bg api

Projects that are alternatives of or similar to Remove Bg

Wizard
Wizard是一款开源的文档管理工具,支持Markdown/Swagger/Table类型的文档。
Stars: ✭ 1,733 (+1297.58%)
Mutual labels:  api, laravel
Laravel Hackathon Starter
💻 A hackathon/MVP boilerplate for laravel web applications. Start your hackathons without hassle.
Stars: ✭ 1,589 (+1181.45%)
Mutual labels:  api, laravel
Api Restful Con Laravel Guia Definitiva
Repositorio para el código base del curso "API RESTful con Laravel - Guía Definitiva"
Stars: ✭ 95 (-23.39%)
Mutual labels:  api, laravel
Bigbluebutton
Package that provides easily communicate between bigbluebutton server and laravel framework
Stars: ✭ 85 (-31.45%)
Mutual labels:  api, laravel
Overwatch Api
A RESTful API for the Overwatch Game
Stars: ✭ 112 (-9.68%)
Mutual labels:  api, laravel
Laravel Woocommerce
WooCommerce Rest API for Laravel
Stars: ✭ 86 (-30.65%)
Mutual labels:  api, laravel
Invoice As A Service
💰 Simple invoicing service (REST API): from JSON to PDF
Stars: ✭ 106 (-14.52%)
Mutual labels:  api, laravel
Requent
A GraphQL like interface to map a request to eloquent query with data transformation for Laravel.
Stars: ✭ 78 (-37.1%)
Mutual labels:  api, laravel
Laqul
A complete starter kit that allows you create amazing apps that look native thanks to the Quasar Framework. Powered by an API developed in Laravel Framework using the easy GraphQL queries language. And ready to use the Google Firebase features.
Stars: ✭ 110 (-11.29%)
Mutual labels:  api, laravel
Totoval
An out-of-the-box artisan API web-framework written in go.
Stars: ✭ 110 (-11.29%)
Mutual labels:  api, laravel
Api Response
Simple and ready to use API response wrapper for Laravel.
Stars: ✭ 123 (-0.81%)
Mutual labels:  api, laravel
Laravel Hateoas
Expose the authorization logic of your REST API using HATEOAS links
Stars: ✭ 116 (-6.45%)
Mutual labels:  api, laravel
Lowpoly
Lowpoly image generator
Stars: ✭ 83 (-33.06%)
Mutual labels:  background, png
Laravel Prefixed Ids
Friendly prefixed IDs for Laravel models
Stars: ✭ 88 (-29.03%)
Mutual labels:  api, laravel
Laravel Blog
Laravel 8.0 blog application with Vue.js, Homestead, Horizon, Telescope and Pusher
Stars: ✭ 1,248 (+906.45%)
Mutual labels:  api, laravel
Laradoo
Odoo ERP API for Laravel
Stars: ✭ 100 (-19.35%)
Mutual labels:  api, laravel
Dark Sky Api
PHP Library for the Dark Sky API.
Stars: ✭ 70 (-43.55%)
Mutual labels:  api, laravel
Laravel Vue Starter
Well Documented Laravel Starter App From Development to Production. For Full Blown RESTFUL API and SPA with Beautiful UI Using Buefy / ElementUi For Reusable Vue Components
Stars: ✭ 76 (-38.71%)
Mutual labels:  api, laravel
Vue Api Query
💎 Elegant and simple way to build requests for REST API
Stars: ✭ 1,528 (+1132.26%)
Mutual labels:  api, laravel
Laravel Api Boilerplate
A Boilerplate Project For Laravel API's (NOT MAINTAINED)
Stars: ✭ 113 (-8.87%)
Mutual labels:  api, laravel

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

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)

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

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)

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

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

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.

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

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)

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)

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

Testing

Tests coming soon...

You can run the tests with:

./vendor/bin/phpunit

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].