1 <?php
2 /**
3 * @author Gasper Kozak
4 * @copyright 2007-2011
5
6 This file is part of WideImage.
7
8 WideImage is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 WideImage is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with WideImage; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
22 * @package WideImage
23 **/
24
25 /**
26 * @package Exceptions
27 */
28 class WideImage_NoFontException extends WideImage_Exception {}
29
30 /**
31 * @package Exceptions
32 */
33 class WideImage_InvalidFontFileException extends WideImage_Exception {}
34
35 /**
36 * @package Exceptions
37 */
38 class WideImage_InvalidCanvasMethodException extends WideImage_Exception {}
39
40 /**
41 * @package WideImage
42 */
43 class WideImage_Canvas
44 {
45 protected $handle = 0;
46 protected $image = null;
47 protected $font = null;
48
49 /**
50 * Creates a canvas object that writes to the image passed as a parameter
51 *
52 * Shouldn't be used directly, use WideImage_Image::getCanvas() instead.
53 *
54 * @param WideImage_Image $img Image object
55 */
56 function __construct($img)
57 {
58 $this->handle = $img->getHandle();
59 $this->image = $img;
60 }
61
62 /**
63 * Sets the active font. Can be an instance of
64 * WideImage_Font_TTF, WideImage_Font_PS, or WideImage_Font_GDF.
65 *
66 *
67 *
68 *
69 * @param object $font Font object to set for writeText()
70 */
71 function setFont($font)
72 {
73 $this->font = $font;
74 }
75
76 /**
77 * Creates and sets the current font
78 *
79 * The supported font types are: TTF/OTF, PS, and GDF.
80 * Font type is detected from the extension. If the $file parameter doesn't have an extension, TTF font is presumed.
81 *
82 * Note: not all parameters are supported by all fonts.
83 *
84 * @param string $file Font file name (string)
85 * @param int $size Font size (supported for TTF/OTF and PS fonts, ignored for GDF)
86 * @param int $color Text color
87 * @param int $bgcolor Background color (supported only for PS font, ignored for TTF and PS)
88 * @return mixed One of the WideImage_Font_* objects
89 */
90 function useFont($file, $size = 12, $color = 0, $bgcolor = null)
91 {
92 $p = strrpos($file, '.');
93 if ($p === false || $p < strlen($file) - 4)
94 $ext = 'ttf';
95 else
96 $ext = strtolower(substr($file, $p + 1));
97
98 if ($ext == 'ttf' || $ext == 'otf')
99 $font = new WideImage_Font_TTF($file, $size, $color);
100 elseif ($ext == 'ps')
101 $font = new WideImage_Font_PS($file, $size, $color, $bgcolor);
102 elseif ($ext == 'gdf')
103 $font = new WideImage_Font_GDF($file, $color);
104 else
105 throw new WideImage_InvalidFontFileException("'$file' appears to be an invalid font file.");
106
107 $this->setFont($font);
108 return $font;
109 }
110
111 /**
112 * Write text on the image at specified position
113 *
114 * You must set a font with a call to WideImage_Canvas::setFont() prior to writing text to the image.
115 *
116 * Smart coordinates are supported for $x and $y arguments, but currently only for TTF/OTF fonts.
117 *
118 * Example:
119 * <code>
120 * $img = WideImage::load('pic.jpg');
121 * $canvas = $img->getCanvas();
122 * $canvas->useFont('Verdana.ttf', 16, $img->allocateColor(255, 0, 0));
123 * $canvas->writeText('right', 'bottom', 'www.website.com');
124 * </code>
125 *
126 * @param int $x Left
127 * @param int $y Top
128 * @param string $text Text to write
129 * @param int $angle The angle, defaults to 0
130 */
131 function writeText($x, $y, $text, $angle = 0)
132 {
133 if ($this->font === null)
134 throw new WideImage_NoFontException("Can't write text without a font.");
135
136 $angle = - floatval($angle);
137 if ($angle < 0)
138 $angle = 360 + $angle;
139 $angle = $angle % 360;
140
141 $this->font->writeText($this->image, $x, $y, $text, $angle);
142 }
143
144 /**
145 * A magic method that allows you to call any PHP function that starts with "image".
146 *
147 * This is a shortcut to call custom functions on the image handle.
148 *
149 * Example:
150 * <code>
151 * $img = WideImage::load('pic.jpg');
152 * $canvas = $img->getCanvas();
153 * $canvas->filledRect(10, 10, 20, 30, $img->allocateColor(0, 0, 0));
154 * $canvas->line(60, 80, 30, 100, $img->allocateColor(255, 0, 0));
155 * </code>
156 */
157 function __call($method, $params)
158 {
159 if (function_exists('image' . $method))
160 {
161 array_unshift($params, $this->handle);
162 call_user_func_array('image' . $method, $params);
163 }
164 else
165 throw new WideImage_InvalidCanvasMethodException("Function doesn't exist: image{$method}.");
166 }
167 }
168