PHP code example of 53ny4 / og-image

1. Go to this page and download the library: Download 53ny4/og-image 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/ */

    

53ny4 / og-image example snippets


$watermark = $imagine->open('/my/watermark.png');
$image     = $imagine->open('/path/to/image.jpg');
$size      = $image->getSize();
$wSize     = $watermark->getSize();

$bottomRight = new Imagine\Image\Point($size->getWidth() - $wSize->getWidth(), $size->getHeight() - $wSize->getHeight());

$image->paste($watermark, $bottomRight);



3ny4\OgImage\OgImage;
use s3ny4\OgImage\OgBackground;
use s3ny4\OgImage\OgText;
use s3ny4\OgImage\OgWatermark;

// Create a new OgImage instance
$ogImage = new OgImage();

// Set background (solid color)
$ogBackground = new OgBackground('#ffffff'); // White background
$ogBackground->addBorder('bottom', 10, '#ff0000');  // 10px red border at the bottom
$ogBackground->addBorder('top', 10, '#00ff00');  // 10px green border at the top
$ogBackground->addBorder('left', 10, '#0000ff');  // 10px blue border at the left
$ogBackground->addBorder('right', 10, '#ffff00');  // 10px yellow border at the right

$ogImage->setBackground($ogBackground); // Set the background



// Add text
$ogText = new OgText();
$ogText->setText('Hello World!');
$ogText->setPosition('center', 'center');
$ogText->setColor('000000'); // Black text
$ogText->setSize(60);
$ogImage->addText($ogText); // Add the text to the image

$ogWatermark = new OgWatermark();
$ogWatermark->image('/path/to/watermark.png');
$ogWatermark->setPosition('right', 'bottom'); // Bottom right corner; x - left, right, center; y - top, bottom, center
$ogWatermark->setSize(100); // Width of 100 pixels
$ogWatermark->setOpacity(50); // 50% opacity
$ogImage->addWatermark($ogWatermark); // Add the watermark to the image

// Render and output the image
$ogImage->render(); // Output to browser

$ogBackground = new OgBackground('#ff0000'); // Red background
$ogImage->setBackground($ogBackground);

$ogBackground = new OgBackground('/path/to/background.jpg');
$ogImage->setBackground($ogBackground);

$ogBackground->addBorder('bottom', 10, '#ff0000'); // 10px red border at the bottom
$ogBackground->addBorder('top', 10, '#00ff00'); // 10px green border at the top
$ogBackground->addBorder('left', 10, '#0000ff'); // 10px blue border at the left
$ogBackground->addBorder('right', 10, '#ffff00'); // 10px yellow border at the right

$ogText->setPosition('center', 'bottom');

$ogText->setColor('#ffffff'); // White text (# hashtag is optional)
$ogText->setSize(50); // Font size 50
$ogText->setFontFile('/path/to/font.ttf'); // Custom font

$ogText->setBackground('000000', 80); // Black background with 80% opacity

use s3ny4\OgImage\OgWatermark;

// Create a new OgWatermark instance
$ogWatermark = new OgWatermark();
$ogWatermark->image('/path/to/watermark.png');
$ogWatermark->setPosition('right', 'bottom'); // Bottom right corner; x - left, right, center; y - top, bottom, center
$ogWatermark->setSize(100); // Width of 100 pixels
$ogWatermark->setOpacity(50); // 50% opacity

$ogImage->addWatermark($ogWatermark);

class CustomTemplate extends OgImageTemplateBase


public function background($color): EventTemplate
    {
        $background = new OgBackground($color);
        $background->addBorder('top', 10, 'fff000');
        $this->ogImage->setBackground($background);
        return $this;
    }

    public function title($text): EventTemplate
    {
        $titleText = new OgText();
        $titleText->setPosition('center', 'top');
        $titleText->setColor('000000');
        $titleText->setSize(50);
        $titleText->setPadding(20);
        $titleText->setText($text);
        $this->ogImage->addText($titleText);

        return $this;
    }

    public function date($date): EventTemplate
    {
        $dateText = new OgText();
        $dateText->setPosition('center', 'bottom');
        $dateText->setColor('000000');
        $dateText->setSize(30);
        $dateText->setPadding(20);
        $dateText->setText($date);
        $this->ogImage->addText($dateText);

        return $this;
    }


    public function logo($image,$size): EventTemplate
    {
        // watermark
        $watermark = new OgWatermark();
        $watermark->image($image);
        $watermark->setPosition('center', 'center');
        $watermark->setSize($size);
        $this->ogImage->addWatermark($watermark);

        return $this;
    }





$ogImage = TemplateManager::registerTemplate('event', EventTemplate::class);

$ogImage
    ->background('#ffffff')
    ->title('Annual Conference 2024')
    ->date('November 1-3, 2024')
    ->logo(__DIR__ . '/assets/images/conf_logo.png', 400)
    ->render();



3ny4\OgImage\OgImage;
use s3ny4\OgImage\OgBackground;
use s3ny4\OgImage\OgText;
use s3ny4\OgImage\OgWatermark;

// Create a new OgImage instance
$ogImage = new OgImage(1200, 630); // Custom dimensions

// Set background (image)
$ogBackground = new OgBackground('/path/to/background.jpg'); # Image background

// Set background (solid color)
// $ogBackground = new OgBackground('#ff0000'); # Red background

$ogImage->setBackground($ogBackground); # Set the background

// Add text
$ogText = new OgText();
$ogText->setText('Your Custom OG Image'); # Text
$ogText->setPosition('center', 'center'); # x - left, right, center; y - top, bottom, center
$ogText->setColor('#ffffff'); # Text color
$ogText->setSize(60); # Font size
$ogText->setBackground('000000', 50); // Semi-transparent black background
$ogText->setPadding(20); # Padding around the text
$ogImage->addText($ogText); # Add the text to the image

// Add watermark
$ogWatermark = new OgWatermark();
$ogWatermark->image('/path/to/watermark.png'); # Watermark image
$ogWatermark->setPosition('right', 'bottom'); # x - left, right, center; y - top, bottom, center
$ogWatermark->setSize(100); # Width of 100 pixels
$ogWatermark->setOpacity(80); # 80% opacity
$ogImage->addWatermark($ogWatermark); # Add the watermark to the image

// output to browser
$ogImage->render();

//or
    
// Render and output the image
// $ogImage->render('output.png');

  $ogText->setFontFile('/path/to/custom-font.ttf');
  

  $ogText->setPadding(30); // 30 pixels of vertical padding
  

  $ogText->setPosition('left', 'top');
  

  $ogText->setMaxWidth(800); // Wrap text at 800 pixels