ImageResize.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
class ImageResize
{
	private $dirpath = "./imgthumb/";
	private $quality = 85;
	private $imgurl;
	private $imgname;
	private $resurl;
	private $side = -1;
	private $im;
	private $w = 100, $h = 100;
	private $dim;
 
	function __construct($imgurl)
	{
		$this->imgurl = $imgurl;
		$this->dim = new Dimension(100, 100);
 
		list($name, $ext) = explode('.', $this->imgurl);
 
		$ext = strtolower($ext);
 
		if ($ext == 'jpg' || $exp == 'jpeg')
			$this->im = imagecreatefromjpeg($this->imgurl);
		else if ($ext == 'png')
			$this->im = imagecreatefrompng($this->imgurl);
		else if ($ext == 'gif')
			$this->im = imagecreatefromgif($this->imgurl);
		else
			$this->im = 0;
 
		if ($this->im != 0)
		{
			$tw = imagesx($this->im);
			$th = imagesy($this->im);
			if ($tw == $th) $this->side = -1;
			else if ($tw > $th) $this->side = 0;
			else $this->side = 1;
		}
	}
 
	function setSize($w, $h)
	{
		$dim = new Dimension($w, $h);
	}
 
	function getSize()
	{
		$d = $this->dim;
		return "[".$d->getWidth().", ".$d->getHeight()."]";
	}
 
	function setName($name)
	{
		$this->imgname = $name;
	}
 
	function getImageUrl()
	{
		return $this->resurl;
	}
 
	function setQuality($quality)
	{
		if ($quality > 0 && $quality <= 100)
			$this->quality = $quality;
		else
			print "Quality value out of bounds. Set a value between: 0 and 100";
	}
 
	function getQuality()
	{
		return $this->quality;
	}
 
	// TODO dare la possibilità al metodo di creare la cartella qualora fosse necessario
	function setImagePath($path)
	{
		if (isset($path) && $path != "")
			$this->dirpath = $path;
		else
			print "Path value must be not empty";
	}
 
	function getImagePath()
	{
		return $this->dirpath;
	}
 
	function checkProportions()
	{
		$wres = 0;
		$hres = 0;
 
		$d = $this->dim;
 
		# ridimensionamento orizzontale
		if ($this->side == 0)
		{
			$wres = $d->getWidth();
			$hres = intval(($wres / imagesx($this->im)) * imagesy($this->im));
		}
		# ridimensionamento verticale
		else if ($this->side == 1)
		{
			$hres = $d->getHeight();
			$wres = intval(($hres / imagesy($this->im)) * imagesx($this->im));
		}
		# ridimensionamento quadrato
		else
		{
			$wres = $d->getWidth();
			$hres = $d->getHeight();
		}
 
		$d = new Dimension($wres, $hres);		
 
		return $d;
	}
 
	function resize()
	{	
		$d = $this->checkProportions();
 
		# immagine di destinazione
		$resimg = imagecreatetruecolor($d->getWidth(), $d->getHeight());
 
		imagecopyresampled(
			$resimg, 		# immagine destinazione
			$this->im,		# immagine sorgente
			0, 0,			# angolo iniziale dell'immagine destinazione
			0, 0,			# angolo iniziale dell'immagine sorgente
			$d->getWidth(), $d->getHeight(),	# larghezza e altezza immagine ridimensionata
			imagesx($this->im), imagesy($this->im)	# dlarghezza e altezza immagine sorgente
			);
 
		$this->imgname = ($this->imgname=="") ? md5($this->imgurl).".jpg" : $this->imgname;
 
		$this->resurl = $this->dirpath . $this->imgname;
 
		imagejpeg($resimg, $this->resurl, $this->quality);
	}
 
	function __destruct()
	{
		imagedestroy($this->im);
	}
}