Skip to content

Commit 00f6944

Browse files
committed
Added support for request body parameters
1.1.0 adds support for adding custom request body parameters. The remove.bg api has developed beyond its first initial release where it only allowed a limited set of request body parameters. Now you can specify paramters such as, "type", "format", "roi", "crop", "bg_color" and many more. This package now supports all of these request body parameters with the 'body' method. Happy background removing!
1 parent d493b40 commit 00f6944

File tree

2 files changed

+71
-11
lines changed

2 files changed

+71
-11
lines changed

README.md

+20-4
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,38 @@ file_put_contents('path/to/your/file5.png', base64_decode($base64Url));
8686

8787
### Advanced usage
8888

89-
Remove.bg offers several 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).
89+
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).
9090

91-
Here is an example of an api call configured with specific parameters.
91+
Here is an example of an api call configured with specific request body parameters.
9292

9393
````php
9494
$removebg = new RemoveBg($apiKey);
9595

9696
// Directly saving files
9797
$removebg->url($absoluteUrl)
98-
->headers([
98+
->body([
9999
'size' => '4k', // regular, medium, hd, 4k, auto
100-
'type' => 'product', // auto, person, product
100+
'bg_color' => '#CBD5E0',
101+
'add_shadow' => true, // primarily used for automotive photos as of the time this documentation was written
101102
'channels' => 'rgba', // rgba, alpha
102103
])
103104
->save('path/to/your/file.png');
104105
````
106+
107+
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:
108+
109+
````php
110+
$removebg = new RemoveBg($apiKey);
111+
112+
// Directly saving files
113+
$removebg->url($absoluteUrl)
114+
->headers([
115+
'X-Foo-Header' => 'Some Bar Value',
116+
'X-Foo-Header-2' => 'Some Bar Value 2',
117+
])
118+
->save('path/to/your/file.png');
119+
````
120+
105121
### Using the global helper (Laravel)
106122

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

src/RemoveBg.php

+51-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ class RemoveBg
1818
*/
1919
public $apiKey;
2020

21+
/**
22+
* Additional request parameters to be included in the body of the api call
23+
* @var array
24+
*/
25+
public $body = [];
26+
2127
/**
2228
* The remove.bg url endpoint used for the request
2329
*
@@ -106,6 +112,19 @@ public function base64(string $base64)
106112
return $this;
107113
}
108114

115+
/**
116+
* Accepts an associative array of data which will be
117+
* added to the request body of the api call
118+
*
119+
* @param array $body
120+
* @return Mtownsend\RemoveBg\RemoveBg
121+
*/
122+
public function body(array $body)
123+
{
124+
$this->body = array_merge($this->body, $body);
125+
return $this;
126+
}
127+
109128
/**
110129
* Return the fully qualified remove.bg url
111130
* to which an api request will be sent
@@ -152,6 +171,29 @@ protected function formatFileName($filename)
152171
return substr($filename, 0, strpos($filename, '.')) . '.png';
153172
}
154173

174+
/**
175+
* Formats the additional request body into the
176+
* acceptable format for the remove.bg api
177+
*
178+
* @return array
179+
*/
180+
protected function formatBody()
181+
{
182+
switch ($this->imageFormat) {
183+
case 'image_file':
184+
$index = 0;
185+
foreach ($this->body as $key => $value) {
186+
$data[$index]['name'] = $key;
187+
$data[$index]['contents'] = $value;
188+
$index++;
189+
}
190+
break;
191+
default:
192+
$data = $this->body;
193+
}
194+
return $data;
195+
}
196+
155197
/**
156198
* Formats the request into the acceptable
157199
* format for the remove.bg api
@@ -162,17 +204,19 @@ protected function formatPayload()
162204
{
163205
switch ($this->imageFormat) {
164206
case 'image_url':
165-
return ['form_params' => [$this->imageFormat => $this->payload]];
207+
return ['form_params' => array_merge([$this->imageFormat => $this->payload], $this->formatBody())];
166208
break;
167209
case 'image_file':
168-
return ['multipart' => [[
169-
'name' => 'image_file',
170-
'contents' => file_get_contents($this->payload),
171-
'filename' => $this->formatFileName($this->fileName)
172-
]]];
210+
return ['multipart' => array_merge([
211+
[
212+
'name' => 'image_file',
213+
'contents' => file_get_contents($this->payload),
214+
'filename' => $this->formatFileName($this->fileName)
215+
]
216+
], $this->formatBody())];
173217
break;
174218
case 'image_file_b64':
175-
return ['form_params' => [$this->imageFormat => $this->payload]];
219+
return ['form_params' => array_merge([$this->imageFormat => $this->payload], $this->formatBody())];
176220
break;
177221
}
178222
}

0 commit comments

Comments
 (0)