Skip to content

Commit

Permalink
Rotation support
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitchenkoSergey committed Jan 17, 2018
1 parent f7a68bd commit c13bd5c
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 17 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

Fast and simple document image generator. This is a wrapper over Imagick with which you can create such images:
<p align="center">
<img src="http://nikitchenko.ru/idg/index.png" width="550" alt="Example" />
<img src="http://nikitchenko.ru/idg/index2.png" width="550" alt="Example" />
</p>
See: examples/index.php

### Features
* Any elements count and structure
* `padding`, `margin`, `border` and `background` support
* `padding`, `margin`, `border`, `background` and `rotation` support
* Possibility to create custom elements
* Simple markup (like html)
* Possibility to custom canvas by Imagick methods
Expand Down Expand Up @@ -152,6 +152,13 @@ The element is responsible for its display
| `$element->setBackgroundOpacity($value)` | `Element` | Setting background opacity |
| `$element->setBackground($color, $opacity)` | `Element` | Setting background |

#### Rotation
Rotation is inherit.
| Method | Return | Description |
| ---| --- | --- |
| `$element->setRotation($value)` | `Element` | Setting rotation |
| `$element->getRotation()` | `int` | Get rotation total rotation |


### Text (extends Element)
| Method | Return | Description |
Expand Down
18 changes: 10 additions & 8 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
->setFont($fontRobotoRegular);
$idg->endBlock();

$idg->beginBlock()->setPadding(4, 10, 10, 10)->setBorder(2, '#500')->setBorderOpacity(0.9)->setMarginTop(20);
$idg->beginBlock()->setPadding(4, 10, 10, 10)
->setBorder(2, '#500')->setBorderOpacity(0.9)->setMarginTop(20);
$idg->text('Text block with border. Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim venia.')->setAlign(Imagick::ALIGN_CENTER)
Expand Down Expand Up @@ -70,10 +71,9 @@
->setFont($fontRobotoRegular)->setFontSize(18)->setAlign(Imagick::ALIGN_CENTER);
$idg->endColumn();
$idg->beginColumn(300);
$idg->text('Text in column. Text in column. Text in column.
Text in column. Text in column. Text in column. Text in column.
Text in column. Text in column.')->setFont($fontRobotoRegular)->setFontSize(18);
$idg->image('test_image.jpg')->setMarginTop(10);
$idg->text('Column with rotated image. Column with rotated image.
Column with rotated image.')->setFont($fontRobotoRegular)->setFontSize(18);
$idg->image('test_image.jpg')->setMarginTop(20)->setRotation(90);
$idg->endColumn();
$idg->endRow();

Expand Down Expand Up @@ -103,8 +103,10 @@
$idg->endColumn();
$idg->endRow();

$idg->beginAbsoluteBlock(286, 73)->setWidth(270)->setBorder(2, 'white')->setPadding(0, 5, 15, 5)->setBackground('green', 0.5);
$idg->text('Absolute block on the bear')
$idg->beginAbsoluteBlock(250, 120)->setWidth(200)
->setBorder(2, 'white')->setPadding(0, 5, 15, 5)
->setBackground('green', 0.5)->setRotation(10);
$idg->text('Absolute block with rotation')
->setFont($fontRobotoBold)->setFontSize(28)->setTextColor('white')->setAlign(Imagick::ALIGN_CENTER);
$idg->endAbsoluteBlock();

Expand All @@ -113,4 +115,4 @@
$idg->compose();

header('Content-Type: image/' . $idg->getCanvas()->getImageFormat());
print $idg->getImageBlob();
print $idg->getImageBlob();
19 changes: 18 additions & 1 deletion src/Elements/AbsoluteBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,21 @@ public function getTopOffset()
{
return $this->top;
}
}

/**
* @inheritdoc
*/
public function getWidth()
{
$result = $this->width;
if (!$result && $this->parent) {
$result = $this->parent->getWidth();
$result -= $this->parent->paddingLeft;
$result -= $this->parent->paddingRight;
}

$result -= $this->marginLeft + $this->marginRight;

return $result;
}
}
4 changes: 3 additions & 1 deletion src/Elements/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Idg\Elements\Properties\Border;
use Idg\Elements\Properties\Margin;
use Idg\Elements\Properties\Padding;
use Idg\Elements\Properties\Rotation;
use Idg\Idg;

/**
Expand All @@ -21,6 +22,7 @@ class Element
use Margin;
use Border;
use Background;
use Rotation;

/**
* @var string
Expand Down Expand Up @@ -395,4 +397,4 @@ public function setAfterRender(\Closure $closure)
$this->afterRender = $closure;
return $this;
}
}
}
7 changes: 6 additions & 1 deletion src/Elements/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public function beforeRender()
$image->readImage($this->file);
}

$rotation = $this->getRotation();
if ($rotation) {
$image->rotateImage('transparent', $rotation);
}

$imageWidth = $image->getImageWidth();
$imageHeight = $image->getImageHeight();

Expand All @@ -59,4 +64,4 @@ public function render()
$this->getTopOffset() + $this->paddingTop
);
}
}
}
7 changes: 6 additions & 1 deletion src/Elements/Properties/Background.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ public function renderBackground()
$draw->setFillColor($fillColor);
$draw->setFillOpacity($this->backgroundOpacity);

$rotation = $this->getRotation();
if ($rotation) {
$draw->rotate($rotation);
}

$draw->rectangle($this->getLeftOffset(), $this->getTopOffset(), $this->getLeftOffset() + $this->getWidth(), $this->getTopOffset() + $this->getHeight());
$this->getIdg()->getCanvas()->drawImage($draw);
}
}
}
7 changes: 6 additions & 1 deletion src/Elements/Properties/Border.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ public function renderBorder()
$draw->setStrokeOpacity($this->borderOpacity);
$draw->setStrokeWidth($this->borderWidth);

$rotation = $this->getRotation();
if ($rotation) {
$draw->rotate($rotation);
}

$draw->rectangle($this->getLeftOffset(), $this->getTopOffset(), $this->getLeftOffset() + $this->getWidth(), $this->getTopOffset() + $this->getHeight());
$this->getIdg()->getCanvas()->drawImage($draw);
}
}
}
44 changes: 44 additions & 0 deletions src/Elements/Properties/Rotation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Rotation trait
*/

namespace Idg\Elements\Properties;

use Idg\Elements\Element;

trait Rotation {

/**
* @var integer
*/
public $rotation = 0;


/**
* Setting rotation
* @param int $value
* @return $this
*/
public function setRotation($value)
{
$this->rotation = $value;
return $this;
}

/**
* Get result rotation
* @return int
*/
public function getRotation()
{
/** @var $this Element */

$rotation = $this->rotation;
if ($this->parent instanceof Element) {
$rotation += $this->parent->getRotation();
}

return $rotation;
}
}
15 changes: 13 additions & 2 deletions src/Elements/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public function render()
$lines = $this->getTextRows();

$textHeight = 0;


foreach ($lines as $line) {
$draw = clone $fontStyle;
$metrics = $this->getIdg()->getCanvas()->queryFontMetrics($draw, $line, false);
Expand All @@ -94,9 +96,12 @@ public function render()
} elseif ($this->align == Imagick::ALIGN_CENTER) {
$leftOffset = $this->getLeftOffset() + ($this->getWidth() / 2);
}

$textHeight += $textLineHeight;
$draw->annotation($leftOffset, $this->getTopOffset() + $textHeight, $line);


$this->getIdg()->getCanvas()->annotateImage($draw, $leftOffset, $this->getTopOffset() + $textHeight, $this->angle, $line);
$this->getIdg()->getCanvas()->drawImage($draw);
}
}

Expand All @@ -110,6 +115,8 @@ protected function getDraw()
return $this->fontStyle;
}

$rotation = $this->getRotation() + $this->angle;

$textDraw = new ImagickDraw();
$textDraw->setFillColor(new \ImagickPixel($this->textColor));
$textDraw->setFontSize($this->fontSize);
Expand All @@ -121,6 +128,10 @@ protected function getDraw()
$textDraw->setFont($this->font);
}

if ($rotation) {
$textDraw->rotate($rotation);
}

return $textDraw;
}

Expand Down Expand Up @@ -229,4 +240,4 @@ public function setTextColor($value)
$this->textColor = $value;
return $this;
}
}
}

0 comments on commit c13bd5c

Please sign in to comment.