/lib/transforms/GD/sfImageRectangleGD.class.php

https://github.com/AMSGWeblinks/sfImageTransformPlugin · PHP · 368 lines · 157 code · 45 blank · 166 comment · 16 complexity · 13ad5b18568059ef98fedb5454e8929d MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the sfImageTransform package.
  4. * (c) 2007 Stuart Lowes <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. * sfImageRectangleGD class.
  12. *
  13. * Draws a rectangle.
  14. *
  15. * Draws a rectangle on a GD image.
  16. *
  17. * @package sfImageTransform
  18. * @subpackage transforms
  19. * @author Stuart Lowes <stuart.lowes@gmail.com>
  20. * @version SVN: $Id$
  21. */
  22. class sfImageRectangleGD extends sfImageTransformAbstract
  23. {
  24. /**
  25. * Start X coordinate.
  26. *
  27. * @var integer
  28. */
  29. protected $x1 = 0;
  30. /**
  31. * Start Y coordinate.
  32. *
  33. * @var integer
  34. */
  35. protected $y1 = 0;
  36. /**
  37. * Finish X coordinate.
  38. *
  39. * @var integer
  40. */
  41. protected $x2 = 0;
  42. /**
  43. * Finish Y coordinate
  44. *
  45. * @var integer
  46. */
  47. protected $y2 = 0;
  48. /**
  49. * Rectangle thickness.
  50. *
  51. * @var integer
  52. */
  53. protected $thickness = 1;
  54. /**
  55. * Hex color.
  56. *
  57. * @var string
  58. */
  59. protected $color = '';
  60. /**
  61. * Fill.
  62. *
  63. * @var string/object hex or sfImage object
  64. */
  65. protected $fill = null;
  66. /**
  67. * Line style.
  68. *
  69. * @var integer
  70. */
  71. protected $style = null;
  72. /**
  73. * Construct an sfImageBlur object.
  74. *
  75. * @param integer
  76. * @param integer
  77. * @param integer
  78. * @param integer
  79. * @param integer
  80. * @param integer
  81. * @param string/object hex or sfImage object
  82. * @param integer
  83. */
  84. public function __construct($x1, $y1, $x2, $y2, $thickness=1, $color=null, $fill=null, $style=null)
  85. {
  86. $this->setStartX($x1);
  87. $this->setStartY($y1);
  88. $this->setEndX($x2);
  89. $this->setEndY($y2);
  90. $this->setThickness($thickness);
  91. $this->setColor($color);
  92. $this->setFill($fill);
  93. $this->setStyle($style);
  94. }
  95. /**
  96. * Sets the start X coordinate
  97. *
  98. * @param integer
  99. * @return boolean
  100. */
  101. public function setStartX($x)
  102. {
  103. if (is_numeric($x))
  104. {
  105. $this->x1 = (int)$x;
  106. return true;
  107. }
  108. return false;
  109. }
  110. /**
  111. * Gets the start X coordinate
  112. *
  113. * @return integer
  114. */
  115. public function getStartX()
  116. {
  117. return $this->x1;
  118. }
  119. /**
  120. * Sets the start Y coordinate
  121. *
  122. * @param integer
  123. * @return boolean
  124. */
  125. public function setStartY($y)
  126. {
  127. if (is_numeric($y))
  128. {
  129. $this->y1 = (int)$y;
  130. return true;
  131. }
  132. return false;
  133. }
  134. /**
  135. * Gets the Y coordinate
  136. *
  137. * @return integer
  138. */
  139. public function getStartY()
  140. {
  141. return $this->y1;
  142. }
  143. /**
  144. * Sets the end X coordinate
  145. *
  146. * @param integer
  147. * @return boolean
  148. */
  149. public function setEndX($x)
  150. {
  151. if (is_numeric($x))
  152. {
  153. $this->x2 = (int)$x;
  154. return true;
  155. }
  156. return false;
  157. }
  158. /**
  159. * Gets the end X coordinate
  160. *
  161. * @return integer
  162. */
  163. public function getEndX()
  164. {
  165. return $this->x2;
  166. }
  167. /**
  168. * Sets the end Y coordinate
  169. *
  170. * @param integer
  171. * @return boolean
  172. */
  173. public function setEndY($y)
  174. {
  175. if (is_numeric($y))
  176. {
  177. $this->y2 = (int)$y;
  178. return true;
  179. }
  180. return false;
  181. }
  182. /**
  183. * Gets the end Y coordinate
  184. *
  185. * @return integer
  186. */
  187. public function getEndY()
  188. {
  189. return $this->y2;
  190. }
  191. /**
  192. * Sets the thickness
  193. *
  194. * @param integer
  195. * @return boolean
  196. */
  197. public function setThickness($thickness)
  198. {
  199. if (is_numeric($thickness))
  200. {
  201. $this->thickness = (int)$thickness;
  202. return true;
  203. }
  204. return false;
  205. }
  206. /**
  207. * Gets the thickness
  208. *
  209. * @return integer
  210. */
  211. public function getThickness()
  212. {
  213. return $this->thickness;
  214. }
  215. /**
  216. * Sets the color
  217. *
  218. * @param string
  219. * @return boolean
  220. */
  221. public function setColor($color)
  222. {
  223. if (preg_match('/#[\d\w]{6}/',$color))
  224. {
  225. $this->color = $color;
  226. return true;
  227. }
  228. return false;
  229. }
  230. /**
  231. * Gets the color
  232. *
  233. * @return integer
  234. */
  235. public function getColor()
  236. {
  237. return $this->color;
  238. }
  239. /**
  240. * Sets the fill
  241. *
  242. * @param mixed
  243. * @return boolean
  244. */
  245. public function setFill($fill)
  246. {
  247. if (preg_match('/#[\d\w]{6}/',$fill) || (is_object($fill) && class_name($fill) === 'sfImage'))
  248. {
  249. $this->fill = $fill;
  250. return true;
  251. }
  252. return false;
  253. }
  254. /**
  255. * Gets the fill
  256. *
  257. * @return mixed
  258. */
  259. public function getFill()
  260. {
  261. return $this->fill;
  262. }
  263. /**
  264. * Sets the style
  265. *
  266. * @param integer
  267. * @return boolean
  268. */
  269. public function setStyle($style)
  270. {
  271. if (is_numeric($style))
  272. {
  273. $this->style = (int)$style;
  274. $this->color = IMG_COLOR_STYLED;
  275. return true;
  276. }
  277. return false;
  278. }
  279. /**
  280. * Gets the style
  281. *
  282. * @return integer
  283. */
  284. public function getStyle()
  285. {
  286. return $this->style;
  287. }
  288. /**
  289. * Apply the transform to the sfImage object.
  290. *
  291. * @param sfImage
  292. * @return sfImage
  293. */
  294. protected function transform(sfImage $image)
  295. {
  296. $resource = $image->getAdapter()->getHolder();
  297. if (!is_null($this->style))
  298. {
  299. imagesetstyle($resource,$this->style);
  300. }
  301. imagesetthickness($resource, $this->thickness);
  302. if (!is_null($this->fill))
  303. {
  304. if (!is_object($this->fill))
  305. {
  306. imagefilledrectangle($resource, $this->x1, $this->y1, $this->x2, $this->y2, $image->getAdapter()->getColorByHex($resource, $this->fill));
  307. }
  308. if ($this->getColor() !== "" && $this->fill !== $this->getColor())
  309. {
  310. imagerectangle($resource, $this->x1, $this->y1, $this->x2, $this->y2, $image->getAdapter()->getColorByHex($resource, $this->getColor()));
  311. }
  312. if (is_object($this->fill))
  313. {
  314. $image->fill($this->x1 + $this->thickness, $this->y1 + $this->thickness, $this->fill);
  315. }
  316. }
  317. else
  318. {
  319. imagerectangle($resource, $this->x1, $this->y1, $this->x2, $this->y2,$image->getAdapter()->getColorByHex($resource, $this->getColor()));
  320. }
  321. return $image;
  322. }
  323. }