PHP code example of core45 / laravel-kiriengine

1. Go to this page and download the library: Download core45/laravel-kiriengine 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/ */

    

core45 / laravel-kiriengine example snippets


use Core45\LaravelKiriengine\Facades\Kiriengine;

// Set API key and chain methods
$result = Kiriengine::setApiKey('your_api_key_here')
    ->uploadPhotoScan()
    ->imageUpload($images);

// Clear the API key when done
Kiriengine::clearApiKey();

use Core45\LaravelKiriengine\Facades\Kiriengine;

// Uses configured API key (from .env or resolver)
$result = Kiriengine::uploadPhotoScan()->imageUpload($images);

// In the migration file
Schema::table('users', function (Blueprint $table) {
    $table->string('kiri_api_key')->nullable()->after('password');
});

// In app/Models/User.php
protected $fillable = [
    'name',
    'email',
    'password',
    'kiri_api_key',
];

protected $hidden = [
    'password',
    'remember_token',
    'kiri_api_key', // Hide from serialization
];

// In config/laravel-kiriengine.php
'api_key_resolver' => function() {
    return auth()->user()->kiri_api_key ?? null;
},

// Example: Check multiple sources
'api_key_resolver' => function() {
    // First try user-specific key
    if (auth()->check() && auth()->user()->kiri_api_key) {
        return auth()->user()->kiri_api_key;
    }
    
    // Then try organization key
    if (auth()->check() && auth()->user()->organization) {
        return auth()->user()->organization->kiri_api_key;
    }
    
    // Finally fall back to global key
    return null;
},

// Example: Database-based resolver
'api_key_resolver' => function() {
    return \App\Models\SystemSetting::where('key', 'kiri_api_key')
        ->where('user_id', auth()->id())
        ->value('value');
},

// Example: Cache-based resolver
'api_key_resolver' => function() {
    return cache()->remember("user_{auth()->id()}_kiri_key", 3600, function() {
        return auth()->user()->kiri_api_key;
    });
},

use Core45\LaravelKiriengine\Facades\Kiriengine;

class ProcessKiriUploadJob implements ShouldQueue
{
    public function __construct(
        private string $apiKey,
        private array $images
    ) {}

    public function handle()
    {
        // Set API key and use KIRI Engine
        $result = Kiriengine::setApiKey($this->apiKey)
            ->uploadPhotoScan()
            ->imageUpload($this->images);

        // Clear the API key when done
        Kiriengine::clearApiKey();
    }
}

use Core45\LaravelKiriengine\Traits\WithKiriEngineApiKey;

class ProcessKiriUploadJob implements ShouldQueue
{
    use WithKiriEngineApiKey;

    public function __construct(
        private int $userId,
        private array $images
    ) {}

    public function handle()
    {
        // Set API key from user ID
        $this->withUserIdKiriEngineApiKey($this->userId);

        // Now use KIRI Engine - it will use the user's API key
        $uploader = new UploadPhotoScan();
        $result = $uploader->imageUpload($this->images);

        // Clear the API key when done
        $this->clearKiriEngineApiKey();
    }
}

// Set directly with API key string
$this->withKiriEngineApiKey('user_specific_api_key_here');

// Set from user model
$user = User::find($userId);
$this->withUserKiriEngineApiKey($user);

// Set from user ID (automatically fetches user)
$this->withUserIdKiriEngineApiKey($userId);

use Core45\LaravelKiriengine\Services\KiriEngineApiKeyResolver;

class ProcessKiriUploadJob implements ShouldQueue
{
    public function handle()
    {
        // Set the API key explicitly
        KiriEngineApiKeyResolver::setApiKey('user_specific_api_key_here');

        // Use KIRI Engine
        $uploader = new UploadPhotoScan();
        $result = $uploader->imageUpload($this->images);

        // Clear when done
        KiriEngineApiKeyResolver::clearApiKey();
    }
}

use Core45\LaravelKiriengine\Facades\Kiriengine;

class ProcessUserUploadsCommand extends Command
{
    protected $signature = 'kiri:process-uploads {userId}';

    public function handle()
    {
        $userId = $this->argument('userId');
        $user = User::findOrFail($userId);
        
        // Set API key and process uploads
        $result = Kiriengine::setApiKey($user->kiri_api_key)
            ->uploadPhotoScan()
            ->imageUpload($this->getUserImages($userId));

        $this->info('Upload completed successfully!');
        Kiriengine::clearApiKey();
    }

    private function getUserImages(int $userId): array
    {
        // Your logic to get user images
        return [];
    }
}

use Core45\LaravelKiriengine\Facades\Kiriengine;

// With specific API key
$result = Kiriengine::setApiKey('user_api_key')
    ->uploadPhotoScan()
    ->imageUpload($images);

// With configured API key
$result = Kiriengine::uploadPhotoScan()->imageUpload($images);

use Core45\LaravelKiriengine\Facades\Kiriengine;

$result = Kiriengine::setApiKey('user_api_key')
    ->uploadObjectScan()
    ->imageUpload($images);

use Core45\LaravelKiriengine\Facades\Kiriengine;

$result = Kiriengine::setApiKey('user_api_key')
    ->upload3DgsScan()
    ->imageUpload($images);

use Core45\LaravelKiriengine\Facades\Kiriengine;

// Get model status
$status = Kiriengine::setApiKey('user_api_key')
    ->model3d()
    ->getStatus('your_serial_number');

// Get download link for completed model
$downloadInfo = Kiriengine::setApiKey('user_api_key')
    ->model3d()
    ->getDownloadLink('your_serial_number');

use Core45\LaravelKiriengine\Facades\Kiriengine;

// Check your KIRI Engine balance
$balance = Kiriengine::setApiKey('user_api_key')
    ->balance()
    ->getBalance();

// Direct file paths - streams from disk without loading into memory
$images = [
    '/absolute/path/to/image1.jpg',
    '/absolute/path/to/image2.jpg',
    'relative/path/to/image3.jpg', // relative to public directory
];

// File paths in arrays - also memory efficient
$images = [
    ['path' => '/path/to/image1.jpg', 'name' => 'custom_name1.jpg'],
    ['path' => '/path/to/image2.jpg'], // name defaults to basename
];

// Content arrays - loads entire file into memory (avoid for large files)
$images = [
    ['name' => 'image1.jpg', 'contents' => file_get_contents('/path/to/image1.jpg')],
    ['name' => 'image2.jpg', 'contents' => file_get_contents('/path/to/image2.jpg')],
];

// Good: Process in batches
$allImages = [/* array of file paths */];
$batchSize = 50;

for ($i = 0; $i < count($allImages); $i += $batchSize) {
    $batch = array_slice($allImages, $i, $batchSize);
    $result = Kiriengine::setApiKey('user_api_key')
        ->uploadPhotoScan()
        ->imageUpload($batch);
    // Process result...
}

// Get file paths from media library
$product = Menuproduct::find(4);
$images = [];

foreach ($product->modelscans as $photo) {
    $images[] = $photo->getPath(); // Returns full file path
}

$result = Kiriengine::setApiKey('user_api_key')
    ->uploadPhotoScan()
    ->imageUpload($images);

// In your EventServiceProvider
protected $listen = [
    \Core45\LaravelKiriengine\Events\KiriWebhookReceived::class => [
        \Core45\LaravelKiriengine\Listeners\ProcessKiriWebhook::class,
    ],
];

class CustomWebhookListener
{
    public function handle(KiriWebhookReceived $event)
    {
        $payload = $event->payload;
        
        // Process the webhook data
        if (isset($payload['id'])) {
            $modelId = $payload['id'];
            // Your custom logic here
        }
    }
}

try {
    $result = $uploader->imageUpload($images);
} catch (KiriengineException $e) {
    // Handle API errors
    Log::error('KIRI Engine error: ' . $e->getMessage());
}
bash
php artisan vendor:publish --provider="Core45\LaravelKiriengine\KiriengineServiceProvider"