1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SixtyNine\Cloud\Tests\Builder; |
4
|
|
|
|
5
|
|
|
use Imagine\Gd\Imagine; |
6
|
|
|
use Imagine\Image\Box as ImagineBox; |
7
|
|
|
use Imagine\Image\Color; |
8
|
|
|
use Imagine\Image\ImageInterface; |
9
|
|
|
use Imagine\Image\Point; |
10
|
|
|
use Imagine\Image\PointInterface; |
11
|
|
|
use SixtyNine\Cloud\Factory\FontsFactory; |
12
|
|
|
use SixtyNine\Cloud\FontMetrics; |
13
|
|
|
|
14
|
|
|
class DrawingTest extends \PHPUnit_Framework_TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Illustrate the difficulty to properly draw a bounding box around rotated text with Imagine. |
18
|
|
|
*/ |
19
|
|
|
public function testDrawing() |
20
|
|
|
{ |
21
|
|
|
$imagine = new Imagine(); |
22
|
|
|
$image = $imagine->create(new ImagineBox(400, 400), new Color('#000000')); |
23
|
|
|
|
24
|
|
|
$text = 'Foobar'; |
25
|
|
|
$size = 80; |
26
|
|
|
|
27
|
|
|
$factory = FontsFactory::create(__DIR__ . '/fixtures/fonts'); |
28
|
|
|
$metrics = new FontMetrics($factory); |
29
|
|
|
$font = $factory->getImagineFont('Arial.ttf', $size, '#ffffff'); |
30
|
|
|
|
31
|
|
|
$fontSize = $metrics->calculateSize($text, 'Arial.ttf', $size, 0); |
32
|
|
|
$pos = array(200, 200); |
33
|
|
|
$box = array($fontSize->getWidth(), $fontSize->getHeight()); |
34
|
|
|
|
35
|
|
|
$image->draw()->text($text, $font, new Point($pos[0], $pos[1]), 0); |
36
|
|
|
$image->draw()->text($text, $font, new Point($pos[0], $pos[1]), 90); |
37
|
|
|
$image->draw()->text($text, $font, new Point($pos[0], $pos[1]), 180); |
38
|
|
|
$image->draw()->text($text, $font, new Point($pos[0], $pos[1]), 270); |
39
|
|
|
|
40
|
|
|
// Angle = 0 - so far so good... |
41
|
|
|
$this->drawBox($image, $this->createPoint($pos[0], $pos[1]), $box[0], $box[1], 0xFF0000); |
42
|
|
|
|
43
|
|
|
// Angle = 90 - Y needs to be adjusted, dimensions inverted |
44
|
|
|
$this->drawBox($image, $this->createPoint($pos[0], $pos[1] + $box[1]), $box[1], $box[0], 0xFF0000); |
45
|
|
|
|
46
|
|
|
// Angle = 180 - X and Y need adjustments, X might go out of the image on the left (which Imagine does not like) |
47
|
|
|
$x = $pos[0] - $box[0]; |
48
|
|
|
$width = $x >= 0 ? $box[0] : $box[0] + $x; |
49
|
|
|
$this->drawBox($image, $this->createPoint($x, $pos[1] + $box[1]), $width, $box[1], 0xFF0000); |
50
|
|
|
|
51
|
|
|
// Angle = 270 - X and Y need adjustments, it depends on the font size, Y might go out of the image on the top, dimensions inverted |
52
|
|
|
$y = $pos[1] - $box[0] + $size; |
53
|
|
|
$height = $y >= 0 ? $box[0] : $box[0] + $y; |
54
|
|
|
$this->drawBox($image, $this->createPoint($pos[0] - $box[1], $y), $box[1], $height, 0xFF0000); |
55
|
|
|
|
56
|
|
|
$image->save('/tmp/test.png'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function drawBox(ImageInterface $image, PointInterface $pos, $width, $height, $color = '#ffffff') |
60
|
|
|
{ |
61
|
|
|
$x = $pos->getX(); |
62
|
|
|
$y = $pos->getY(); |
63
|
|
|
|
64
|
|
|
$points = array( |
65
|
|
|
$this->createPoint($x, $y), |
66
|
|
|
$this->createPoint($x + $width, $y), |
67
|
|
|
$this->createPoint($x + $width, $y + $height), |
68
|
|
|
$this->createPoint($x, $y + $height), |
69
|
|
|
); |
70
|
|
|
$image->draw()->polygon($points, new Color($color)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function createPoint($x, $y) |
74
|
|
|
{ |
75
|
|
|
$x = $x >= 0 ? $x : 0; |
76
|
|
|
$y = $y >= 0 ? $y : 0; |
77
|
|
|
return new Point($x, $y); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|