How to make image background transparent using PHP

How to make image background transparent using PHP

Most of the API's are paid or isn't working perfectly to remove background color from an image. There is an PHP default function imagecolortransparent() is used to define the transparent background of an image while creating image. This is very helpful API for developers & graphic designers.

In this article i will show you how you can remove background color of an image using an API and make the image background transparent without using PHP functions. For this I've used an API remove.bg. To use this API, we need an API KEY, which will we can generate after creating an account.

I will use CURL in PHP using below example in which we will remove background color of an image from an image URL. You need to pass you API Key at place of `API KEY HERE` and enter image URL at place of `Enter Your Image URL here`. Use below code to remove backgorund color from an image.

<?php

$url = "https://api.remove.bg/v1.0/removebg";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'x-api-key:API KEYE HERE',
]);

// move image_url here:
curl_setopt($ch, CURLOPT_POSTFIELDS, [
    'image_url' => 'Enter Your Image URL here',
]);

$server_output = curl_exec($ch);
curl_close($ch);

file_put_contents('wdd.png', $server_output);
?>

In above code i have use file_put_contents function to store image which getting from curl response.

Steps to create an API KEY

  • First create an account on remove.bg
  • After login go to `My Dashboard`
  • There will be a tab `API Key`
  • Click on `show` and use that key in your code

Featurs of This API

  • Save time and money: You have to hire a freelancer who takes money for work and it will take time to do that work. Manually editing an image takes so much time and effort, It will takes approx 5 seconds to make a transparent background.
  • Simple: Just use above code, change image URL and hit enter. No need to install any heavy software in your system, which results computer running slow.
  • Free & Automat: No need to do anything, it is automatically and free.

 

Hi, I'm Saurav, the developer behind usingphp. Donate to help me keep usingphp free and maintained.

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Miranda
Miranda
13 Dec 2020

Nice article. I`ve tried many other API`s, but this API work for me

Daniel
Daniel
13 Dec 2020

We can use this API 50 times in a month. You have to upgrade your plan, if you want to use it more than 50 times a month

Andrew bishop
Andrew bishop
18 Dec 2020

How It automatically removes the background from any photo? Upload image, count from 5, download image.

Aqib Aslam
Aqib Aslam
20 Dec 2020

Really helpful. Thanks to the author

Rony
Rony
20 Dec 2020

Found exact example what want without any library

Victor
Victor
27 Dec 2020

Simple and easy..

Add your comment