PHP code example of hankchen / php-canvas-next

1. Go to this page and download the library: Download hankchen/php-canvas-next 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/ */

    

hankchen / php-canvas-next example snippets


use HankChen\CanvasNext\Canvas;
use HankChen\CanvasNext\Layer\ImageLayer;
use HankChen\CanvasNext\Layer\TextLayer;
use HankChen\CanvasNext\Renderer\Image\ImageRenderer; // 来自 php-canvas-image-renderer 包
use HankChen\CanvasNext\Runtime\NullCancellation;

$canvas = Canvas::make(400, 300,
    // priority 越大越先渲染(越垫底),与 php-canvas 一致
    ImageLayer::make(400, 300, '#ffffff')->setPriority(10),
    (new TextLayer())
        ->setText('Hello, php-canvas-next')
        ->setFont('/path/to/font.ttf', 24, '#333333')
        ->setPosition(20, 40)
        ->setPriority(1)
);

// 第一个参数是渲染取消上下文;无取消需求传 NullCancellation
$image = (new ImageRenderer())->render(new NullCancellation(), $canvas); // intervention/image v4 ImageInterface
$image->save('/tmp/out.png');

$layer->setLineBreaker($myBreaker);            // HankChen\CanvasNext\Contracts\LineBreakerInterface
$layer->setMeasurerFactory($myFactory);        // HankChen\CanvasNext\Contracts\TextMeasurerFactoryInterface

$renderer = new ImageRenderer(new \HankChen\CanvasNext\ResourceManagers\ResourceResolver($downloader));

$spec = $canvas->graph();                       // 无损结构树,可 JSON 持久化
$canvas = Canvas::fromGraph($spec);             // 完整重建(含表格尺寸同步)