Font.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace Intervention\Image\Imagick;
  3. use Intervention\Image\AbstractFont;
  4. use Intervention\Image\Exception\RuntimeException;
  5. use Intervention\Image\Image;
  6. class Font extends AbstractFont
  7. {
  8. /**
  9. * Draws font to given image at given position
  10. *
  11. * @param Image $image
  12. * @param int $posx
  13. * @param int $posy
  14. * @return void
  15. */
  16. public function applyToImage(Image $image, $posx = 0, $posy = 0)
  17. {
  18. // build draw object
  19. $draw = new \ImagickDraw();
  20. $draw->setStrokeAntialias(true);
  21. $draw->setTextAntialias(true);
  22. // set font file
  23. if ($this->hasApplicableFontFile()) {
  24. $draw->setFont($this->file);
  25. } else {
  26. throw new RuntimeException(
  27. "Font file must be provided to apply text to image."
  28. );
  29. }
  30. // parse text color
  31. $color = new Color($this->color);
  32. $draw->setFontSize($this->size);
  33. $draw->setFillColor($color->getPixel());
  34. // align horizontal
  35. switch (strtolower($this->align)) {
  36. case 'center':
  37. $align = \Imagick::ALIGN_CENTER;
  38. break;
  39. case 'right':
  40. $align = \Imagick::ALIGN_RIGHT;
  41. break;
  42. default:
  43. $align = \Imagick::ALIGN_LEFT;
  44. break;
  45. }
  46. $draw->setTextAlignment($align);
  47. // align vertical
  48. if (strtolower($this->valign) != 'bottom') {
  49. // corrections on y-position
  50. switch (strtolower($this->valign)) {
  51. case 'center':
  52. case 'middle':
  53. // calculate box size
  54. $dimensions = $image->getCore()->queryFontMetrics($draw, $this->text);
  55. $posy = $posy + $dimensions['textHeight'] * 0.65 / 2;
  56. break;
  57. case 'top':
  58. // calculate box size
  59. $dimensions = $image->getCore()->queryFontMetrics($draw, $this->text, false);
  60. $posy = $posy + $dimensions['textHeight'] * 0.65;
  61. break;
  62. }
  63. }
  64. // apply to image
  65. $image->getCore()->annotateImage($draw, $posx, $posy, $this->angle * (-1), $this->text);
  66. }
  67. /**
  68. * Calculates bounding box of current font setting
  69. *
  70. * @return array
  71. */
  72. public function getBoxSize()
  73. {
  74. $box = [];
  75. // build draw object
  76. $draw = new \ImagickDraw();
  77. $draw->setStrokeAntialias(true);
  78. $draw->setTextAntialias(true);
  79. // set font file
  80. if ($this->hasApplicableFontFile()) {
  81. $draw->setFont($this->file);
  82. } else {
  83. throw new RuntimeException(
  84. "Font file must be provided to apply text to image."
  85. );
  86. }
  87. $draw->setFontSize($this->size);
  88. $dimensions = (new \Imagick())->queryFontMetrics($draw, $this->text);
  89. if (strlen($this->text) == 0) {
  90. // no text -> no boxsize
  91. $box['width'] = 0;
  92. $box['height'] = 0;
  93. } else {
  94. // get boxsize
  95. $box['width'] = intval(abs($dimensions['textWidth']));
  96. $box['height'] = intval(abs($dimensions['textHeight']));
  97. }
  98. return $box;
  99. }
  100. }