PHP code example of mtownsend / remove-bg

1. Go to this page and download the library: Download mtownsend/remove-bg library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mtownsend / remove-bg example snippets


/*
 * Package Service Providers...
 */
Mtownsend\RemoveBg\Providers\RemoveBgServiceProvider::class,

$app->register(Mtownsend\RemoveBg\Providers\RemoveBgServiceProvider::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.

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

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

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

$removebg = new RemoveBg($apiKey);

$account = $removebg->account();

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

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

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

use RemoveBg;

RemoveBg::file($pathToFile)->save(public_path('path/to/your/file.png'));
`
php artisan vendor:publish --provider="Mtownsend\RemoveBg\Providers\RemoveBgServiceProvider"