1: <?php
2: /**
3: ##DOC-SIGNATURE##
4:
5: This file is part of WideImage.
6:
7: WideImage is free software; you can redistribute it and/or modify
8: it under the terms of the GNU Lesser General Public License as published by
9: the Free Software Foundation; either version 2.1 of the License, or
10: (at your option) any later version.
11:
12: WideImage is distributed in the hope that it will be useful,
13: but WITHOUT ANY WARRANTY; without even the implied warranty of
14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: GNU Lesser General Public License for more details.
16:
17: You should have received a copy of the GNU Lesser General Public License
18: along with WideImage; if not, write to the Free Software
19: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20:
21: * @package WideImage
22: **/
23:
24: namespace WideImage\Font;
25:
26: use WideImage\Coordinate;
27:
28: /**
29: * TTF font support class
30: *
31: * @package WideImage
32: */
33: class TTF
34: {
35: public $face;
36: public $size;
37: public $color;
38:
39: public function __construct($face, $size, $color)
40: {
41: $this->face = $face;
42: $this->size = $size;
43: $this->color = $color;
44: }
45:
46: /**
47: * Writes text onto an image
48: *
49: * @param \WideImage\Image $image
50: * @param mixed $x smart coordinate
51: * @param mixed $y smart coordinate
52: * @param string $text
53: * @param int $angle Angle in degrees clockwise
54: */
55: public function writeText($image, $x, $y, $text, $angle = 0)
56: {
57: if ($image->isTrueColor()) {
58: $image->alphaBlending(true);
59: }
60:
61: $box = imageftbbox($this->size, $angle, $this->face, $text);
62: $obox = array(
63: 'left' => min($box[0], $box[2], $box[4], $box[6]),
64: 'top' => min($box[1], $box[3], $box[5], $box[7]),
65: 'right' => max($box[0], $box[2], $box[4], $box[6]) - 1,
66: 'bottom' => max($box[1], $box[3], $box[5], $box[7]) - 1
67: );
68: $obox['width'] = abs($obox['left']) + abs($obox['right']);
69: $obox['height'] = abs($obox['top']) + abs($obox['bottom']);
70:
71: $x = Coordinate::fix($x, $image->getWidth(), $obox['width']);
72: $y = Coordinate::fix($y, $image->getHeight(), $obox['height']);
73:
74: $fixed_x = $x - $obox['left'];
75: $fixed_y = $y - $obox['top'];
76:
77: imagettftext($image->getHandle(), $this->size, $angle, $fixed_x, $fixed_y, $this->color, $this->face, $text);
78: }
79: }
80: