PHP code example of devedge / photo-desc

1. Go to this page and download the library: Download devedge/photo-desc 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/ */

    

devedge / photo-desc example snippets


use React\EventLoop\Loop;
use React\Http\Browser;
use PhotoDesc\Service\AsyncOpenRouterService;
use PhotoDesc\AsyncPhotoProcessor;
use Psr\Log\LoggerInterface;

// Create ReactPHP browser
$browser = new Browser(Loop::get());

// Create async services
$asyncService = new AsyncOpenRouterService(
    $browser,
    $logger,  // your PSR-3 logger
    $apiKey,  // your OpenRouter API key
    $model    // the AI model to use
);

$asyncProcessor = new AsyncPhotoProcessor(
    $asyncService,
    $logger
);

// Process an image asynchronously
$asyncProcessor->processSingleAsync('/path/to/image.jpg')
    ->then(
        function ($metadata) {
            // Handle successful result
            echo json_encode($metadata, JSON_PRETTY_PRINT);
        },
        function ($error) {
            // Handle error
            echo "Error: " . $error->getMessage();
        }
    );

// Run the event loop
Loop::get()->run();

use PhotoDesc\Service\OpenRouterService;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use Psr\Log\LoggerInterface;
use GuzzleHttp\Psr7\HttpFactory;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// Set up dependencies
$client = new Client();
$requestFactory = new HttpFactory();
$streamFactory = new HttpFactory();
$logger = new Logger('photo-description');
$logger->pushHandler(new StreamHandler('php://stdout', Logger::INFO));

// Your API key and model configuration
$apiKey = 'your-openrouter-api-key';
$model = 'anthropic/claude-3-opus:beta'; // Or any other supported model

// Create the service
$openRouterService = new OpenRouterService(
    $client,
    $requestFactory,
    $streamFactory,
    $logger,
    $apiKey,
    $model
);

// Read an image and convert to base64
$imagePath = 'path/to/your/image.jpg';
$imageData = file_get_contents($imagePath);
$base64Image = base64_encode($imageData);

// Classify the image
$result = $openRouterService->classifyImage($base64Image, basename($imagePath));

if ($result) {
    echo "Description: {$result['description']}\n";
    echo "Tags: " . implode(', ', $result['tags']) . "\n";
}

php process_photos.php

# Process a local file asynchronously
php examples/process_photos_async.php /path/to/your/image.jpg

# Process an image from URL asynchronously
php examples/process_photos_async.php https://example.com/image.jpg
json
{
  "description": "A detailed description of the image contents",
  "tags": ["tag1", "tag2", "tag3", "..."]
}