image.class.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace Juju;
  3. class Image implements JsonSerializable{
  4. private $img;
  5. private $actions = array();
  6. private $colours = array();
  7. protected $width;
  8. protected $height;
  9. protected $background;
  10. protected $type;
  11. public function __construct($width,$height,$background='white',$type='png'){
  12. $this->img = imagecreatetruecolor($width,$height);
  13. if(!$this->img){
  14. throw new Exception('Could not create image');
  15. }
  16. if(function_exists('imageantialias')){
  17. imageantialias($this->img,true);
  18. }
  19. $this->width = $width;
  20. $this->height = $height;
  21. $this->colour('black',0,0,0);
  22. $this->colour('white',255,255,255);
  23. $this->colour('red',255,0,0);
  24. $this->colour('green',0,255,0);
  25. $this->colour('blue',0,0,255);
  26. $this->background = $background;
  27. $this->type = $type;
  28. }
  29. public function __get($name){
  30. switch($name){
  31. case 'width':
  32. return $this->width;
  33. break;
  34. case 'height':
  35. return $this->height;
  36. break;
  37. case 'type':
  38. return $this->type;
  39. break;
  40. case 'background':
  41. return $this->background;
  42. break;
  43. case 'actions':
  44. return $this->actions;
  45. break;
  46. }
  47. }
  48. public function __clone(){
  49. $this->img = clone $this->img;
  50. }
  51. public function __destruct(){
  52. imagedestroy($this->img);
  53. }
  54. public function jsonSerialize(){
  55. ob_start();
  56. $this();
  57. $data = ob_get_contents();
  58. ob_end_clean();
  59. return array(
  60. 'width'=>$this->width,
  61. 'height'=>$this->height,
  62. 'type'=>$this->type,
  63. 'data'=>'data:image/'.$this->type.';base64,'.base64_encode($data)
  64. );
  65. }
  66. public function __toString(){
  67. return '[Image Object ('.$this->width.','.$this->height.')]';
  68. }
  69. public function __invoke(){
  70. $this->render();
  71. $args = array_merge(array($this->img),func_get_args());
  72. return call_user_func_array('image'.$this->type,$args);
  73. }
  74. public function render(){
  75. imagefill($this->img,0,0,$this->colour($this->background));
  76. for($k=0;$k<count($this->actions);$k++){
  77. $a = $this->actions[$k];
  78. switch($a[0]){
  79. case 'text':
  80. imagestring(
  81. $this->img,
  82. $a[5],
  83. $a[2],
  84. $a[3],
  85. $a[1],
  86. $this->color($a[4])
  87. );
  88. break;
  89. case 'vertical_text':
  90. imagestringup(
  91. $this->img,
  92. $a[5],
  93. $a[2],
  94. $a[3],
  95. $a[1],
  96. $this->color($a[4])
  97. );
  98. break;
  99. case 'line':
  100. imageline(
  101. $this->img,
  102. $a[1],$a[2],
  103. $a[3], $a[4],
  104. $this->color($a[5])
  105. );
  106. break;
  107. case 'rectangle':
  108. imagerectangle(
  109. $this->img,
  110. $a[1],$a[2],
  111. $a[3],$a[4],
  112. $this->color($a[5])
  113. );
  114. break;
  115. case 'filled_rectangle':
  116. imagefilledrectangle(
  117. $this->img,
  118. $a[1],$a[2],
  119. $a[3],$a[4],
  120. $this->color($a[5])
  121. );
  122. break;
  123. case 'polygon':
  124. imagepolygon(
  125. $this->img,
  126. $a[1],
  127. count($a[1])/2,
  128. $this->colour($a[2])
  129. );
  130. break;
  131. case 'filled_polygon':
  132. imagefilledpolygon(
  133. $this->img,
  134. $a[1],
  135. count($a[1])/2,
  136. $this->colour($a[2])
  137. );
  138. break;
  139. case 'arc':
  140. imagearc(
  141. $this->img,
  142. $a[1],$a[2],
  143. $a[3],$a[4],
  144. $a[5],$a[6],
  145. $this->colour($a[7])
  146. );
  147. break;
  148. case 'filled_arc':
  149. imagefilledarc(
  150. $this->img,
  151. $a[1],$a[2],
  152. $a[3],$a[4],
  153. $a[5],$a[6],
  154. $this->colour($a[7]),
  155. $a[8]
  156. );
  157. break;
  158. case 'ellipse':
  159. imageellipse(
  160. $this->img,
  161. $a[1],$a[2],
  162. $a[3],$a[4],
  163. $this->colour($a[5])
  164. );
  165. break;
  166. case 'filled_ellipse':
  167. imagefilledellipse(
  168. $this->img,
  169. $a[1],$a[2],
  170. $a[3],$a[4],
  171. $this->colour($a[5])
  172. );
  173. break;
  174. case 'flood_fill':
  175. imagefilltoborder(
  176. $this->img,
  177. $a[1],$a[2],
  178. $this->colour($a[3]),
  179. $this->colour($a[4])
  180. );
  181. break;
  182. case 'filter':
  183. $args = array_merge(array($this->img),$a[1]);
  184. call_user_func_array('imagefilter',$args);
  185. break;
  186. }
  187. }
  188. return $this;
  189. }
  190. public function color(){
  191. return call_user_func_array(array($this, 'colour'),func_get_args());
  192. }
  193. public function colour(){
  194. if(func_num_args() > 1){
  195. if(!isset($this->colours[func_get_arg(0)])){
  196. $this->colours[func_get_arg(0)] = imagecolorallocate($this->img,func_get_arg(1),func_get_arg(2),func_get_arg(3));
  197. }
  198. }
  199. return $this->colours[func_get_arg(0)];
  200. }
  201. public function text($text,$x,$y,$colour='white',$size=1){
  202. array_push($this->actions,array(
  203. 'text',
  204. $text,
  205. $x,$y,
  206. $colour,
  207. $size
  208. ));
  209. return $this;
  210. }
  211. public function vertical_text($text,$x,$y,$colour='white',$size=1){
  212. array_push($this->actions,array(
  213. 'vertical_text',
  214. $text,
  215. $x,$y,
  216. $colour,
  217. $size
  218. ));
  219. return $this;
  220. }
  221. public function line($x1,$y1,$x2,$y2,$colour){
  222. array_push($this->actions,array(
  223. 'line',
  224. $x1,$y1,
  225. $x2,$y2,
  226. $colour
  227. ));
  228. return $this;
  229. }
  230. public function polygon($points,$colour){
  231. array_push($this->actions,array(
  232. 'polygon',
  233. $points,
  234. $colour
  235. ));
  236. return $this;
  237. }
  238. public function filled_polygon($points,$colour){
  239. array_push($this->actions,array(
  240. 'filled_polygon',
  241. $points,
  242. $colour
  243. ));
  244. return $this;
  245. }
  246. public function rectangle($x1,$y1,$x2,$y2,$colour){
  247. array_push($this->actions,array(
  248. 'rectangle',
  249. $x1,$y1,
  250. $x2,$y2,
  251. $colour
  252. ));
  253. return $this;
  254. }
  255. public function filled_rectangle($x1,$y1,$x2,$y2,$colour){
  256. array_push($this->actions,array(
  257. 'filled_rectangle',
  258. $x1,$y1,
  259. $x2,$y2,
  260. $colour
  261. ));
  262. return $this;
  263. }
  264. public function arc($cx,$cy,$w,$h,$sa,$ea,$colour){
  265. array_push($this->actions,array(
  266. 'arc',
  267. $cx,$cy,
  268. $w,$h,
  269. $sa,$ea,
  270. $colour
  271. ));
  272. return $this;
  273. }
  274. public function filled_arc($cx,$cy,$w,$h,$sa,$ea,$colour,$style=IMG_ARC_PIE){
  275. array_push($this->actions,array(
  276. 'filled_arc',
  277. $cx,$cy,
  278. $w,$h,
  279. $sa,$ea,
  280. $colour,
  281. $style
  282. ));
  283. return $this;
  284. }
  285. public function ellipse($cx,$cy,$w,$h,$colour){
  286. array_push($this->actions,array(
  287. 'ellipse',
  288. $cx,$cy,
  289. $w,$h,
  290. $colour
  291. ));
  292. return $this;
  293. }
  294. public function circle($cx,$cy,$w,$colour){
  295. return $this->ellipse($cx,$cy,$w,$w,$colour);
  296. }
  297. public function filled_ellipse($cx,$cy,$w,$h,$colour){
  298. array_push($this->actions,array(
  299. 'filled_ellipse',
  300. $cx,$cy,
  301. $w,$h,
  302. $colour
  303. ));
  304. return $this;
  305. }
  306. public function filled_circle($cx,$cy,$w,$colour){
  307. return $this->filled_ellipse($cx,$cy,$w,$w,$colour);
  308. }
  309. public function flood_fill($x,$y,$border_colour,$colour){
  310. array_push($this->actions,array(
  311. 'flood_fill',
  312. $x,$y,
  313. $border_colour,
  314. $colour
  315. ));
  316. return $this;
  317. }
  318. public function filter(){
  319. array_push($this->actions,array(
  320. 'filter',
  321. func_get_args()
  322. ));
  323. return $this;
  324. }
  325. public function copy($dest){
  326. $this->render();
  327. imagepalettecopy($dest,$this->img);
  328. return $this;
  329. }
  330. }
  331. ?>