PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/intervention/image/src/Intervention/Image/Font.php

https://gitlab.com/daniruizcamacho/pfcascensores
PHP | 472 lines | 230 code | 73 blank | 169 comment | 24 complexity | ede30a338d13c07af5e4561b76100d53 MD5 | raw file
  1. <?php
  2. namespace Intervention\Image;
  3. class Font
  4. {
  5. /**
  6. * Text to be written
  7. *
  8. * @var String
  9. */
  10. private $text;
  11. /**
  12. * Text size in pixels
  13. *
  14. * @var integer
  15. */
  16. private $size = 12;
  17. /**
  18. * Color of the text
  19. *
  20. * @var mixed
  21. */
  22. private $color = '000000';
  23. /**
  24. * Rotation angle of the text
  25. *
  26. * @var integer
  27. */
  28. private $angle = 0;
  29. /**
  30. * Horizontal alignment of the text
  31. *
  32. * @var String
  33. */
  34. private $align;
  35. /**
  36. * Vertical alignment of the text
  37. *
  38. * @var String
  39. */
  40. private $valign;
  41. /**
  42. * Path to TTF or GD library internal font file of the text
  43. *
  44. * @var mixed
  45. */
  46. private $file;
  47. /**
  48. * Create a new instance of Font
  49. *
  50. * @param Strinf $text Text to be written
  51. */
  52. public function __construct($text = null)
  53. {
  54. $this->text = $text;
  55. }
  56. /**
  57. * Set text to be written
  58. *
  59. * @param String $text
  60. * @return void
  61. */
  62. public function text($text)
  63. {
  64. $this->text = $text;
  65. }
  66. /**
  67. * Get text to be written
  68. *
  69. * @return String
  70. */
  71. public function getText()
  72. {
  73. return $this->text;
  74. }
  75. /**
  76. * Set font size in pixels
  77. *
  78. * @param integer $size
  79. * @return void
  80. */
  81. public function size($size)
  82. {
  83. $this->size = $size;
  84. }
  85. /**
  86. * Get font size in pixels
  87. *
  88. * @return integer
  89. */
  90. public function getSize()
  91. {
  92. return $this->size;
  93. }
  94. /**
  95. * Get font size in points
  96. *
  97. * @return integer
  98. */
  99. public function getPointSize()
  100. {
  101. return intval(ceil($this->size * 0.75));
  102. }
  103. /**
  104. * Set color of text to be written
  105. *
  106. * @param mixed $color
  107. * @return void
  108. */
  109. public function color($color)
  110. {
  111. $this->color = $color;
  112. }
  113. /**
  114. * Get color of text
  115. *
  116. * @return mixed
  117. */
  118. public function getColor()
  119. {
  120. return $this->color;
  121. }
  122. /**
  123. * Set rotation angle of text
  124. *
  125. * @param integer $angle
  126. * @return void
  127. */
  128. public function angle($angle)
  129. {
  130. $this->angle = $angle;
  131. }
  132. /**
  133. * Get rotation angle of text
  134. *
  135. * @return integer
  136. */
  137. public function getAngle()
  138. {
  139. return $this->angle;
  140. }
  141. /**
  142. * Set horizontal text alignment
  143. *
  144. * @param string $align
  145. * @return void
  146. */
  147. public function align($align)
  148. {
  149. $this->align = $align;
  150. }
  151. /**
  152. * Get horizontal text alignment
  153. *
  154. * @return string
  155. */
  156. public function getAlign()
  157. {
  158. return $this->align;
  159. }
  160. /**
  161. * Set vertical text alignment
  162. *
  163. * @param string $valign
  164. * @return void
  165. */
  166. public function valign($valign)
  167. {
  168. $this->valign = $valign;
  169. }
  170. /**
  171. * Get vertical text alignment
  172. *
  173. * @return string
  174. */
  175. public function getValign()
  176. {
  177. return $this->valign;
  178. }
  179. /**
  180. * Set path to font file
  181. *
  182. * @param string $align
  183. * @return void
  184. */
  185. public function file($file)
  186. {
  187. $this->file = $file;
  188. }
  189. /**
  190. * Get path to font file
  191. *
  192. * @return string
  193. */
  194. public function getFile()
  195. {
  196. return $this->file;
  197. }
  198. /**
  199. * Checks if current font has access to an applicable font file
  200. *
  201. * @return boolean [description]
  202. */
  203. private function hasApplicableFontFile()
  204. {
  205. if (is_string($this->file)) {
  206. return file_exists($this->file);
  207. }
  208. return false;
  209. }
  210. /**
  211. * Filter function to access internal integer font values
  212. *
  213. * @return integer
  214. */
  215. private function getInternalFont()
  216. {
  217. $internalfont = is_null($this->file) ? 1 : $this->file;
  218. $internalfont = is_numeric($internalfont) ? $internalfont : false;
  219. if ( ! in_array($internalfont, array(1, 2, 3, 4, 5))) {
  220. throw new Exception\FontNotFoundException(sprintf('Internal font %s not available.', $internalfont));
  221. }
  222. return intval($internalfont);
  223. }
  224. /**
  225. * Get width of an internal font character
  226. *
  227. * @return integer
  228. */
  229. private function getInternalFontWidth()
  230. {
  231. return $this->getInternalFont() + 4;
  232. }
  233. /**
  234. * Get height of an internal font character
  235. *
  236. * @return integer
  237. */
  238. private function getInternalFontHeight()
  239. {
  240. switch ($this->getInternalFont()) {
  241. case 1:
  242. return 8;
  243. case 2:
  244. return 14;
  245. case 3:
  246. return 14;
  247. case 4:
  248. return 16;
  249. case 5:
  250. return 16;
  251. }
  252. }
  253. /**
  254. * Calculates bounding box of current font setting
  255. *
  256. * @return Array
  257. */
  258. public function getBoxSize()
  259. {
  260. $box = array();
  261. if ($this->hasApplicableFontFile()) {
  262. // get bounding box with angle 0
  263. $box = imagettfbbox($this->getPointSize(), 0, $this->file, $this->text);
  264. // rotate points manually
  265. if ($this->angle != 0) {
  266. $angle = pi() * 2 - $this->angle * pi() * 2 / 360;
  267. for ($i=0; $i<4; $i++) {
  268. $x = $box[$i * 2];
  269. $y = $box[$i * 2 + 1];
  270. $box[$i * 2] = cos($angle) * $x - sin($angle) * $y;
  271. $box[$i * 2 + 1] = sin($angle) * $x + cos($angle) * $y;
  272. }
  273. }
  274. $box['width'] = intval(abs($box[4] - $box[0]));
  275. $box['height'] = intval(abs($box[5] - $box[1]));
  276. } else {
  277. // get current internal font size
  278. $width = $this->getInternalFontWidth();
  279. $height = $this->getInternalFontHeight();
  280. if (strlen($this->text) == 0) {
  281. // no text -> no boxsize
  282. $box['width'] = 0;
  283. $box['height'] = 0;
  284. } else {
  285. // calculate boxsize
  286. $box['width'] = strlen($this->text) * $width;
  287. $box['height'] = $height;
  288. }
  289. }
  290. return $box;
  291. }
  292. /**
  293. * Draws font to given image at given position
  294. * @param Image $image
  295. * @param integer $posx
  296. * @param integer $posy
  297. * @return void
  298. */
  299. public function applyToImage(Image $image, $posx = 0, $posy = 0)
  300. {
  301. // parse text color
  302. $color = $image->parseColor($this->color);
  303. if ($this->hasApplicableFontFile()) {
  304. if ($this->angle != 0 || is_string($this->align) || is_string($this->valign)) {
  305. $box = $this->getBoxSize();
  306. $align = is_null($this->align) ? 'left' : strtolower($this->align);
  307. $valign = is_null($this->valign) ? 'bottom' : strtolower($this->valign);
  308. // correction on position depending on v/h alignment
  309. switch ($align.'-'.$valign) {
  310. case 'center-top':
  311. $posx = $posx - round(($box[6]+$box[4])/2);
  312. $posy = $posy - round(($box[7]+$box[5])/2);
  313. break;
  314. case 'right-top':
  315. $posx = $posx - $box[4];
  316. $posy = $posy - $box[5];
  317. break;
  318. case 'left-top':
  319. $posx = $posx - $box[6];
  320. $posy = $posy - $box[7];
  321. break;
  322. case 'center-center':
  323. case 'center-middle':
  324. $posx = $posx - round(($box[0]+$box[4])/2);
  325. $posy = $posy - round(($box[1]+$box[5])/2);
  326. break;
  327. case 'right-center':
  328. case 'right-middle':
  329. $posx = $posx - round(($box[2]+$box[4])/2);
  330. $posy = $posy - round(($box[3]+$box[5])/2);
  331. break;
  332. case 'left-center':
  333. case 'left-middle':
  334. $posx = $posx - round(($box[0]+$box[6])/2);
  335. $posy = $posy - round(($box[1]+$box[7])/2);
  336. break;
  337. case 'center-bottom':
  338. $posx = $posx - round(($box[0]+$box[2])/2);
  339. $posy = $posy - round(($box[1]+$box[3])/2);
  340. break;
  341. case 'right-bottom':
  342. $posx = $posx - $box[2];
  343. $posy = $posy - $box[3];
  344. break;
  345. case 'left-bottom':
  346. $posx = $posx - $box[0];
  347. $posy = $posy - $box[1];
  348. break;
  349. }
  350. }
  351. // $image->rectangle(array(0,0,0,0.5), $posx+$box[6], $posy+$box[7], $posx+$box[2], $posy+$box[3]);
  352. // enable alphablending for imagettftext
  353. imagealphablending($image->resource, true);
  354. // draw ttf text
  355. imagettftext($image->resource, $this->getPointSize(), $this->angle, $posx, $posy, $color, $this->file, $this->text);
  356. } else {
  357. // get box size
  358. $box = $this->getBoxSize();
  359. $width = $box['width'];
  360. $height = $box['height'];
  361. // internal font specific position corrections
  362. if ($this->getInternalFont() == 1) {
  363. $top_correction = 1;
  364. $bottom_correction = 2;
  365. } elseif ($this->getInternalFont() == 3) {
  366. $top_correction = 2;
  367. $bottom_correction = 4;
  368. } else {
  369. $top_correction = 3;
  370. $bottom_correction = 4;
  371. }
  372. // x-position corrections for horizontal alignment
  373. switch (strtolower($this->align)) {
  374. case 'center':
  375. $posx = ceil($posx - ($width / 2));
  376. break;
  377. case 'right':
  378. $posx = ceil($posx - $width) + 1;
  379. break;
  380. }
  381. // y-position corrections for vertical alignment
  382. switch (strtolower($this->valign)) {
  383. case 'center':
  384. case 'middle':
  385. $posy = ceil($posy - ($height / 2));
  386. break;
  387. case 'top':
  388. $posy = ceil($posy - $top_correction);
  389. break;
  390. default:
  391. case 'bottom':
  392. $posy = round($posy - $height + $bottom_correction);
  393. break;
  394. }
  395. // draw text
  396. imagestring($image->resource, $this->getInternalFont(), $posx, $posy, $this->text, $color);
  397. }
  398. }
  399. }