gdaisy
A highly experimental image templating system based on PHP-GD to dynamically generate image banners and covers.
Installation
erikaheidi/gdaisy
in your project using Composer:
1. Require composer require erikaheidi/gdaisy
2. Once it's installed, you can run the default script to generate an example cover based on meta tags.
Gdaisy comes with an example script that generates a header image adequately sized for Twitter, based on a default template. The vendor/bin/gdaisy generate
script expects the URL to fetch as first parameter and the output path as second parameter, as follows:
./vendor/bin/gdaisy generate https://www.digitalocean.com/community/tutorials/how-to-set-up-visual-studio-code-for-php-projects output.png
This will generate the following image:
The example generation script is defined in vendor/erikaheidi/gdaisy/bin/gdaisy
.
Creating Templates
Consider the following basic.json
template example:
{
"width": 600,
"height": 600,
"background": "FFFFFF",
"elements": {
"title": {
"type": "text",
"properties": {
"pos_x": 50,
"pos_y": 20,
"size": 30,
"color": "666666",
"max_width": 500,
"align": "center"
}
},
"thumbnail": {
"type": "image",
"properties": {
"pos_x": 50,
"pos_y": 50,
"width": 500,
"height": 500
}
}
}
}
This template has two elements: title
(type text
) and thumbnail
(type image
).
Template Properties:
width
: Resulting image widthheight
: Resulting image heightbackground
: Resulting image background
Text Properties:
pos_x
: X coordinate (bottom left X coordinate for the base of text)pos_y
: Y coordinate (botom left Y coordinate for the base of text)size
: Text sizecolor
: Text Color (hex)max_width
(optional): Maximum text width - text will be broken down into multiple lines when setalign
(optional): Text align, possible values areleft
(default),center
, orright
.font
: path to font file (ttf)
Image Properties:
pos_x
: X coordinate (top left corner) where the image will be appliedpos_y
: Y coordinate (top left corner) where the image will be applied,width
: width (will proportially resize to fit)height
: height (will proportially resize to fit)image_file
(optional): when set, will use this image, otherwise you'll have to provide this as parameter when applying the templatecrop
(optional): when set tocenter
, will resize-crop while centering the image. Default isleft
, can also be set toright
.
Following, a PHP script to generate a new image based on the example template:
<?php
use GDaisy\Template;
require __DIR__. '/vendor/autoload.php';
$template = Template::create(__DIR__ . '/resources/templates/basic.json');
$template->apply("thumbnail", [
"image_file" => __DIR__ . '/resources/images/gdaisy.png'
])->apply("title", [
"text" => "generated with gdaisy"
]);
$template->write('output.png');
echo "Finished.\n";