Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.pixcraft.es/llms.txt

Use this file to discover all available pages before exploring further.

PHP Integration Guide

This guide covers integration with plain PHP (cURL) and with the Guzzle HTTP client, including a Laravel-specific example.

Using cURL

<?php

$apiKey = getenv('PIXCRAFT_API_KEY');
$baseUrl = 'https://www.pixcraft.es/api/v1';

function generatePattern(string $imageUrl, string $niche, int $widthCm, int $heightCm): array {
    global $apiKey, $baseUrl;

    $ch = curl_init("$baseUrl/generate");
    curl_setopt_array($ch, [
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => [
            "Authorization: Bearer $apiKey",
            'Content-Type: application/json',
        ],
        CURLOPT_POSTFIELDS => json_encode([
            'image_url' => $imageUrl,
            'niche' => $niche,
            'width_cm' => $widthCm,
            'height_cm' => $heightCm,
        ]),
    ]);

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

    if ($httpCode !== 200) {
        $error = json_decode($response, true);
        throw new Exception("PixCraft API error: {$error['code']} — {$error['message']}");
    }

    return json_decode($response, true);
}

$pattern = generatePattern('https://example.com/photo.jpg', 'mosaics', 100, 100);
echo "Generated {$pattern['dimensions']['total_units']} tiles\n";

Using Guzzle

composer require guzzlehttp/guzzle
<?php

use GuzzleHttp\Client;

$client = new Client([
    'base_uri' => 'https://www.pixcraft.es/api/v1/',
    'headers' => [
        'Authorization' => 'Bearer ' . getenv('PIXCRAFT_API_KEY'),
        'Content-Type' => 'application/json',
    ],
]);

// Generate pattern
$response = $client->post('generate', [
    'json' => [
        'image_url' => 'https://example.com/photo.jpg',
        'niche' => 'curtains',
        'width_cm' => 200,
        'height_cm' => 250,
        'max_colors' => 8,
    ],
]);

$pattern = json_decode($response->getBody(), true);

// Save PDF
$pdfResponse = $client->post('generate/pdf', [
    'json' => [
        'image_url' => 'https://example.com/photo.jpg',
        'niche' => 'curtains',
        'width_cm' => 200,
        'height_cm' => 250,
    ],
]);

file_put_contents('pattern.pdf', $pdfResponse->getBody());

Laravel example

Service class

<?php

namespace App\Services;

use Illuminate\Support\Facades\Http;

class PixCraftService
{
    private string $baseUrl = 'https://www.pixcraft.es/api/v1';
    private string $apiKey;

    public function __construct()
    {
        $this->apiKey = config('services.pixcraft.key');
    }

    public function generate(string $imageUrl, string $niche, int $widthCm, int $heightCm): array
    {
        $response = Http::withToken($this->apiKey)
            ->post("{$this->baseUrl}/generate", [
                'image_url' => $imageUrl,
                'niche' => $niche,
                'width_cm' => $widthCm,
                'height_cm' => $heightCm,
            ]);

        $response->throw();
        return $response->json();
    }

    public function generatePdf(string $imageUrl, string $niche, int $widthCm, int $heightCm): string
    {
        $response = Http::withToken($this->apiKey)
            ->post("{$this->baseUrl}/generate/pdf", [
                'image_url' => $imageUrl,
                'niche' => $niche,
                'width_cm' => $widthCm,
                'height_cm' => $heightCm,
            ]);

        $response->throw();
        return $response->body();
    }

    public function usage(): array
    {
        return Http::withToken($this->apiKey)
            ->get("{$this->baseUrl}/usage")
            ->json();
    }
}

Config (config/services.php)

'pixcraft' => [
    'key' => env('PIXCRAFT_API_KEY'),
],

Controller

<?php

namespace App\Http\Controllers;

use App\Services\PixCraftService;
use Illuminate\Http\Request;

class PatternController extends Controller
{
    public function generate(Request $request, PixCraftService $pixcraft)
    {
        $validated = $request->validate([
            'image_url' => 'required|url',
            'niche' => 'required|string',
            'width_cm' => 'required|integer|min:10|max:1000',
            'height_cm' => 'required|integer|min:10|max:1000',
        ]);

        $pattern = $pixcraft->generate(
            $validated['image_url'],
            $validated['niche'],
            $validated['width_cm'],
            $validated['height_cm']
        );

        return response()->json($pattern);
    }
}