Cheetah
Font.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use \Intervention\Image\Image;
6 
8 {
17  public function applyToImage(Image $image, $posx = 0, $posy = 0)
18  {
19  // build draw object
20  $draw = new \ImagickDraw();
21  $draw->setStrokeAntialias(true);
22  $draw->setTextAntialias(true);
23 
24  // set font file
25  if ($this->hasApplicableFontFile()) {
26  $draw->setFont($this->file);
27  } else {
28  throw new \Intervention\Image\Exception\RuntimeException(
29  "Font file must be provided to apply text to image."
30  );
31  }
32 
33  // parse text color
34  $color = new Color($this->color);
35 
36  $draw->setFontSize($this->size);
37  $draw->setFillColor($color->getPixel());
38 
39  // align horizontal
40  switch (strtolower($this->align)) {
41  case 'center':
42  $align = \Imagick::ALIGN_CENTER;
43  break;
44 
45  case 'right':
46  $align = \Imagick::ALIGN_RIGHT;
47  break;
48 
49  default:
50  $align = \Imagick::ALIGN_LEFT;
51  break;
52  }
53 
54  $draw->setTextAlignment($align);
55 
56  // align vertical
57  if (strtolower($this->valign) != 'bottom') {
58 
59  // calculate box size
60  $dimensions = $image->getCore()->queryFontMetrics($draw, $this->text);
61 
62  // corrections on y-position
63  switch (strtolower($this->valign)) {
64  case 'center':
65  case 'middle':
66  $posy = $posy + $dimensions['textHeight'] * 0.65 / 2;
67  break;
68 
69  case 'top':
70  $posy = $posy + $dimensions['textHeight'] * 0.65;
71  break;
72  }
73  }
74 
75  // apply to image
76  $image->getCore()->annotateImage($draw, $posx, $posy, $this->angle * (-1), $this->text);
77  }
78 }
Intervention\Image\Imagick\Font\applyToImage
applyToImage(Image $image, $posx=0, $posy=0)
Definition: Font.php:17
Intervention\Image\Imagick
Definition: Color.php:3
Intervention\Image\Imagick\Color
Definition: Color.php:6
php
Intervention\Image\AbstractFont\align
align($align)
Definition: AbstractFont.php:166
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\Imagick\Font
Definition: Font.php:8
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\AbstractFont
Definition: AbstractFont.php:6
Intervention\Image\AbstractFont\$color
$color
Definition: AbstractFont.php:26
Intervention\Image\AbstractFont\size
size($size)
Definition: AbstractFont.php:103
Intervention\Image\AbstractFont\hasApplicableFontFile
hasApplicableFontFile()
Definition: AbstractFont.php:228
Intervention\Image\AbstractFont\angle
angle($angle)
Definition: AbstractFont.php:145