PHP code example of sunergos / og-pilot-php

1. Go to this page and download the library: Download sunergos/og-pilot-php 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/ */

    

sunergos / og-pilot-php example snippets


use Sunergos\OgPilot\OgPilot;

OgPilot::setConfig([
    'api_key' => 'your-api-key',
    'domain' => 'your-domain.com',
    'image_type' => 'webp',
    'quality' => 82,
    'max_bytes' => 220000,
    // 'strip_extensions' => true,
    // 'strip_query_parameters' => true,
]);

use Sunergos\OgPilot\Facades\OgPilot;

// Generate an image URL
$imageUrl = OgPilot::createImage([
    'template' => 'blog_post',
    'title' => 'How to Build Amazing OG Images',
    'description' => 'A complete guide to social media previews',
    'bg_color' => '#1a1a1a',
    'text_color' => '#ffffff',
    'author_name' => 'Jane Smith',
    'publish_date' => '2024-01-15',
]);

// With cache refresh (using iat)
$imageUrl = OgPilot::createImage([
    'template' => 'blog_post',
    'title' => 'My Blog Post',
], [
    'iat' => time(), // Refresh cache daily
]);

// Get JSON metadata instead
$data = OgPilot::createImage([
    'template' => 'page',
    'title' => 'Hello OG Pilot',
], [
    'json' => true,
]);

use Sunergos\OgPilot\OgPilot;

// Configure once at application bootstrap
OgPilot::setConfig([
    'api_key' => 'your-api-key',
    'domain' => 'your-domain.com',
    'image_type' => 'webp',
    'quality' => 82,
    'max_bytes' => 220000,
]);

// Generate an image URL
$imageUrl = OgPilot::createImage([
    'template' => 'blog_post',
    'title' => 'How to Build Amazing OG Images',
]);

// Or use the callback-style configuration
OgPilot::configure(function ($config) {
    $config->apiKey = 'your-api-key';
    $config->domain = 'your-domain.com';
});

use Sunergos\OgPilot\OgPilot;
use Sunergos\OgPilot\Client;

// Using the factory method
$client = OgPilot::createClient([
    'api_key' => 'your-api-key',
    'domain' => 'your-domain.com',
    'connect_timeout' => 3.0,
    'timeout' => 8.0,
    'image_type' => 'webp',
    'quality' => 82,
    'max_bytes' => 220000,
]);

$url = $client->createImage(['title' => 'Hello']);

// Or instantiate directly
$client = new Client([
    'api_key' => 'your-api-key',
    'domain' => 'your-domain.com',
    'image_type' => 'webp',
]);

$imageUrl = OgPilot::createBlogPostImage([
    'title' => 'How to Build Amazing OG Images',
    'author_name' => 'Jane Smith',
    'publish_date' => '2024-01-15',
]);

use Sunergos\OgPilot\Facades\OgPilot;

// In a controller, view, or anywhere in your Laravel app
$imageUrl = OgPilot::createImage([
    'template' => 'blog_post',
    'title' => 'My Blog Post',
]);
// path is automatically captured from the current request

use Sunergos\OgPilot\OgPilot;

// At the start of your request handling
OgPilot::setCurrentRequest([
    'url' => $_SERVER['REQUEST_URI'] ?? '/'
]);

// Your application code...
$imageUrl = OgPilot::createImage(['title' => 'My Page']);
// path is automatically set from REQUEST_URI

// At the end of your request
OgPilot::clearCurrentRequest();

use Sunergos\OgPilot\OgPilot;

$imageUrl = OgPilot::withRequestContext(
    ['url' => $_SERVER['REQUEST_URI']],
    fn() => OgPilot::createImage(['title' => 'My Page'])
);

use Sunergos\OgPilot\OgPilot;
use Symfony\Component\HttpKernel\Event\RequestEvent;

class OgPilotListener
{
    public function onKernelRequest(RequestEvent $event): void
    {
        $request = $event->getRequest();
        OgPilot::setCurrentRequest([
            'path' => $request->getRequestUri()
        ]);
    }
}

$imageUrl = OgPilot::createImage([
    'template' => 'page',
    'title' => 'Hello OG Pilot',
    'path' => '/pricing?plan=pro'
]);

$imageUrl = OgPilot::createImage([
    'template' => 'blog_post',
    'title' => 'Default OG Image',
], [
    'default' => true
]);
// path is set to "/"

// Laravel (.env)
OG_PILOT_STRIP_EXTENSIONS=true

// Standalone PHP
OgPilot::setConfig([
    'api_key' => 'your-api-key',
    'domain' => 'your-domain.com',
    'strip_extensions' => true,
]);

// All of these resolve to path "/docs":
OgPilot::createImage(['title' => 'Docs', 'path' => '/docs']);
OgPilot::createImage(['title' => 'Docs', 'path' => '/docs.md']);
OgPilot::createImage(['title' => 'Docs', 'path' => '/docs.php']);

// Nested paths work too: /blog/my-post.html → /blog/my-post
// Query strings are preserved: /docs.md?ref=main → /docs?ref=main
// Dotfiles are unchanged: /.hidden stays /.hidden

// Laravel (.env)
OG_PILOT_STRIP_QUERY_PARAMETERS=true

// Standalone PHP
OgPilot::setConfig([
    'api_key' => 'your-api-key',
    'domain' => 'your-domain.com',
    'strip_query_parameters' => true,
]);

// When enabled, /docs.md?ref=main resolves to /docs before signing.

$imageUrl = OgPilot::createImage(['title' => 'My Image']);
if ($imageUrl === null) {
    // Failed to generate image in URL mode
}

$json = OgPilot::createImage(['title' => 'My Image'], ['json' => true]);
if (($json['image_url'] ?? null) === null) {
    // Failed to generate image in JSON mode
}
bash
composer 
bash
php artisan vendor:publish --tag=og-pilot-config