AbstractFont.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace Intervention\Image;
  3. abstract class AbstractFont
  4. {
  5. /**
  6. * Text to be written
  7. *
  8. * @var String
  9. */
  10. public $text;
  11. /**
  12. * Text size in pixels
  13. *
  14. * @var integer
  15. */
  16. public $size = 12;
  17. /**
  18. * Color of the text
  19. *
  20. * @var mixed
  21. */
  22. public $color = '000000';
  23. /**
  24. * Rotation angle of the text
  25. *
  26. * @var integer
  27. */
  28. public $angle = 0;
  29. /**
  30. * Horizontal alignment of the text
  31. *
  32. * @var String
  33. */
  34. public $align;
  35. /**
  36. * Vertical alignment of the text
  37. *
  38. * @var String
  39. */
  40. public $valign;
  41. /**
  42. * Path to TTF or GD library internal font file of the text
  43. *
  44. * @var mixed
  45. */
  46. public $file;
  47. /**
  48. * Draws font to given image on given position
  49. *
  50. * @param Image $image
  51. * @param integer $posx
  52. * @param integer $posy
  53. * @return boolean
  54. */
  55. abstract public function applyToImage(Image $image, $posx = 0, $posy = 0);
  56. /**
  57. * Create a new instance of Font
  58. *
  59. * @param Strinf $text Text to be written
  60. */
  61. public function __construct($text = null)
  62. {
  63. $this->text = $text;
  64. }
  65. /**
  66. * Set text to be written
  67. *
  68. * @param String $text
  69. * @return void
  70. */
  71. public function text($text)
  72. {
  73. $this->text = $text;
  74. return $this;
  75. }
  76. /**
  77. * Get text to be written
  78. *
  79. * @return String
  80. */
  81. public function getText()
  82. {
  83. return $this->text;
  84. }
  85. /**
  86. * Set font size in pixels
  87. *
  88. * @param integer $size
  89. * @return void
  90. */
  91. public function size($size)
  92. {
  93. $this->size = $size;
  94. return $this;
  95. }
  96. /**
  97. * Get font size in pixels
  98. *
  99. * @return integer
  100. */
  101. public function getSize()
  102. {
  103. return $this->size;
  104. }
  105. /**
  106. * Set color of text to be written
  107. *
  108. * @param mixed $color
  109. * @return void
  110. */
  111. public function color($color)
  112. {
  113. $this->color = $color;
  114. return $this;
  115. }
  116. /**
  117. * Get color of text
  118. *
  119. * @return mixed
  120. */
  121. public function getColor()
  122. {
  123. return $this->color;
  124. }
  125. /**
  126. * Set rotation angle of text
  127. *
  128. * @param integer $angle
  129. * @return void
  130. */
  131. public function angle($angle)
  132. {
  133. $this->angle = $angle;
  134. return $this;
  135. }
  136. /**
  137. * Get rotation angle of text
  138. *
  139. * @return integer
  140. */
  141. public function getAngle()
  142. {
  143. return $this->angle;
  144. }
  145. /**
  146. * Set horizontal text alignment
  147. *
  148. * @param string $align
  149. * @return void
  150. */
  151. public function align($align)
  152. {
  153. $this->align = $align;
  154. return $this;
  155. }
  156. /**
  157. * Get horizontal text alignment
  158. *
  159. * @return string
  160. */
  161. public function getAlign()
  162. {
  163. return $this->align;
  164. }
  165. /**
  166. * Set vertical text alignment
  167. *
  168. * @param string $valign
  169. * @return void
  170. */
  171. public function valign($valign)
  172. {
  173. $this->valign = $valign;
  174. return $this;
  175. }
  176. /**
  177. * Get vertical text alignment
  178. *
  179. * @return string
  180. */
  181. public function getValign()
  182. {
  183. return $this->valign;
  184. }
  185. /**
  186. * Set path to font file
  187. *
  188. * @param string $file
  189. * @return void
  190. */
  191. public function file($file)
  192. {
  193. $this->file = $file;
  194. return $this;
  195. }
  196. /**
  197. * Get path to font file
  198. *
  199. * @return string
  200. */
  201. public function getFile()
  202. {
  203. return $this->file;
  204. }
  205. /**
  206. * Checks if current font has access to an applicable font file
  207. *
  208. * @return boolean
  209. */
  210. protected function hasApplicableFontFile()
  211. {
  212. if (is_string($this->file)) {
  213. return file_exists($this->file);
  214. }
  215. return false;
  216. }
  217. /**
  218. * Counts lines of text to be written
  219. *
  220. * @return integer
  221. */
  222. public function countLines()
  223. {
  224. return count(explode(PHP_EOL, $this->text));
  225. }
  226. }