PHP code example of kehet / imagick-layout-engine

1. Go to this page and download the library: Download kehet/imagick-layout-engine 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/ */

    

kehet / imagick-layout-engine example snippets


// Helper function to create ImagickDraw objects with fill color
function fill(string $fill): ImagickDraw
{
    $return = new \ImagickDraw();
    $return->setFillColor(new \ImagickPixel($fill));
    return $return;
}

// Create a new image
$width = 1500;
$height = 1000;
$imagick = new Imagick();
$imagick->newImage($width, $height, new ImagickPixel('white'));

// Create a row container with rectangles
$frame = new RowContainer();
$frame->addItem(new Rectangle(fill('#fee2e2')));
$frame->addItem(new Rectangle(fill('#fca5a5')));
$frame->addItem(new Rectangle(fill('#dc2626')));
$frame->addItem(new Rectangle(fill('#450a0a')));

// Draw container onto image
$frame->draw($imagick, 0, 0, $width, $height);

// Output image as PNG
$imagick->setImageFormat('png');
$imagick->writeImage('output.png');

$row = new RowContainer();
$row->addItem(new Rectangle(fill('#fee2e2')), 50);  // 50 px wide
$row->addItem(new Rectangle(fill('#fca5a5')), 100); // 100 px wide
$row->addItem(new Rectangle(fill('#dc2626')));      // Takes remaining space

// Single value padding (10px on all sides)
$row = new RowContainer();
$row->addItem(new Rectangle(fill('#fee2e2')), padding: 10);
$row->addItem(new Rectangle(fill('#fca5a5')), padding: 10);

// Two value padding (10px top/bottom, 20px left/right)
$column = new ColumnContainer();
$column->addItem(new Rectangle(fill('#fee2e2')), padding: [10, 20]);
$column->addItem(new Rectangle(fill('#fca5a5')), padding: [10, 20]);

// Three value padding (10px top, 20px left/right, 30px bottom)
$row = new RowContainer();
$row->addItem(new Rectangle(fill('#fee2e2')), padding: [10, 20, 30]);
$row->addItem(new Rectangle(fill('#fca5a5')), padding: [10, 20, 30]);

// Four value padding (10px top, 20px right, 30px bottom, 40px left)
$column = new ColumnContainer();
$column->addItem(new Rectangle(fill('#fee2e2')), padding: [10, 20, 30, 40]);
$column->addItem(new Rectangle(fill('#fca5a5')), padding: [10, 20, 30, 40]);

// Text with auto-sizing
$container->addItem(
    new Text(
        fill('#000'),
        'Lorem Ipsum Dolor',
        initialFontSize: 120,
        minFontSize: 50,
        gravity: Gravity::CENTER
    )
);

// Text with auto-wrapping and sizing
$container->addItem(
    new TextWrap(
        fill('#000'),
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        initialFontSize: 60,
        minFontSize: 10
    )
);

// Default behavior (fit) - maintains aspect ratio
$container->addItem(new Image('path/to/image.jpg'));

// Fill mode - image will be cropped to fill the container
$container->addItem(new Image('path/to/image.jpg', fill: ImageMode::FILL));

// With gravity option for positioning
$container->addItem(new Image('path/to/image.jpg', fill: ImageMode::FIT, gravity: Gravity::TOP));

// Create a column container with multiple row containers
$frame = new ColumnContainer();

// First row
$row = new RowContainer();
$row->addItem(new Rectangle(fill('#fee2e2')), 50);
$row->addItem(new Rectangle(fill('#fca5a5')), 100);
$row->addItem(new Rectangle(fill('#dc2626')), 150);
$row->addItem(new Rectangle(fill('#450a0a')));
$frame->addItem($row);

// Second row
$row = new RowContainer();
$row->addItem(new Rectangle(fill('#ecfccb')));
$row->addItem(new Rectangle(fill('#bef264')), 50);
$row->addItem(new Rectangle(fill('#65a30d')), 100);
$row->addItem(new Rectangle(fill('#1a2e05')), 150);
$frame->addItem($row);

// Create a column container with an image and text
$container = new ColumnContainer();
$container->addItem(new Image('path/to/image.jpg', fill: ImageMode::FIT, gravity: Gravity::CENTER));
$container->addItem(new TextWrap(fill('black'), 'Image Caption'));