Skip to content

Commit f84655c

Browse files
committed
Added functionality to check account details
The package now supports checking your account credit balance and details by way of the account method.
1 parent 00f6944 commit f84655c

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

README.md

+43
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,49 @@ $removebg->url($absoluteUrl)
118118
->save('path/to/your/file.png');
119119
````
120120

121+
### Account details
122+
123+
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!
124+
125+
The following code example is how you can check 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.
126+
127+
````php
128+
$removebg = new RemoveBg($apiKey);
129+
130+
$account = $removebg->account();
131+
132+
// $account will be something like this:
133+
{
134+
"data": {
135+
"attributes": {
136+
"credits": {
137+
"total": 200,
138+
"subscription": 150,
139+
"payg": 50
140+
},
141+
"api": {
142+
"free_calls": 50,
143+
"sizes": "all"
144+
}
145+
}
146+
}
147+
}
148+
````
149+
150+
To access your total credits you could do so like this: `$account->data->attributes->credits->total`.
151+
152+
A practical example could look something like the following:
153+
154+
````php
155+
$removebg = new RemoveBg($apiKey);
156+
157+
$account = $removebg->account();
158+
159+
if ($account->data->attributes->credits->total >= 1) {
160+
$removebg->url($absoluteUrl)->save('path/to/your/file.png');
161+
}
162+
````
163+
121164
### Using the global helper (Laravel)
122165

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

src/RemoveBg.php

+21
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,27 @@ public function __construct($apiKey = '', $headers = [])
9898
$this->setApiKey();
9999
}
100100

101+
/**
102+
* Fetch the account's credit balance and free API calls
103+
*
104+
* @param bool $getResponseAsObject Whether or not the response should be returned as an object or array
105+
* @return mixed remove.bg account response
106+
*/
107+
public function account($getResponseAsObject = true)
108+
{
109+
$request = new Guzzle();
110+
$this->response = $request->request('GET', str_replace('/removebg', '/account', $this->endpoint), array_merge(
111+
$this->requestOptions,
112+
['headers' => $this->headers]
113+
));
114+
115+
if ($this->hasErrors()) {
116+
return $this->throwApiException();
117+
}
118+
119+
return json_decode((string) $this->response->getBody(), $getResponseAsObject ? false : true);
120+
}
121+
101122
/**
102123
* Accepts a base64 encoded image string
103124
* and utilizes remove.bg's image_file_b64 feature

0 commit comments

Comments
 (0)