PHP code example of lanos / php-bfl
1. Go to this page and download the library: Download lanos/php-bfl library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lanos / php-bfl example snippets
anos\PHPBFL\FluxClient;
use Lanos\PHPBFL\Builders\ImageRequestBuilder;
// Initialize the client with your API key
$client = new FluxClient('your-api-key-here');
// Generate an image with FLUX1 Pro
$request = ImageRequestBuilder::create()
->withPrompt('A majestic dragon soaring through clouds at sunset')
->withDimensions(1024, 768)
->asJpeg()
->buildFlux1Pro();
$response = $client->imageGeneration()->flux1Pro($request);
// Wait for the image to be generated
$result = $client->utility()->waitForCompletion($response->id);
if ($result->isSuccessful()) {
echo "Image generated successfully: " . $result->getResultAsString();
} else {
echo "Generation failed: " . $result->status->value;
}
// Option 1: Environment variable
$client = new FluxClient(getenv('BFL_API_KEY'));
// Option 2: Direct assignment
$client = new FluxClient('your-api-key-here');
// FLUX1 Pro - High quality, balanced performance
$request = ImageRequestBuilder::create()
->withPrompt('A cyberpunk cityscape at night')
->withAspectRatio('16:9')
->withSteps(50)
->buildFlux1Pro();
$response = $client->imageGeneration()->flux1Pro($request);
// FLUX1 Dev - Development model, faster generation
$response = $client->imageGeneration()->flux1Dev([
'prompt' => 'A serene mountain landscape',
'width' => 512,
'height' => 512,
'steps' => 28
]);
// FLUX 1.1 Pro Ultra - Highest quality
$response = $client->imageGeneration()->flux11ProUltra([
'prompt' => 'An abstract artistic composition',
'aspect_ratio' => '1:1',
'raw' => false
]);
// Image Fill/Inpainting
$response = $client->imageGeneration()->flux1Fill([
'image' => base64_encode(file_get_contents('input.jpg')),
'mask' => base64_encode(file_get_contents('mask.png')),
'prompt' => 'A beautiful garden'
]);
// Image Expansion
$response = $client->imageGeneration()->flux1Expand([
'image' => base64_encode(file_get_contents('input.jpg')),
'top' => 100,
'bottom' => 100,
'left' => 50,
'right' => 50,
'prompt' => 'Expand with matching scenery'
]);
// Canny Edge Control
$response = $client->imageGeneration()->flux1Canny([
'prompt' => 'A realistic portrait',
'control_image' => base64_encode(file_get_contents('edges.jpg')),
'canny_low_threshold' => 50,
'canny_high_threshold' => 200
]);
// Create a fine-tune
$finetuneResponse = $client->finetune()->create([
'file_data' => base64_encode(file_get_contents('training_data.zip')),
'finetune_comment' => 'My custom style',
'mode' => 'style',
'trigger_word' => 'MYSTYLE',
'iterations' => 500
]);
// List your fine-tunes
$finetunes = $client->finetune()->listMyFinetunes();
print_r($finetunes['finetunes']);
// Generate with a fine-tuned model
$response = $client->finetune()->generateWithFinetunedPro([
'finetune_id' => 'your-finetune-id',
'prompt' => 'MYSTYLE a beautiful landscape',
'finetune_strength' => 1.2
]);
// Delete a fine-tune
$deleteResponse = $client->finetune()->delete('finetune-id-to-delete');
// Get task status
$result = $client->utility()->getResult('task-id');
// Poll with custom settings
$result = $client->utility()->pollResult(
taskId: 'task-id',
maxAttempts: 100,
delaySeconds: 3
);
// Check if task is complete
$isComplete = $client->utility()->isTaskComplete('task-id');
// Get progress if available
$progress = $client->utility()->getProgress('task-id');
if ($progress !== null) {
echo "Progress: {$progress}%";
}
$request = ImageRequestBuilder::create()
->withPrompt('A magical forest with glowing mushrooms')
->withImagePrompt(base64_encode(file_get_contents('reference.jpg')))
->withAspectRatio('4:3', 1024) // 4:3 ratio with base size 1024
->withSteps(75)
->withGuidance(3.5)
->withSafetyTolerance(1) // Stricter content filtering
->withRandomSeed() // Generate random seed
->asPng() // PNG output format
->withWebhook('https://your-site.com/webhook', 'secret-key')
->buildFlux1Pro();
$response = $client->imageGeneration()->flux1Pro($request);
use Lanos\PHPBFL\Exceptions\FluxApiException;
use Lanos\PHPBFL\Exceptions\AuthenticationException;
try {
$response = $client->imageGeneration()->flux1Pro($request);
$result = $client->utility()->waitForCompletion($response->id);
if ($result->isFailed()) {
echo "Generation failed: " . $result->status->value;
}
} catch (AuthenticationException $e) {
echo "Authentication failed: " . $e->getMessage();
} catch (FluxApiException $e) {
if ($e->isClientError()) {
echo "Client error: " . $e->getFriendlyMessage();
} elseif ($e->isServerError()) {
echo "Server error: " . $e->getFriendlyMessage();
} else {
echo "API error: " . $e->getMessage();
}
}
$result = $client->utility()->getResult('task-id');
// Check status
if ($result->isComplete()) {
echo "Task completed!";
}
if ($result->isSuccessful()) {
// Get the result
$imageUrl = $result->getResultAsString();
$imageData = $result->getResultAsArray();
}
if ($result->isInProgress()) {
$progress = $result->getProgressPercentage();
echo "Progress: {$progress}%";
}
// Access raw properties
echo "Task ID: " . $result->id;
echo "Status: " . $result->status->value;
$client = new FluxClient('your-api-key', [
'timeout' => 60,
'connect_timeout' => 10,
'headers' => [
'User-Agent' => 'My App/1.0'
],
'proxy' => 'http://proxy.example.com:8080'
]);
// ✅ Good: Wait for completion
$response = $client->imageGeneration()->flux1Pro($request);
$result = $client->utility()->waitForCompletion($response->id);
// ❌ Bad: Don't forget to poll for results
$response = $client->imageGeneration()->flux1Pro($request);
// Result is not ready immediately!
// For strict content filtering
$builder->withSafetyTolerance(0);
// For balanced filtering (default)
$builder->withSafetyTolerance(2);
// For lenient filtering
$builder->withSafetyTolerance(6);
// ✅ Good: Use multiples of 32
$builder->withDimensions(1024, 768);
// ❌ Bad: Non-multiple of 32
$builder->withDimensions(1000, 750); // Will cause validation errors
try {
$response = $client->imageGeneration()->flux1Pro($request);
} catch (FluxApiException $e) {
if ($e->getCode() === 429) {
// Rate limited, wait and retry
sleep(60);
$response = $client->imageGeneration()->flux1Pro($request);
}
}
new FluxClient(string $apiKey, array $options = [])
$request = ImageRequestBuilder::create()
->withPrompt('A majestic dragon')
->withDimensions(1024, 768)
->buildFlux1Pro();
$response = $client->imageGeneration()->flux1Pro($request);
$response = $client->imageGeneration()->flux1Dev([
'prompt' => 'A serene mountain landscape',
'width' => 512,
'height' => 512,
'steps' => 28
]);
$response = $client->imageGeneration()->flux11ProUltra([
'prompt' => 'An abstract artistic composition',
'aspect_ratio' => '1:1',
'raw' => false
]);
$response = $client->imageGeneration()->flux1Fill([
'image' => base64_encode(file_get_contents('input.jpg')),
'mask' => base64_encode(file_get_contents('mask.png')),
'prompt' => 'A beautiful garden'
]);
$response = $client->imageGeneration()->flux1Expand([
'image' => base64_encode(file_get_contents('input.jpg')),
'top' => 100,
'bottom' => 100,
'prompt' => 'Expand with matching scenery'
]);
$response = $client->imageGeneration()->flux1Canny([
'prompt' => 'A realistic portrait',
'control_image' => base64_encode(file_get_contents('edges.jpg')),
'canny_low_threshold' => 50,
'canny_high_threshold' => 200
]);
$response = $client->finetune()->create([
'file_data' => base64_encode(file_get_contents('training_data.zip')),
'finetune_comment' => 'My custom style',
'mode' => 'style',
'trigger_word' => 'MYSTYLE',
'iterations' => 500
]);
$finetunes = $client->finetune()->listMyFinetunes();
print_r($finetunes['finetunes']);
$response = $client->finetune()->generateWithFinetunedPro([
'finetune_id' => 'your-finetune-id',
'prompt' => 'MYSTYLE a beautiful landscape',
'finetune_strength' => 1.2
]);
$result = $client->utility()->getResult('task-id-12345');
if ($result->isSuccessful()) {
$imageUrl = $result->getResultAsString();
}
$result = $client->utility()->pollResult('task-id', 100, 3);
$result = $client->utility()->waitForCompletion('task-id');
$builder = ImageRequestBuilder::create();
$builder->withDimensions(1024, 768);
$builder->withAspectRatio('16:9', 1024); // Results in 1024x576
$request = ImageRequestBuilder::create()
->withPrompt('A magical forest')
->withAspectRatio('4:3', 1024)
->withSteps(50)
->withGuidance(3.5)
->withRandomSeed()
->asPng()
->buildFlux1Pro();
$result = $client->utility()->getResult('task-id');
if ($result->isSuccessful()) {
echo "Image URL: " . $result->getResultAsString();
} elseif ($result->isFailed()) {
echo "Task failed: " . $result->status->value;
} elseif ($result->isInProgress()) {
$progress = $result->getProgressPercentage();
echo "Progress: {$progress}%";
}
bash
composer