PHP + PixelPanda API

Remove Background with PHP

Remove image backgrounds with PHP's built-in cURL. No Composer packages, no ImageMagick, no GD library. Works with WordPress, Laravel, and WooCommerce.

$ # No installation needed — uses PHP's built-in cURL

PHP examples — copy and go

Each example is self-contained. Copy the one that matches your use case.

Quick start

Basic background removal with PHP cURL.

<?php
$apiKey = "pk_live_your_api_key";

$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL            => "https://pixelpanda.ai/api/v1/remove-background",
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => ["Authorization: Bearer $apiKey"],
    CURLOPT_POSTFIELDS     => ["file" => new CURLFile("product.jpg")],
    CURLOPT_TIMEOUT        => 30,
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

$data = json_decode($response, true);
echo $data["image_url"];          // Transparent PNG URL
echo $data["credits_remaining"];  // Credits remaining

Download the result

Save the transparent PNG to your server.

<?php
function removeBackground($imagePath, $outputPath) {
    $apiKey = getenv("PIXELPANDA_API_KEY");

    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL            => "https://pixelpanda.ai/api/v1/remove-background",
        CURLOPT_POST           => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER     => ["Authorization: Bearer $apiKey"],
        CURLOPT_POSTFIELDS     => ["file" => new CURLFile($imagePath)],
        CURLOPT_TIMEOUT        => 30,
    ]);

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($httpCode !== 200) {
        throw new Exception("API error: $httpCode - $response");
    }

    $data = json_decode($response, true);

    // Download and save the transparent PNG
    file_put_contents($outputPath, file_get_contents($data["image_url"]));

    return $data;
}

// Usage
$result = removeBackground("product.jpg", "product_nobg.png");
echo "Saved! Credits remaining: " . $result["credits_remaining"];

Process a directory

Batch process all images in a folder.

<?php
$inputDir = "products/";
$outputDir = "products_nobg/";
if (!is_dir($outputDir)) mkdir($outputDir, 0755, true);

$files = glob($inputDir . "*.{jpg,jpeg,png,webp}", GLOB_BRACE);
$total = count($files);
echo "Processing $total images...
";

foreach ($files as $i => $file) {
    $filename = basename($file);
    $outputPath = $outputDir . pathinfo($filename, PATHINFO_FILENAME) . ".png";

    try {
        removeBackground($file, $outputPath);
        echo "[" . ($i + 1) . "/$total] $filename -> $outputPath
";
    } catch (Exception $e) {
        echo "[" . ($i + 1) . "/$total] $filename FAILED: " . $e->getMessage() . "
";
    }

    usleep(500000); // 0.5s delay between requests
}

echo "Done!
";

WordPress / WooCommerce hook

Auto-remove backgrounds when product images are uploaded.

<?php
/**
 * Auto-remove background when a WooCommerce product image is uploaded.
 * Add this to your theme's functions.php or a custom plugin.
 */
add_filter("wp_handle_upload", function ($upload) {
    // Only process images
    if (strpos($upload["type"], "image/") !== 0) return $upload;

    $apiKey = defined("PIXELPANDA_API_KEY") ? PIXELPANDA_API_KEY : "";
    if (empty($apiKey)) return $upload;

    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL            => "https://pixelpanda.ai/api/v1/remove-background",
        CURLOPT_POST           => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER     => ["Authorization: Bearer $apiKey"],
        CURLOPT_POSTFIELDS     => ["file" => new CURLFile($upload["file"])],
        CURLOPT_TIMEOUT        => 30,
    ]);

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($httpCode === 200) {
        $data = json_decode($response, true);
        // Replace the uploaded file with the bg-removed version
        $pngPath = preg_replace("/\.\w+$/", "_nobg.png", $upload["file"]);
        file_put_contents($pngPath, file_get_contents($data["image_url"]));
        // You could update $upload["file"] and $upload["url"] here
        // to replace the original, or save as a separate attachment
    }

    return $upload;
});

Laravel controller

Accept an upload in Laravel and return the processed image.

<?php
// app/Http/Controllers/ImageController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class ImageController extends Controller
{
    public function removeBackground(Request $request)
    {
        $request->validate(["image" => "required|image|max:10240"]);

        $response = Http::withToken(config("services.pixelpanda.key"))
            ->attach("file", $request->file("image")->get(), $request->file("image")->getClientOriginalName())
            ->post("https://pixelpanda.ai/api/v1/remove-background");

        if ($response->successful()) {
            return response()->json($response->json());
        }

        return response()->json(["error" => "Processing failed"], 500);
    }
}

// routes/api.php
// Route::post("/remove-bg", [ImageController::class, "removeBackground"]);

Response format

Every successful request returns this JSON. The image_url is a permanent link to the transparent PNG.

{
    "success": true,
    "image_url": "https://pub-xxx.r2.dev/results/abc123.png",
    "credits_remaining": 199
}
FieldTypeDescription
successbooleantrue if background was removed successfully
image_urlstringURL to the transparent PNG result (permanent, hosted on CDN)
credits_remainingintegerHow many credits are left on your account

Error codes

CodeMeaning
200Success — image_url contains the transparent PNG
400Bad request — invalid image format or missing file
401Unauthorized — invalid or missing API key
402Payment required — out of credits
413Image too large — max 10MB
429Rate limited — too many requests

Common issues

cURL error: SSL certificate problem
Your PHP installation may have outdated CA certificates. Update them or add CURLOPT_CAINFO pointing to a current ca-bundle.crt. Don't disable SSL verification in production.
Getting empty response
Make sure CURLOPT_RETURNTRANSFER is set to true. Also check curl_error($ch) after curl_exec for connection issues.
CURLFile not found
CURLFile requires PHP 5.5+. If on an older version, use '@/path/to/file' syntax instead (deprecated but works).

Common questions

Yes — see the WordPress/WooCommerce hook example above. Use wp_handle_upload to process images automatically, or call the API from a custom admin page.
Yes. Hook into product image uploads and automatically remove backgrounds. See the WooCommerce hook example above. You can also build a bulk processing admin page.
No. All examples use PHP's built-in cURL extension, which is enabled by default on virtually all hosting. Zero external dependencies.
Yes — see the Laravel controller example above. Use Laravel's HTTP client (Http::attach) for clean, fluent API calls.
PHP 7.0+ with the cURL extension (enabled by default). CURLFile class requires PHP 5.5+.
1 credit per image ($0.029). Start with 280 credits/week for $7.99. Monthly plans from $24/month for higher volume.

Start removing backgrounds with PHP

280 credits/week for $7.99. Copy the code above and go.

Get Your API Key