2017 © Pedro Peláez
 

library idg

Document image generator

image

idg/idg

Document image generator

  • Monday, March 19, 2018
  • by NikitchenkoSergey
  • Repository
  • 6 Watchers
  • 30 Stars
  • 12 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Document image generator

Minimum PHP Version Build Status, (*1)

Fast and simple document image generator. This is a wrapper over Imagick with which you can create such images:, (*2)

Example , (*3)

See: examples/index.php, (*4)

Features

  • Any elements count and structure
  • padding, margin, border, background and rotation support
  • Possibility to create custom elements
  • Computed properties
  • Simple markup (like html)
  • Possibility to custom canvas by Imagick methods
  • Faster and more optimal that html->pdf->image

Requirements

PHP 5.6+, Imagick extension., (*5)

Installation

composer require idg/idg, (*6)

Usage

<?php

require_once __DIR__ . '/vendor/autoload.php';
// font for example
$fontRegular = 'RobotoCondensed-Regular.ttf';

$idg = new \Idg\Idg(1000, 3000, null, new ImagickPixel('#fff'));
$idg->beginDocument(40, 30, 40, 30);
 $idg->beginRow();
        $idg->beginColumn(300);
            $idg->image('test_image.jpg');
            $idg->beginBlock()->setLeft(20);
                $idg->text('Figure 1. Dolore eu fugiat nulla pariatur.')
                ->setFont($fontRegular)->setFontSize(14)->setTextColor('#555');
            $idg->endBlock();
        $idg->endColumn();
        $idg->beginColumn(600);
                $idg->text('Lorem ipsum dolor sit amet, 
                consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate 
                velit esse cillum dolore eu fugiat nulla pariatur. 
                Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.')
                    ->setFont($fontRegular);
        $idg->endColumn();
 $idg->endRow();
$idg->endDocument();
$idg->compose();

header('Content-Type: image/' . $idg->getCanvas()->getImageFormat());
print $idg->getImageBlob();

Result

Example , (*7)

see: examples/columns.php, (*8)

Methods

Idg

Method Return Description
Idg($width, $maxHeight, $minHeight = null, $background = null, $type = 'png') Idg Creating new generator
$idg->beginDocument($marginTop = 0, $marginLeft = 0, $marginBottom = 0, $marginRight = 0) Document Begin Document. It required.
$idg->endDocument() End document.
$idg->beginBlock() Block Begin relative block.
$idg->endBlock() End block.
$idg->beginAbsoluteBlock($top, $left) AbsoluteBlock Begin absolute block.
$idg->endAbsoluteBlock() End absolute block.
$idg->beginRow() Row Begin row.
$idg->endRow() End row.
$idg->beginColumn($width) Column Begin column.
$idg->endColumn() End column.
$idg->image($file, $fromBlob = false) Image Adding image.
$idg->text($content) Text Adding text.
$idg->addElement(Element $element) Element Add custom element.
$idg->getCanvas() Imagick Return Imagick object.
$idg->compose() Composing blocks.
$idg->getImageBlob() string Returning image result blob.
$idg->beginElement(Element $element) Element Begin custom element.
$idg->endElement() End custom element.

Element

The element is responsible for its display, (*9)

Main methods

Method Return Description
Element() Element Creating new element
$element->setTop($value) Element Setting top
$element->setLeft($value) Element Setting left
$element->setWidth($value) Element Setting width
$element->setStaticHeight($value) Element Setting static height
$element->setAfterRender($closure) Element Setting after render function, see: examples/custom_element.php
$element->getParent() Element or null Get parent element
$element->getIdg() Igd Get Igd object
$element->getWidth() int Get width or parents width
$element->getLeft() int Get left
$element->getTop() int Get top
$element->getTopOffset() int Get global top offset
$element->getHeight() int Get height with children
$element->getOuterHeight() int Get height with top
$element->increaseHeight($value) Increase self height
$element->getChildren() Element[] List of children
$element->getSiblings() Element[] List of siblings with current element
$element->getPrevSibling() Element Prev sibling
$element->getPrevSiblings() Element[] List of prev siblings
$element->beforeRender() Method will call before render document
$element->render() Method will call on render document
$element->afterRender() Method will call after render document

Element properties

Padding

Method Return Description
$element->setPaddingTop($value) Element Setting padding top
$element->setPaddingLeft($value) Element Setting padding left
$element->setPaddingRight($value) Element Setting padding right
$element->setPaddingBottom($value) Element Setting padding bottom
$element->setPadding($top, $right, $bottom, $left) Element Setting padding

Margin

Method Return Description
$element->setMarginTop($value) Element Setting margin top
$element->setMarginLeft($value) Element Setting margin left
$element->setMarginRight($value) Element Setting margin right
$element->setMarginBottom($value) Element Setting margin bottom
$element->setMargin($top, $right, $bottom, $left) Element Setting margin

Border

Method Return Description
$element->setBorderColor($value) Element Setting border color
$element->setBorderWidth($value) Element Setting border width
$element->setBorder($width, $color) Element Setting border
$element->setBorderOpacity($value) Element Setting border opacity

Background

Method Return Description
$element->setBackgroundColor($value) Element Setting background color. $value can be Gradient object
$element->setBackgroundOpacity($value) Element Setting background opacity
$element->setBackground($color, $opacity) Element Setting background

Rotation

Rotation is inherit., (*10)

Method Return Description
$element->setRotation($value) Element Setting rotation
$element->getRotation() int Get rotation total rotation

Text (extends Element)

Method Return Description
$element->setAlign($value) Element Setting align
$element->setAngle($value) Element Setting angle of line
$element->setFontSize($value) Element Setting font size, 16 default
$element->setFont($value) Element Setting font family
$element->setTextColor($value) Element Setting text color, black default
$element->setDecoration($value) Element Setting text decoration
$element->setFontStyle(\ImagickDraw $draw) Element Setting style, it override all attributes

Padding, margin, border and gradient on background

Example , (*11)

See: examples/properties.php
, (*12)

Computed properties

// .....
$idg->beginBlock();
    $idg->text('Text in block.')
            ->setMargin(10, 20, 20, 20);
$idg->endBlock();
$idg->beginBlock()->setStaticHeight(function(\Idg\Elements\Element $element) {
            return $element->getPrevSibling()->getHeight();
        });
        $idg->text('Block with height like prev.')
            ->setMargin(10, 20, 20, 20);
$idg->endBlock();
// .....

Example , (*13)

See: examples/computed_properties.php
, (*14)

Custom elements

Example , (*15)

See: examples/custom_element.php
Custom element must be instance from Element (or children), (*16)

/**
 * Custom element with green background
 * Class GreenBlock
 */
class GreenBlock extends \Idg\Elements\Element
{
    /**
     * @inheritdoc
     */
    function afterRender()
    {
        $draw = new \ImagickDraw();
        $strokeColor = new \ImagickPixel('green');
        $fillColor = new \ImagickPixel('green');

        $draw->setStrokeColor($strokeColor);
        $draw->setFillColor($fillColor);
        $draw->setFillOpacity(0.1);
        $draw->setStrokeOpacity(1);
        $draw->setStrokeWidth(2);

        $draw->rectangle($this->getLeftOffset(), $this->getTopOffset(), $this->getLeftOffset() + $this->getWidth(), $this->getTopOffset() + $this->getHeight());
        $this->getIdg()->getCanvas()->drawImage($draw);
    }
}
$customBlock = new GreenBlock();
// .....
$idg->beginElement($customBlock)->setTop(20)->setPaddingBottom(25);
// .....
$idg->endElement();
// .....

For more customization you can use standart Imagick methods:, (*17)

$idg->compose();
$idg->getCanvas()->gaussianBlurImage(10, 20);

Donation

PayPal: nikitchenko.sergey@yandex.ru, (*18)

Yandex.Money: 410011704835851, (*19)

The Versions

19/03 2018

dev-master

9999999-dev https://github.com/NikitchenkoSergey/idg

Document image generator

  Sources   Download

MIT

The Requires

  • php >=5.6.0
  • ext-imagick *

 

The Development Requires

by Nikitchenko Sergey

php generator image imagick document text-to-image

26/01 2018

v1.2

1.2.0.0 https://github.com/NikitchenkoSergey/idg

Document image generator

  Sources   Download

MIT

The Requires

  • php >=5.6.0
  • ext-imagick *

 

The Development Requires

by Nikitchenko Sergey

php generator image imagick document text-to-image

17/01 2018

v1.1

1.1.0.0 https://github.com/NikitchenkoSergey/idg

Image document generator

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Nikitchenko Sergey

php generator image document

08/01 2018

v1.0

1.0.0.0 https://github.com/NikitchenkoSergey/idg

Image document generator

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Nikitchenko Sergey

php generator image document