PHP Image Edit Demo
<?php
// Composer autoloader
require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
// Test variables
$model = "flux-kontext-max";
$queue = ['prompt' => 'Change to blue'];
$correctedImg = 'scene1.png'; // image file path
// Create Guzzle client
$client = new Client();
// Build multipart payload (aligned with API docs)
$multipart = [
[
'name' => 'model',
'contents' => $model
],
[
'name' => 'prompt',
'contents' => $queue['prompt']
],
[
'name' => 'n',
'contents' => '1'
],
[
'name' => 'size',
'contents' => '1024x1024'
],
[
'name' => 'response_format',
'contents' => 'b64_json'
]
];
// Attach image file when present
if ($correctedImg !== false && file_exists($correctedImg)) {
$multipart[] = [
'name' => 'image',
'contents' => fopen($correctedImg, 'r'),
'filename' => basename($correctedImg),
'headers' => [
'Content-Type' => 'image/png'
]
];
echo "Image attached: " . $correctedImg . "\n";
} else {
echo "Warning: image missing or bad path: " . $correctedImg . "\n";
}
// Log request info
echo "Model: " . $model . "\n";
echo "Prompt: " . $queue['prompt'] . "\n";
echo "Image: " . $correctedImg . "\n\n";
// Send HTTP request
try {
echo "Sending request...\n";
$response = $client->request('POST', 'https://4zapi.com/v1/images/edits', [
'headers' => [
'Authorization' => 'Bearer sk-your-key',
'Accept' => 'application/json'
],
'multipart' => $multipart,
'timeout' => 60, // 60s timeout
'debug' => true // enable debug for request details
]);
$statusCode = $response->getStatusCode();
$responseBody = $response->getBody()->getContents();
echo "Success!\n";
echo "Status: " . $statusCode . "\n";
echo "Body:\n" . $responseBody . "\n";
$responseData = json_decode($responseBody, true);
if (isset($responseData['data']) && isset($responseData['data'][0]['url'])) {
echo "\nImage URL: " . $responseData['data'][0]['url'] . "\n";
}
} catch (GuzzleHttp\Exception\ClientException $e) {
echo "Client error (4xx): " . $e->getMessage() . "\n";
echo "Body: " . $e->getResponse()->getBody()->getContents() . "\n";
} catch (GuzzleHttp\Exception\ServerException $e) {
echo "Server error (5xx): " . $e->getMessage() . "\n";
echo "Body: " . $e->getResponse()->getBody()->getContents() . "\n";
} catch (Exception $e) {
echo "Request failed: " . $e->getMessage() . "\n";
}