PHP 7 Laravel Script to Remove Background From Image Using Remove.BG API in Browser

You are currently viewing PHP 7 Laravel Script to Remove Background From Image Using Remove.BG API in Browser

PHP 7 Laravel Script to Remove Background From Image Using remove.bg API in Browser

composer require mtownsend/remove-bg
 

<span class="pl-c">/*</span>
<span class="pl-c"> * Package Service Providers...</span>
<span class="pl-c"> */</span>
<span class="pl-v">Mtownsend</span>\<span class="pl-v">RemoveBg</span>\<span class="pl-v">Providers</span>\<span class="pl-v">RemoveBgServiceProvider</span>::class,

<span class="pl-s1"><span class="pl-c1">
lt;/span>app</span>-><span class="pl-en">register</span>(<span class="pl-v">Mtownsend</span>\<span class="pl-v">RemoveBg</span>\<span class="pl-v">Providers</span>\<span class="pl-v">RemoveBgServiceProvider</span>::class);

php artisan vendor:publish --provider="Mtownsend\RemoveBg\Providers\RemoveBgServiceProvider"
 

<span class="pl-k">use</span> <span class="pl-v">Mtownsend</span>\<span class="pl-v">RemoveBg</span>\<span class="pl-v">RemoveBg</span>;
 
<span class="pl-s1"><span class="pl-c1">
lt;/span>absoluteUrl</span> = <span class="pl-s">'https://yoursite.com/images/photo.jpg'</span>;
<span class="pl-s1"><span class="pl-c1">
lt;/span>pathToFile</span> = <span class="pl-s">'images/avatar.jpg'</span>;
<span class="pl-s1"><span class="pl-c1">
lt;/span>base64EncodedFile</span> = <span class="pl-en">base64_encode</span>(<span class="pl-en">file_get_contents</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>pathToFile</span>));
 
<span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span> = <span class="pl-k">new</span> <span class="pl-v">RemoveBg</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>apiKey</span>);
 
<span class="pl-c">// Directly saving files</span>
<span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span>-><span class="pl-en">url</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>absoluteUrl</span>)-><span class="pl-en">save</span>(<span class="pl-s">'path/to/your/file.png'</span>);
<span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span>-><span class="pl-en">file</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>pathToFile</span>)-><span class="pl-en">save</span>(<span class="pl-s">'path/to/your/file2.png'</span>);
<span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span>-><span class="pl-en">base64</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>base64EncodedFile</span>)-><span class="pl-en">save</span>(<span class="pl-s">'path/to/your/file3.png'</span>);
 
<span class="pl-c">// Getting the file's raw contents to save or do something else with</span>
<span class="pl-s1"><span class="pl-c1">
lt;/span>rawUrl</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span>-><span class="pl-en">url</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>absoluteUrl</span>)-><span class="pl-en">get</span>();
<span class="pl-s1"><span class="pl-c1">
lt;/span>rawFile</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span>-><span class="pl-en">file</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>pathToFile</span>)-><span class="pl-en">get</span>();
<span class="pl-s1"><span class="pl-c1">
lt;/span>rawBase64</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span>-><span class="pl-en">base64</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>base64EncodedFile</span>)-><span class="pl-en">get</span>();
 
<span class="pl-en">file_put_contents</span>(<span class="pl-s">'path/to/your/file4.png'</span>, <span class="pl-s1"><span class="pl-c1">
lt;/span>rawUrl</span>);
<span class="pl-c">// etc...</span>
 
<span class="pl-c">// Getting the file's base64 encoded contents from the api</span>
<span class="pl-s1"><span class="pl-c1">
lt;/span>base64Url</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span>-><span class="pl-en">url</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>absoluteUrl</span>)-><span class="pl-en">getBase64</span>();
<span class="pl-s1"><span class="pl-c1">
lt;/span>base64File</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span>-><span class="pl-en">file</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>pathToFile</span>)-><span class="pl-en">getBase64</span>();
<span class="pl-s1"><span class="pl-c1">
lt;/span>base64Base64</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span>-><span class="pl-en">base64</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>base64EncodedFile</span>)-><span class="pl-en">getBase64</span>();
 
<span class="pl-en">file_put_contents</span>(<span class="pl-s">'path/to/your/file5.png'</span>, <span class="pl-en">base64_decode</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>base64Url</span>));
<span class="pl-c">// etc...</span>
 
<span class="pl-c">// Please note: remove.bg returns all images in .png format, so you should be saving all files received from the api as .png.</span>

<span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span> = <span class="pl-k">new</span> <span class="pl-v">RemoveBg</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>apiKey</span>);
 
<span class="pl-c">// Directly saving files</span>
<span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span>-><span class="pl-en">url</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>absoluteUrl</span>)
-><span class="pl-en">body</span>([
    <span class="pl-s">'size'</span> => <span class="pl-s">'4k'</span>, <span class="pl-c">// regular, medium, hd, 4k, auto</span>
    <span class="pl-s">'bg_color'</span> => <span class="pl-s">'#CBD5E0'</span>,
    <span class="pl-s">'add_shadow'</span> => <span class="pl-c1">true</span>, <span class="pl-c">// primarily used for automotive photos as of the time this documentation was written</span>
    <span class="pl-s">'channels'</span> => <span class="pl-s">'rgba'</span>, <span class="pl-c">// rgba, alpha</span>
])
-><span class="pl-en">save</span>(<span class="pl-s">'path/to/your/file.png'</span>);

<span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span> = <span class="pl-k">new</span> <span class="pl-v">RemoveBg</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>apiKey</span>);
 
<span class="pl-c">// Directly saving files</span>
<span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span>-><span class="pl-en">url</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>absoluteUrl</span>)
-><span class="pl-en">headers</span>([
    <span class="pl-s">'X-Foo-Header'</span> => <span class="pl-s">'Some Bar Value'</span>,
    <span class="pl-s">'X-Foo-Header-2'</span> => <span class="pl-s">'Some Bar Value 2'</span>,
])
-><span class="pl-en">save</span>(<span class="pl-s">'path/to/your/file.png'</span>);

<span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span> = <span class="pl-k">new</span> <span class="pl-v">RemoveBg</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>apiKey</span>);
 
<span class="pl-s1"><span class="pl-c1">
lt;/span>account</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span>-><span class="pl-en">account</span>();
 
<span class="pl-c">// $account will be something like this:</span>
{
  <span class="pl-s">"data"</span>: {
    <span class="pl-s">"attributes"</span>: {
      <span class="pl-s">"credits"</span>: {
        <span class="pl-s">"total"</span>: <span class="pl-c1">200</span>,
        <span class="pl-s">"subscription"</span>: <span class="pl-c1">150</span>,
        <span class="pl-s">"payg"</span>: <span class="pl-c1">50</span>
      },
      <span class="pl-s">"api"</span>: {
        <span class="pl-s">"free_calls"</span>: <span class="pl-c1">50</span>,
        <span class="pl-s">"sizes"</span>: <span class="pl-s">"all"</span>
      }
    }
  }
}

<span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span> = <span class="pl-k">new</span> <span class="pl-v">RemoveBg</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>apiKey</span>);
 
<span class="pl-s1"><span class="pl-c1">
lt;/span>account</span> = <span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span>-><span class="pl-en">account</span>();
 
<span class="pl-k">if</span> (<span class="pl-s1"><span class="pl-c1">
lt;/span>account</span>-><span class="pl-c1">data</span>-><span class="pl-c1">attributes</span>-><span class="pl-c1">credits</span>-><span class="pl-c1">total</span> >= <span class="pl-c1">1</span>) {
<span class="pl-s1"><span class="pl-c1">
lt;/span>removebg</span>-><span class="pl-en">url</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>absoluteUrl</span>)-><span class="pl-en">save</span>(<span class="pl-s">'path/to/your/file.png'</span>);
}

<span class="pl-en">removebg</span>()-><span class="pl-en">url</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>absoluteUrl</span>)-><span class="pl-en">save</span>(<span class="pl-en">public_path</span>(<span class="pl-s">'path/to/your/file.png'</span>));

<span class="pl-s">'RemoveBg'</span> => <span class="pl-v">Mtownsend</span>\<span class="pl-v">RemoveBg</span>\<span class="pl-v">Facades</span>\<span class="pl-v">RemoveBg</span>::class,

<span class="pl-k">use</span> <span class="pl-v">RemoveBg</span>;
 
<span class="pl-v">RemoveBg</span>::<span class="pl-en">file</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>pathToFile</span>)-><span class="pl-en">save</span>(<span class="pl-en">public_path</span>(<span class="pl-s">'path/to/your/file.png'</span>));

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply