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;
25:
26: use WideImage\Font;
27: use WideImage\Exception\InvalidFontFileException;
28: use WideImage\Exception\NoFontException;
29: use WideImage\Exception\InvalidCanvasMethodException;
30:
31: /**
32: * @package WideImage
33: */
34: class Canvas
35: {
36: protected $handle = 0;
37: protected $image = null;
38: protected $font = null;
39:
40: /**
41: * Creates a canvas object that writes to the image passed as a parameter
42: *
43: * Shouldn't be used directly, use \WideImage\Image::getCanvas() instead.
44: *
45: * @param \WideImage\Image $img Image object
46: */
47: public function __construct($img)
48: {
49: $this->handle = $img->getHandle();
50: $this->image = $img;
51: }
52:
53: /**
54: * Sets the active font. Can be an instance of
55: * \WideImage\Font\TTF, \WideImage\Font\PS, or \WideImage\Font\GDF.
56: *
57: * @param object $font Font object to set for writeText()
58: */
59: public function setFont($font)
60: {
61: $this->font = $font;
62: }
63:
64: /**
65: * Creates and sets the current font
66: *
67: * The supported font types are: TTF/OTF, PS, and GDF.
68: * Font type is detected from the extension. If the $file parameter doesn't have an extension, TTF font is presumed.
69: *
70: * Note: not all parameters are supported by all fonts.
71: *
72: * @param string $file Font file name (string)
73: * @param int $size Font size (supported for TTF/OTF and PS fonts, ignored for GDF)
74: * @param int $color Text color
75: * @param int $bgcolor Background color (supported only for PS font, ignored for TTF and PS)
76: * @return mixed One of the \WideImage\Font\* objects
77: */
78: public function useFont($file, $size = 12, $color = 0, $bgcolor = null)
79: {
80: $p = strrpos($file, '.');
81:
82: if ($p === false || $p < strlen($file) - 4) {
83: $ext = 'ttf';
84: } else {
85: $ext = strtolower(substr($file, $p + 1));
86: }
87:
88: if ($ext == 'ttf' || $ext == 'otf') {
89: $font = new Font\TTF($file, $size, $color);
90: } elseif ($ext == 'ps') {
91: $font = new Font\PS($file, $size, $color, $bgcolor);
92: } elseif ($ext == 'gdf') {
93: $font = new Font\GDF($file, $color);
94: } else {
95: throw new InvalidFontFileException("'$file' appears to be an invalid font file.");
96: }
97:
98: $this->setFont($font);
99: return $font;
100: }
101:
102: /**
103: * Write text on the image at specified position
104: *
105: * You must set a font with a call to \WideImage\Canvas::setFont() prior to writing text to the image.
106: *
107: * Smart coordinates are supported for $x and $y arguments, but currently only for TTF/OTF fonts.
108: *
109: * Example:
110: * <code>
111: * $img = WideImage::load('pic.jpg');
112: * $canvas = $img->getCanvas();
113: * $canvas->useFont('Verdana.ttf', 16, $img->allocateColor(255, 0, 0));
114: * $canvas->writeText('right', 'bottom', 'www.website.com');
115: * </code>
116: *
117: * @param int $x Left
118: * @param int $y Top
119: * @param string $text Text to write
120: * @param int $angle The angle, defaults to 0
121: */
122: public function writeText($x, $y, $text, $angle = 0)
123: {
124: if ($this->font === null) {
125: throw new NoFontException("Can't write text without a font.");
126: }
127:
128: $angle = - floatval($angle);
129:
130: if ($angle < 0) {
131: $angle = 360 + $angle;
132: }
133:
134: $angle = $angle % 360;
135:
136: $this->font->writeText($this->image, $x, $y, $text, $angle);
137: }
138:
139: /**
140: * A magic method that allows you to call any PHP function that starts with "image".
141: *
142: * This is a shortcut to call custom functions on the image handle.
143: *
144: * Example:
145: * <code>
146: * $img = WideImage::load('pic.jpg');
147: * $canvas = $img->getCanvas();
148: * $canvas->filledRect(10, 10, 20, 30, $img->allocateColor(0, 0, 0));
149: * $canvas->line(60, 80, 30, 100, $img->allocateColor(255, 0, 0));
150: * </code>
151: */
152: function __call($method, $params)
153: {
154: if (function_exists('image' . $method)) {
155: array_unshift($params, $this->handle);
156: call_user_func_array('image' . $method, $params);
157: } else {
158: throw new InvalidCanvasMethodException("Function doesn't exist: image{$method}.");
159: }
160: }
161: }
162: