PHP Classes

File: docs/content/examples/composing-with-canvas.md

Recommend this page to a friend!
  Classes of Jose Luis Quintana   GImage   docs/content/examples/composing-with-canvas.md   Download  
File: docs/content/examples/composing-with-canvas.md
Role: Example script
Content type: text/markdown
Description: Example script
Class: GImage
Create graphic images with a fluent interface
Author: By
Last change:
Date: 1 year ago
Size: 1,081 bytes
 

Contents

Class file image Download

Composing with canvas

Canvas class is useful for composing several elements like images, text and figures.

!!! tip "Tip"

Canvas `append(arr)` function also supports an array of elements.

<?php

use GImage\Text;
use GImage\Image;
use GImage\Figure;
use GImage\Canvas;

$img1 = new Image();
$img1
    ->load('https://i.imgur.com/G5MR088.png')
    ->scale(0.5);

$figure1 = new Figure(300, 150);
$figure1
    ->setBackgroundColor(10, 20, 30)
    ->create();

$text1 = new Text('My awesome text!');
$text1
    ->setSize(18)
    ->setColor(255, 255, 255)
    ->setFontface('/my/path/fonts/times.ttf');

// used as canvas layout
$figure0 = new Figure(400, 250);
$figure0
    ->setBackgroundColor(50, 20, 30)
    ->create();

$canvas = new Canvas($figure0);
$canvas
    // append every element
    ->append($figure1)
    ->append($img1)
    ->append($text1)
    // as PNG format
    ->toPNG()
    // draw the canvas with all elements
    ->draw()
    ->save('text.png');

For more details check out the presentation card example.