/lib/transforms/GD/sfImageArcGD.class.php

https://github.com/AMSGWeblinks/sfImageTransformPlugin · PHP · 424 lines · 178 code · 56 blank · 190 comment · 16 complexity · 57778decd65cb15fd36a60c6be601612 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. * sfImageArcGD class.
  12. *
  13. * Draws an arc.
  14. *
  15. * Draws an arc on an GD image.
  16. *
  17. * @package sfImageTransform
  18. * @subpackage transforms
  19. * @author Stuart Lowes <stuart.lowes@gmail.com>
  20. * @version SVN: $Id$
  21. */
  22. class sfImageArcGD extends sfImageTransformAbstract
  23. {
  24. /**
  25. * X-coordinate of the center.
  26. * @var integer
  27. */
  28. protected $x = 0;
  29. /**
  30. * Y-coordinate of the center.
  31. * @var integer
  32. */
  33. protected $y = 0;
  34. /**
  35. * The arc width
  36. * @var integer
  37. */
  38. protected $width = 0;
  39. /**
  40. * The arc height
  41. * @var integer
  42. */
  43. protected $height = 0;
  44. /**
  45. * Line thickness
  46. * @var integer
  47. */
  48. protected $thickness = 0;
  49. /**
  50. * The arc start angle, in degrees.
  51. * @var integer
  52. */
  53. protected $start_angle = 0;
  54. /**
  55. * The arc end angle, in degrees.
  56. * @var integer
  57. */
  58. protected $end_angle = 90;
  59. /**
  60. * Line color.
  61. * @var string hex
  62. */
  63. protected $color = '#000000';
  64. /**
  65. * Fill.
  66. * @var string/sfImage hex color or sfImage
  67. */
  68. protected $fill = null;
  69. /**
  70. * Line style.
  71. * @var integer
  72. */
  73. protected $style = null;
  74. /**
  75. * Construct an sfImageArc object.
  76. *
  77. * @param integer $x x coordinate
  78. * @param integer $y y coordinate
  79. * @param integer $width width of arc
  80. * @param integer $height height of arc
  81. * @param integer $start_angle angle in degrees
  82. * @param integer $end_angle angle in degrees
  83. * @param integer $thickness line thickness
  84. * @param string $color hex color of line
  85. * @param string/object $fill string color or fill object
  86. * @param integer $style fill style, only applicable if using a fill object
  87. */
  88. public function __construct($x, $y, $width, $height, $start_angle, $end_angle, $thickness = 1, $color = '#000000', $fill=null,$style = null )
  89. {
  90. $this->setX($x);
  91. $this->setY($y);
  92. $this->setWidth($width);
  93. $this->setHeight($height);
  94. $this->setStartAngle($start_angle);
  95. $this->setEndAngle($end_angle);
  96. $this->setThickness($thickness);
  97. $this->setColor($color);
  98. $this->setFill($fill);
  99. $this->setStyle($style);
  100. }
  101. /**
  102. * Sets the X coordinate
  103. *
  104. * @param integer
  105. * @return boolean
  106. */
  107. public function setX($x)
  108. {
  109. if (is_numeric($x))
  110. {
  111. $this->x = (int)$x;
  112. return true;
  113. }
  114. return false;
  115. }
  116. /**
  117. * Gets the X coordinate
  118. *
  119. * @return integer
  120. */
  121. public function getX()
  122. {
  123. return $this->x;
  124. }
  125. /**
  126. * Sets the Y coordinate
  127. *
  128. * @param integer
  129. * @return boolean
  130. */
  131. public function setY($y)
  132. {
  133. if (is_numeric($y))
  134. {
  135. $this->y = (int)$y;
  136. return true;
  137. }
  138. return false;
  139. }
  140. /**
  141. * Gets the Y coordinate
  142. *
  143. * @return integer
  144. */
  145. public function getY()
  146. {
  147. return $this->y;
  148. }
  149. /**
  150. * Sets the width
  151. *
  152. * @param integer
  153. * @return boolean
  154. */
  155. public function setWidth($width)
  156. {
  157. if (is_numeric($width))
  158. {
  159. $this->width = (int)$width;
  160. return true;
  161. }
  162. return false;
  163. }
  164. /**
  165. * Gets the Width
  166. *
  167. * @return integer
  168. */
  169. public function getWidth()
  170. {
  171. return $this->width;
  172. }
  173. /**
  174. * Sets the height
  175. *
  176. * @param integer
  177. * @return boolean
  178. */
  179. public function setHeight($height)
  180. {
  181. if (is_numeric($height))
  182. {
  183. $this->height = (int)$height;
  184. return true;
  185. }
  186. return false;
  187. }
  188. /**
  189. * Gets the height
  190. *
  191. * @return integer
  192. */
  193. public function getHeight()
  194. {
  195. return $this->height;
  196. }
  197. /**
  198. * Sets the start angel
  199. *
  200. * @param integer
  201. * @return boolean
  202. */
  203. public function setStartAngle($start_angle)
  204. {
  205. if (is_numeric($start_angle))
  206. {
  207. $this->start_angle = (int)$start_angle;
  208. return true;
  209. }
  210. return false;
  211. }
  212. /**
  213. * Gets the start angel
  214. *
  215. * @return integer
  216. */
  217. public function getStartAngle()
  218. {
  219. return $this->start_angle;
  220. }
  221. /**
  222. * Sets the end angel
  223. *
  224. * @param integer
  225. * @return boolean
  226. */
  227. public function setEndAngle($end_angle)
  228. {
  229. if (is_numeric($end_angle))
  230. {
  231. $this->end_angle = (int)$end_angle;
  232. return true;
  233. }
  234. return false;
  235. }
  236. /**
  237. * Gets the end angel
  238. *
  239. * @return integer
  240. */
  241. public function getEndAngle()
  242. {
  243. return $this->end_angle;
  244. }
  245. /**
  246. * Sets the thickness
  247. *
  248. * @param integer
  249. * @return boolean
  250. */
  251. public function setThickness($thickness)
  252. {
  253. if (is_numeric($thickness))
  254. {
  255. $this->thickness = (int)$thickness;
  256. return true;
  257. }
  258. return false;
  259. }
  260. /**
  261. * Gets the thickness
  262. *
  263. * @return integer
  264. */
  265. public function getThickness()
  266. {
  267. return $this->thickness;
  268. }
  269. /**
  270. * Sets the color
  271. *
  272. * @param string
  273. * @return boolean
  274. */
  275. public function setColor($color)
  276. {
  277. if (preg_match('/#[\d\w]{6}/',$color))
  278. {
  279. $this->color = $color;
  280. return true;
  281. }
  282. return false;
  283. }
  284. /**
  285. * Gets the color
  286. *
  287. * @return integer
  288. */
  289. public function getColor()
  290. {
  291. return $this->color;
  292. }
  293. /**
  294. * Sets the fill
  295. *
  296. * @param mixed
  297. * @return boolean
  298. */
  299. public function setFill($fill)
  300. {
  301. if (preg_match('/#[\d\w]{6}/',$fill) || (is_object($fill) && class_name($fill) === 'sfImage'))
  302. {
  303. $this->fill = $fill;
  304. return true;
  305. }
  306. return false;
  307. }
  308. /**
  309. * Gets the fill
  310. *
  311. * @return mixed
  312. */
  313. public function getFill()
  314. {
  315. return $this->fill;
  316. }
  317. /**
  318. * Sets the style
  319. *
  320. * @param integer
  321. * @return boolean
  322. */
  323. public function setStyle($style)
  324. {
  325. if (is_numeric($style))
  326. {
  327. $this->style = (int)$style;
  328. return true;
  329. }
  330. return false;
  331. }
  332. /**
  333. * Gets the style
  334. *
  335. * @return integer
  336. */
  337. public function getStyle()
  338. {
  339. return $this->style;
  340. }
  341. /**
  342. * Apply the transform to the sfImage object.
  343. *
  344. * @param object
  345. * @return object
  346. */
  347. protected function transform(sfImage $image)
  348. {
  349. $resource = $image->getAdapter()->getHolder();
  350. imagesetthickness($resource, $this->thickness);
  351. if (!is_null($this->fill))
  352. {
  353. if (!is_object($this->fill))
  354. {
  355. imagefilledarc($resource, $this->x, $this->y, $this->width, $this->height, $this->start_angle, $this->end_angle, $image->getAdapter()->getColorByHex($resource, $this->fill), $this->style);
  356. }
  357. if ($this->color !== "" && $this->fill !== $this->color)
  358. {
  359. imagearc($resource, $this->x, $this->y, $this->width, $this->height, $this->start_angle, $this->end_angle, $image->getAdapter()->getColorByHex($resource, $this->color));
  360. }
  361. }
  362. else
  363. {
  364. imagearc($resource, $this->x, $this->y, $this->width, $this->height, $this->start_angle, $this->end_angle, $image->getAdapter()->getColorByHex($resource, $this->color));
  365. }
  366. return $image;
  367. }
  368. }