1
0

image.class.php 7.0 KB

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