14 protected $fontSize = 12;
24 protected $alignX = 'left';
29 protected $alignY = 'top';
34 protected $lineHeight = 1.25;
39 protected $baseline = 0.2;
44 protected $fontFace = null;
49 protected $debug = false;
54 protected $textShadow = false;
59 protected $box = array(
66 public function __construct(&$image)
69 $this->fontColor = new Color(0, 0, 0);
73 * @param Color $color Font color
75 public function setFontColor(Color $color)
77 $this->fontColor = $color;
81 * @param string $path Path to the font file
83 public function setFontFace($path)
85 $this->fontFace = $path;
89 * @param int $v Font size in *pixels*
91 public function setFontSize($v)
97 * @param Color $color Shadow color
98 * @param int $xShift Relative shadow position in pixels. Positive values move shadow to right, negative to left.
99 * @param int $yShift Relative shadow position in pixels. Positive values move shadow to bottom, negative to up.
101 public function setTextShadow(Color $color, $xShift, $yShift)
103 $this->textShadow = array(
111 * Allows to customize spacing between lines.
112 * @param float $v Height of the single text line, in percents, proportionally to font size
114 public function setLineHeight($v)
116 $this->lineHeight = $v;
120 * @param float $v Position of baseline, in percents, proportionally to line height measuring from the bottom.
122 public function setBaseline($v)
124 $this->baseline = $v;
128 * Sets text alignment inside textbox
129 * @param string $x Horizontal alignment. Allowed values are: left, center, right.
130 * @param string $y Vertical alignment. Allowed values are: top, center, bottom.
132 public function setTextAlign($x = 'left', $y = 'top')
134 $xAllowed = array('left', 'right', 'center');
135 $yAllowed = array('top', 'bottom', 'center');
137 if (!in_array($x, $xAllowed)) {
138 throw new \InvalidArgumentException('Invalid horizontal alignement value was specified.');
141 if (!in_array($y, $yAllowed)) {
142 throw new \InvalidArgumentException('Invalid vertical alignement value was specified.');
150 * Sets textbox position and dimensions
151 * @param int $x Distance in pixels from left edge of image.
152 * @param int $y Distance in pixels from top edge of image.
153 * @param int $width Width of texbox in pixels.
154 * @param int $height Height of textbox in pixels.
156 public function setBox($x, $y, $width, $height)
158 $this->box['x'] = $x;
159 $this->box['y'] = $y;
160 $this->box['width'] = $width;
161 $this->box['height'] = $height;
165 * Enables debug mode. Whole textbox and individual lines will be filled with random colors.
167 public function enableDebug()
173 * Draws the text on the picture.
174 * @param string $text Text to draw. May contain newline characters.
176 public function draw($text)
178 if (!isset($this->fontFace)) {
179 throw new \InvalidArgumentException('No path to font file has been specified.');
183 // Split text explicitly into lines by \n, \r\n and \r
184 $explicitLines = preg_split('/\n|\r\n?/', $text);
185 foreach ($explicitLines as $line) {
186 // Check every line if it needs to be wrapped
187 $words = explode(" ", $line);
189 for ($i = 1; $i < count($words); $i++) {
190 $box = $this->calculateBox($line." ".$words[$i]);
191 if (($box[4]-$box[6]) >= $this->box['width']) {
195 $line .= " ".$words[$i];
202 // Marks whole texbox area with color
203 $this->drawFilledRectangle(
207 $this->box['height'],
208 new Color(rand(180, 255), rand(180, 255), rand(180, 255), 80)
212 $lineHeightPx = $this->lineHeight * $this->fontSize;
213 $textHeight = count($lines) * $lineHeightPx;
215 switch ($this->alignY) {
217 $yAlign = ($this->box['height'] / 2) - ($textHeight / 2);
220 $yAlign = $this->box['height'] - $textHeight;
228 foreach ($lines as $line) {
229 $box = $this->calculateBox($line);
230 $boxWidth = $box[2] - $box[0];
231 switch ($this->alignX) {
233 $xAlign = ($this->box['width'] - $boxWidth) / 2;
236 $xAlign = ($this->box['width'] - $boxWidth);
242 $yShift = $lineHeightPx * (1 - $this->baseline);
244 // current line X and Y position
245 $xMOD = $this->box['x'] + $xAlign;
246 $yMOD = $this->box['y'] + $yAlign + $yShift + ($n * $lineHeightPx);
249 // Marks current line with color
250 $this->drawFilledRectangle(
252 $this->box['y'] + $yAlign + ($n * $lineHeightPx),
255 new Color(rand(1, 180), rand(1, 180), rand(1, 180))
259 if ($this->textShadow !== false) {
261 $xMOD + $this->textShadow['x'],
262 $yMOD + $this->textShadow['y'],
263 $this->textShadow['color'],
279 protected function getFontSizeInPoints()
281 return 0.75 * $this->fontSize;
284 protected function drawFilledRectangle($x, $y, $width, $height, Color $color)
286 imagefilledrectangle($this->im, $x, $y, $x + $width, $y + $height,
287 $color->getIndex($this->im)
291 protected function calculateBox($text)
293 return imageftbbox($this->getFontSizeInPoints(), 0, $this->fontFace, $text);
296 protected function drawInternal($x, $y, Color $color, $text)
300 $this->getFontSizeInPoints(),
304 $color->getIndex($this->im),