How to Remove Image Backgrounds in PHP

A complete guide to integrating FAPIhub's background removal API into your PHP project. Takes less than 10 minutes.

This guide shows how to call the FapiHub background removal API from PHP using the built-in cURL extension. No Composer packages required — cURL is available in most PHP 8.1+ installations by default.

Prerequisites

  • A FAPIhub API key (get one free at fapihub.com/dashboard)
  • PHP 8.1 or higher installed
  • Basic understanding of HTTP requests

Step-by-Step Integration

1

Install dependencies

Install any libraries needed for HTTP requests.

# cURL is built into most PHP installs
2

Get your API key

Create a free account and copy your API key from the dashboard.

3

Basic background removal

Upload an image and receive transparent PNG output.

$c = curl_init();
curl_setopt_array($c, [
  CURLOPT_URL => "https://fapihub.com/v2/rembg/",
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_POST => 1,
  CURLOPT_HTTPHEADER => ["token: YOUR_API_KEY"],
  CURLOPT_POSTFIELDS => ["image" => new CURLFile("photo.jpg")]
]);
$res = curl_exec($c);
4

Save and verify output

Write API response bytes to a PNG file and verify output.

$result = curl_exec($c);
$http_code = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);

if ($http_code === 200) {
    file_put_contents("result.png", $result);
    echo "Saved result.png\n";
} else {
    echo "Error: HTTP $http_code\n";
}
5

Handle errors

Check HTTP status codes and catch network errors.

$c = curl_init();
curl_setopt_array($c, [
    CURLOPT_URL => "https://fapihub.com/v2/rembg/",
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_POST => 1,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTPHEADER => ["token: YOUR_API_KEY"],
    CURLOPT_POSTFIELDS => ["image" => new CURLFile("photo.jpg")],
]);

$result = curl_exec($c);
$error = curl_error($c);
$http_code = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);

if ($error) {
    throw new RuntimeException("cURL error: $error");
}

match ($http_code) {
    200 => file_put_contents("result.png", $result),
    401 => throw new RuntimeException("Invalid API key"),
    429 => throw new RuntimeException("Rate limit reached — retry after delay"),
    default => throw new RuntimeException("API error: HTTP $http_code"),
};
6

Batch processing

Loop through image paths for high-volume jobs.

$files = glob("images/*.jpg");
$output_dir = "results/";

if (!is_dir($output_dir)) mkdir($output_dir);

foreach ($files as $file) {
    $c = curl_init();
    curl_setopt_array($c, [
        CURLOPT_URL => "https://fapihub.com/v2/rembg/",
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_POST => 1,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTPHEADER => ["token: YOUR_API_KEY"],
        CURLOPT_POSTFIELDS => ["image" => new CURLFile($file)],
    ]);

    $result = curl_exec($c);
    $http_code = curl_getinfo($c, CURLINFO_HTTP_CODE);
    curl_close($c);

    if ($http_code === 200) {
        $out = $output_dir . pathinfo($file, PATHINFO_FILENAME) . ".png";
        file_put_contents($out, $result);
        echo "Done: $file\n";
    } else {
        echo "Error on $file: HTTP $http_code\n";
    }
}

Advanced Usage

Blur endpoint

Apply blur to image backgrounds.

$c = curl_init();
curl_setopt_array($c, [
    CURLOPT_URL => "https://fapihub.com/v2/rembg/blur/",
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_POST => 1,
    CURLOPT_HTTPHEADER => ["token: YOUR_API_KEY"],
    CURLOPT_POSTFIELDS => [
        "image" => new CURLFile("photo.jpg"),
        "blur_radius" => "15",
    ],
]);
file_put_contents("blur_bg.png", curl_exec($c));
curl_close($c);

Color endpoint

Set RGB color backgrounds.

$c = curl_init();
curl_setopt_array($c, [
    CURLOPT_URL => "https://fapihub.com/v2/rembg/color/",
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_POST => 1,
    CURLOPT_HTTPHEADER => ["token: YOUR_API_KEY"],
    CURLOPT_POSTFIELDS => [
        "image" => new CURLFile("photo.jpg"),
        "background_color" => "255,255,255",
    ],
]);
file_put_contents("white_bg.png", curl_exec($c));
curl_close($c);

Gradient endpoint

Render gradient outputs.

$c = curl_init();
curl_setopt_array($c, [
    CURLOPT_URL => "https://fapihub.com/v2/rembg/gradient/",
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_POST => 1,
    CURLOPT_HTTPHEADER => ["token: YOUR_API_KEY"],
    CURLOPT_POSTFIELDS => [
        "image" => new CURLFile("photo.jpg"),
        "top_color" => "0,0,200",
        "bottom_color" => "255,255,255",
    ],
]);
file_put_contents("gradient_bg.png", curl_exec($c));
curl_close($c);

Shadow endpoint

Add shadow depth controls.

$c = curl_init();
curl_setopt_array($c, [
    CURLOPT_URL => "https://fapihub.com/v2/rembg/shadow/",
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_POST => 1,
    CURLOPT_HTTPHEADER => ["token: YOUR_API_KEY"],
    CURLOPT_POSTFIELDS => [
        "image" => new CURLFile("photo.jpg"),
        "offset" => "10",
        "blur" => "15",
    ],
]);
file_put_contents("shadow.png", curl_exec($c));
curl_close($c);

Ready to Start?

Free tier includes 100 full-resolution requests/month. No credit card. Your API key is ready in 30 seconds.

Get Your Free API Key