/plugins/sfImageTransformPlugin/lib/transforms/GD/sfImageTextGD.class.php

https://github.com/sanjeevan/codelovely · PHP · 288 lines · 121 code · 40 blank · 127 comment · 2 complexity · 4b0096ef8aae8095d7af5f0e6cf07740 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the sfImageTransform package.
  4. * (c) 2007 Stuart <stuart.lowes@gmail.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. *
  11. * sfImageTextGD class.
  12. *
  13. * Adds text to the image.
  14. *
  15. * Text.
  16. *
  17. * @package sfImageTransform
  18. * @author Stuart Lowes <stuart.lowes@gmail.com>
  19. * @version SVN: $Id$
  20. */
  21. class sfImageTextGD extends sfImageTransformAbstract
  22. {
  23. /**
  24. * Font face.
  25. */
  26. protected $font = 'Arial';
  27. /**
  28. * Font size.
  29. */
  30. protected $size = 10;
  31. /**
  32. * Text.
  33. */
  34. protected $text = '';
  35. /**
  36. * Angel of the text.
  37. */
  38. protected $angle = 0;
  39. /**
  40. * X coordinate.
  41. */
  42. protected $x = 0;
  43. /**
  44. * Y coordinate.
  45. */
  46. protected $y = 0;
  47. /**
  48. * Font Color.
  49. */
  50. protected $color = '#000000';
  51. /**
  52. * Path to font.
  53. */
  54. protected $font_dir = '';
  55. /**
  56. * Construct an sfImageText object.
  57. *
  58. * @param array integer
  59. */
  60. public function __construct($text, $x=0, $y=0, $size=10, $font='Arial', $color='#000000', $angle=0)
  61. {
  62. $this->font_dir = sfConfig::get('app_sfImageTransformPlugin_font_dir','/usr/share/fonts/truetype/msttcorefonts');
  63. $this->setText($text);
  64. $this->setX($x);
  65. $this->setY($y);
  66. $this->setSize($size);
  67. $this->setFont($font);
  68. $this->setColor($color);
  69. $this->setAngle($angle);
  70. }
  71. /**
  72. * Sets the text.
  73. *
  74. * @param string
  75. */
  76. public function setText($text)
  77. {
  78. $this->text = $text;
  79. }
  80. /**
  81. * Gets the text.
  82. *
  83. * @return string
  84. */
  85. public function getText()
  86. {
  87. return $this->text;
  88. }
  89. /**
  90. * Sets X coordinate.
  91. *
  92. * @param integer
  93. */
  94. public function setX($x)
  95. {
  96. $this->x = $x;
  97. }
  98. /**
  99. * Gets X coordinate.
  100. *
  101. * @return integer
  102. */
  103. public function getX()
  104. {
  105. return $this->x;
  106. }
  107. /**
  108. * Sets Y coordinate.
  109. *
  110. * @param integer
  111. */
  112. public function setY($y)
  113. {
  114. $this->y = $y;
  115. }
  116. /**
  117. * Gets Y coordinate.
  118. *
  119. * @return integer
  120. */
  121. public function getY()
  122. {
  123. return $this->y;
  124. }
  125. /**
  126. * Sets text size.
  127. *
  128. * @param integer
  129. */
  130. public function setSize($size)
  131. {
  132. $this->size = $size;
  133. }
  134. /**
  135. * Gets text size.
  136. *
  137. * @return integer
  138. */
  139. public function getSize()
  140. {
  141. return $this->size;
  142. }
  143. /**
  144. * Sets text font.
  145. *
  146. * @param string
  147. */
  148. public function setFont($font)
  149. {
  150. $this->font = str_replace(' ', '_', $font);
  151. }
  152. /**
  153. * Gets text font.
  154. *
  155. * @return string
  156. */
  157. public function getFont()
  158. {
  159. return $this->font;
  160. }
  161. /**
  162. * Sets text color.
  163. *
  164. * @param string
  165. */
  166. public function setColor($color)
  167. {
  168. $this->color = $color;
  169. }
  170. /**
  171. * Gets text color.
  172. *
  173. * @return string
  174. */
  175. public function getColor()
  176. {
  177. return $this->color;
  178. }
  179. /**
  180. * Sets text angle.
  181. *
  182. * @param string
  183. */
  184. public function setAngle($angle)
  185. {
  186. $this->angle = $angle;
  187. }
  188. /**
  189. * Gets text angle.
  190. *
  191. * @return string
  192. */
  193. public function getAngle()
  194. {
  195. return $this->angle;
  196. }
  197. /**
  198. * Apply the transform to the sfImage object.
  199. *
  200. * @access protected
  201. * @param sfImage
  202. * @return sfImage
  203. */
  204. protected function transform(sfImage $image)
  205. {
  206. $resource = $image->getAdapter()->getHolder();
  207. $this->font = $this->font_dir . DIRECTORY_SEPARATOR . $this->font . '.ttf';
  208. $lines = preg_split('/[\n\r]+/', $this->getText());
  209. print $this->getMaximumLineLength($lines);
  210. exit;
  211. foreach($lines as $line)
  212. {
  213. $this->writeLine($image, $line);
  214. }
  215. }
  216. protected function writeLine($image, $text)
  217. {
  218. $resource = $image->getAdapter()->getHolder();
  219. $box = imageTTFBbox($this->size,$this->angle,$this->font,$this->text);
  220. $textwidth = abs($box[4] - $box[0]) - 4;
  221. $textheight = abs($box[5] - $box[1]) - 4;
  222. $rgb = sscanf($this->color, '#%2x%2x%2x');
  223. $color = imagecolorallocate($resource, $rgb[0], $rgb[1], $rgb[2]);
  224. // disable alpha handling to enable font rendering
  225. imagealphablending($resource, true);
  226. imagettftext($resource, $this->size, $this->angle, $this->x, $this->y + $textheight, $color, $this->font, $this->text);
  227. return $image;
  228. }
  229. // Find the longest line
  230. protected function getMaximumLineLength($lines)
  231. {
  232. if(!is_array($lines))
  233. {
  234. $lines = preg_split('/[\n\r]+/', $lines);
  235. }
  236. $max = 0;
  237. $len = 0;
  238. foreach($lines as $line)
  239. {
  240. $len = strlen($line);
  241. if($len > $max)
  242. {
  243. $max = $len;
  244. }
  245. }
  246. return $max;
  247. }
  248. }