Cheetah
Font.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Intervention\Image\Gd;
4 
5 use \Intervention\Image\Image;
6 
8 {
14  protected function getPointSize()
15  {
16  return intval(ceil($this->size * 0.75));
17  }
18 
24  private function getInternalFont()
25  {
26  $internalfont = is_null($this->file) ? 1 : $this->file;
27  $internalfont = is_numeric($internalfont) ? $internalfont : false;
28 
29  if ( ! in_array($internalfont, array(1, 2, 3, 4, 5))) {
30  throw new \Intervention\Image\Exception\NotSupportedException(
31  sprintf('Internal GD font (%s) not available. Use only 1-5.', $internalfont)
32  );
33  }
34 
35  return intval($internalfont);
36  }
37 
43  private function getInternalFontWidth()
44  {
45  return $this->getInternalFont() + 4;
46  }
47 
53  private function getInternalFontHeight()
54  {
55  switch ($this->getInternalFont()) {
56  case 1:
57  return 8;
58 
59  case 2:
60  return 14;
61 
62  case 3:
63  return 14;
64 
65  case 4:
66  return 16;
67 
68  case 5:
69  return 16;
70  }
71  }
72 
78  public function getBoxSize()
79  {
80  $box = array();
81 
82  if ($this->hasApplicableFontFile()) {
83 
84  // get bounding box with angle 0
85  $box = imagettfbbox($this->getPointSize(), 0, $this->file, $this->text);
86 
87  // rotate points manually
88  if ($this->angle != 0) {
89 
90  $angle = pi() * 2 - $this->angle * pi() * 2 / 360;
91 
92  for ($i=0; $i<4; $i++) {
93  $x = $box[$i * 2];
94  $y = $box[$i * 2 + 1];
95  $box[$i * 2] = cos($angle) * $x - sin($angle) * $y;
96  $box[$i * 2 + 1] = sin($angle) * $x + cos($angle) * $y;
97  }
98  }
99 
100  $box['width'] = intval(abs($box[4] - $box[0]));
101  $box['height'] = intval(abs($box[5] - $box[1]));
102 
103  } else {
104 
105  // get current internal font size
106  $width = $this->getInternalFontWidth();
107  $height = $this->getInternalFontHeight();
108 
109  if (strlen($this->text) == 0) {
110  // no text -> no boxsize
111  $box['width'] = 0;
112  $box['height'] = 0;
113  } else {
114  // calculate boxsize
115  $box['width'] = strlen($this->text) * $width;
116  $box['height'] = $height;
117  }
118  }
119 
120  return $box;
121  }
122 
131  public function applyToImage(Image $image, $posx = 0, $posy = 0)
132  {
133  // parse text color
134  $color = new Color($this->color);
135 
136  if ($this->hasApplicableFontFile()) {
137 
138  if ($this->angle != 0 || is_string($this->align) || is_string($this->valign)) {
139 
140  $box = $this->getBoxSize();
141 
142  $align = is_null($this->align) ? 'left' : strtolower($this->align);
143  $valign = is_null($this->valign) ? 'bottom' : strtolower($this->valign);
144 
145  // correction on position depending on v/h alignment
146  switch ($align.'-'.$valign) {
147 
148  case 'center-top':
149  $posx = $posx - round(($box[6]+$box[4])/2);
150  $posy = $posy - round(($box[7]+$box[5])/2);
151  break;
152 
153  case 'right-top':
154  $posx = $posx - $box[4];
155  $posy = $posy - $box[5];
156  break;
157 
158  case 'left-top':
159  $posx = $posx - $box[6];
160  $posy = $posy - $box[7];
161  break;
162 
163  case 'center-center':
164  case 'center-middle':
165  $posx = $posx - round(($box[0]+$box[4])/2);
166  $posy = $posy - round(($box[1]+$box[5])/2);
167  break;
168 
169  case 'right-center':
170  case 'right-middle':
171  $posx = $posx - round(($box[2]+$box[4])/2);
172  $posy = $posy - round(($box[3]+$box[5])/2);
173  break;
174 
175  case 'left-center':
176  case 'left-middle':
177  $posx = $posx - round(($box[0]+$box[6])/2);
178  $posy = $posy - round(($box[1]+$box[7])/2);
179  break;
180 
181  case 'center-bottom':
182  $posx = $posx - round(($box[0]+$box[2])/2);
183  $posy = $posy - round(($box[1]+$box[3])/2);
184  break;
185 
186  case 'right-bottom':
187  $posx = $posx - $box[2];
188  $posy = $posy - $box[3];
189  break;
190 
191  case 'left-bottom':
192  $posx = $posx - $box[0];
193  $posy = $posy - $box[1];
194  break;
195  }
196  }
197 
198  // enable alphablending for imagettftext
199  imagealphablending($image->getCore(), true);
200 
201  // draw ttf text
202  imagettftext($image->getCore(), $this->getPointSize(), $this->angle, $posx, $posy, $color->getInt(), $this->file, $this->text);
203 
204  } else {
205 
206  // get box size
207  $box = $this->getBoxSize();
208  $width = $box['width'];
209  $height = $box['height'];
210 
211  // internal font specific position corrections
212  if ($this->getInternalFont() == 1) {
213  $top_correction = 1;
214  $bottom_correction = 2;
215  } elseif ($this->getInternalFont() == 3) {
216  $top_correction = 2;
217  $bottom_correction = 4;
218  } else {
219  $top_correction = 3;
220  $bottom_correction = 4;
221  }
222 
223  // x-position corrections for horizontal alignment
224  switch (strtolower($this->align)) {
225  case 'center':
226  $posx = ceil($posx - ($width / 2));
227  break;
228 
229  case 'right':
230  $posx = ceil($posx - $width) + 1;
231  break;
232  }
233 
234  // y-position corrections for vertical alignment
235  switch (strtolower($this->valign)) {
236  case 'center':
237  case 'middle':
238  $posy = ceil($posy - ($height / 2));
239  break;
240 
241  case 'top':
242  $posy = ceil($posy - $top_correction);
243  break;
244 
245  default:
246  case 'bottom':
247  $posy = round($posy - $height + $bottom_correction);
248  break;
249  }
250 
251  // draw text
252  imagestring($image->getCore(), $this->getInternalFont(), $posx, $posy, $this->text, $color->getInt());
253  }
254  }
255 }
Intervention\Image\Gd\Font\getBoxSize
getBoxSize()
Definition: Font.php:78
Intervention\Image\AbstractFont\$file
$file
Definition: AbstractFont.php:54
Intervention\Image\AbstractFont\$angle
$angle
Definition: AbstractFont.php:33
php
Intervention\Image\AbstractFont\align
align($align)
Definition: AbstractFont.php:166
Intervention\Image\Gd
Definition: Color.php:3
Intervention\Image\AbstractFont\color
color($color)
Definition: AbstractFont.php:124
Intervention\Image\AbstractFont\file
file($file)
Definition: AbstractFont.php:208
Intervention\Image\Image\getCore
getCore()
Definition: Image.php:186
Intervention\Image\Image
Definition: Image.php:50
Intervention\Image\Gd\Font\applyToImage
applyToImage(Image $image, $posx=0, $posy=0)
Definition: Font.php:131
Intervention\Image\AbstractFont\$valign
$valign
Definition: AbstractFont.php:47
Intervention\Image\AbstractFont\text
text($text)
Definition: AbstractFont.php:82
Intervention\Image\AbstractFont\$align
$align
Definition: AbstractFont.php:40
Intervention\Image\AbstractFont\valign
valign($valign)
Definition: AbstractFont.php:187
Intervention\Image\Gd\Color
Definition: Color.php:8
Intervention\Image\AbstractFont
Definition: AbstractFont.php:6
Intervention\Image\Gd\Font\getPointSize
getPointSize()
Definition: Font.php:14
Intervention\Image\AbstractFont\$color
$color
Definition: AbstractFont.php:26
Intervention\Image\AbstractFont\size
size($size)
Definition: AbstractFont.php:103
Intervention\Image\Gd\Font
Definition: Font.php:8
Intervention\Image\AbstractFont\hasApplicableFontFile
hasApplicableFontFile()
Definition: AbstractFont.php:228
Intervention\Image\AbstractFont\angle
angle($angle)
Definition: AbstractFont.php:145