Graphics.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class Graphics
{
	private $im;
	private $dim;
	private $color;
 
	function __construct($width, $height)
	{
		$this->dim = new Dimension($width, $height);
		$this->color = new Color(0, 0, 0);
		$c = new Color(255, 255, 255);
 
		$this->im = imagecreatetruecolor($width, $height);
 
		imagefill($this->im, 0, 0, $c->getGDColor($this->im));
	}
 
	function setColor($color)
	{
		if ($color instanceof Color)
			$this->color = $color;
		else
			$this->color = new Color(0, 0, 0);
	}
 
	function drawRect($x, $y, $width, $height)
	{
		$c = $this->color;
		imagerectangle($this->im, $x, $y, $x+$width, $y+$height, $c->getGDColor($this->im)); 
	}
 
	function fillRect($x, $y, $width, $height)
	{
		$c = $this->color;
		imagefilledrectangle($this->im, $x, $y, $x+$width, $y+$height, $c->getGDColor($this->im));
	}
 
	function drawOval($x, $y, $width, $height)
	{
		$c = $this->color;
		imageellipse($this->im, $x+$width/2, $y+$height/2, $width, $height, $c->getGDColor($this->im));
	}
 
	function fillOval($x, $y, $width, $height)
	{
		$c = $this->color;
		imagefilledellipse($this->im, $x+$width/2, $y+$height/2, $width, $height, $c->getGDColor($this->im));
	}
 
	function drawLine($x1, $y1, $x2, $y2)
	{
		$c = $this->color;
		imageline($this->im, $x1, $y1, $x2, $y2, $c->getGDColor($this->im));
	}
 
	function renderImage($imagename = "temp", $ext = "jpg")
	{
		list($imagename, $e) = explode('.', $imagename);
		$ext = strtolower($ext);
		$filename = $imagename.".".$ext;
		if ($ext == "jpg" || $ext == "jpeg")
			imagejpeg($this->im, $filename, 100);
		else if ($ext == "png")
			imagepng($this->im, $filename, 5);
	}
 
	function getWidth()
	{
		$d = $this->dim;
		return $d->getWidth();
	}
 
	function getHeight()
	{
		$d = $this->dim;
		return $d->getHeight();
	}
 
	function __destruct()
	{
		imagedestroy($this->im);
		unset($this->dim);
		unset($this->color);
	}
}