Project

General

Profile

1 1035 Ruebenwurz
<?php
2
3
// $Id$
4
5
/*
6
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2009, Ryan Djurovich
9
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
24
*/
25
26
	/**
27
	 * Image Resizer.
28
	 * @author : Harish Chauhan
29
	 * @copyright : Freeware
30
	 * About :This PHP script will resize the given image and can show on the fly or save as image file.
31
	 *
32
	 */
33
34
35
	define("HAR_AUTO_NAME",1);
36
	Class RESIZEIMAGE
37
	{
38
		var $imgFile="";
39
		var $imgWidth=0;
40
		var $imgHeight=0;
41
		var $imgType="";
42
		var $imgAttr="";
43
		var $type=NULL;
44
		var $_img=NULL;
45
		var $_error="";
46
47
		/**
48
		 * Constructor
49
		 *
50
		 * @param [String $imgFile] Image File Name
51
		 * @return RESIZEIMAGE (Class Object)
52
		 */
53
54
		function RESIZEIMAGE($imgFile="")
55
		{
56
			if (!function_exists("imagecreate"))
57
			{
58
				$this->_error="Error: GD Library is not available.";
59
				return false;
60
			}
61
62
			$this->type=Array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG', 4 => 'SWF', 5 => 'PSD', 6 => 'BMP', 7 => 'TIFF', 8 => 'TIFF', 9 => 'JPC', 10 => 'JP2', 11 => 'JPX', 12 => 'JB2', 13 => 'SWC', 14 => 'IFF', 15 => 'WBMP', 16 => 'XBM');
63
			if(!empty($imgFile))
64
				$this->setImage($imgFile);
65
		}
66
		/**
67
		 * Error occured while resizing the image.
68
		 *
69
		 * @return String
70
		 */
71
		function error()
72
		{
73
			return $this->_error;
74
		}
75
76
		/**
77
		 * Set image file name
78
		 *
79
		 * @param String $imgFile
80
		 * @return void
81
		 */
82
		function setImage($imgFile)
83
		{
84
			$this->imgFile=$imgFile;
85
			return $this->_createImage();
86
		}
87
		/**
88
		 *
89
		 * @return void
90
		 */
91
		function close()
92
		{
93
			return @imagedestroy($this->_img);
94
		}
95
		/**
96
		 * Resize a image to given width and height and keep it's current width and height ratio
97
		 *
98
		 * @param Number $imgwidth
99
		 * @param Numnber $imgheight
100
		 * @param String $newfile
101
		 */
102
		function resize_limitwh($imgwidth,$imgheight,$newfile=NULL)
103
		{
104
			$image_per = 100;
105
			list($width, $height, $type, $attr) = @getimagesize($this->imgFile);
106
			if($width > $imgwidth && $imgwidth > 0)
107
				$image_per = (double)(($imgwidth * 100) / $width);
108
109
			if(floor(($height * $image_per)/100)>$imgheight && $imgheight > 0)
110
				$image_per = (double)(($imgheight * 100) / $height);
111
112
			$this->resize_percentage($image_per,$newfile);
113
114
		}
115
		/**
116
		 * Resize an image to given percentage.
117
		 *
118
		 * @param Number $percent
119
		 * @param String $newfile
120
		 * @return Boolean
121
		 */
122
		function resize_percentage($percent=100,$newfile=NULL)
123
		{
124
			$newWidth=($this->imgWidth*$percent)/100;
125
			$newHeight=($this->imgHeight*$percent)/100;
126
			return $this->resize($newWidth,$newHeight,$newfile);
127
		}
128
		/**
129
		 * Resize an image to given X and Y percentage.
130
		 *
131
		 * @param Number $xpercent
132
		 * @param Number $ypercent
133
		 * @param String $newfile
134
		 * @return Boolean
135
		 */
136
		function resize_xypercentage($xpercent=100,$ypercent=100,$newfile=NULL)
137
		{
138
			$newWidth=($this->imgWidth*$xpercent)/100;
139
			$newHeight=($this->imgHeight*$ypercent)/100;
140
			return $this->resize($newWidth,$newHeight,$newfile);
141
		}
142
143
		/**
144
		 * Resize an image to given width and height
145
		 *
146
		 * @param Number $width
147
		 * @param Number $height
148
		 * @param String $newfile
149
		 * @return Boolean
150
		 */
151
		function resize($width,$height,$newfile=NULL)
152
		{
153
			if(empty($this->imgFile))
154
			{
155
				$this->_error="File name is not initialised.";
156
				return false;
157
			}
158
			if($this->imgWidth<=0 || $this->imgHeight<=0)
159
			{
160
				$this->_error="Could not resize given image";
161
				return false;
162
			}
163
			if($width<=0)
164
				$width=$this->imgWidth;
165
			if($height<=0)
166
				$height=$this->imgHeight;
167
168
			return $this->_resize($width,$height,$newfile);
169
		}
170
171
		/**
172
		 * Get the image attributes
173
		 * @access Private
174
		 *
175
		 */
176
		function _getImageInfo()
177
		{
178
			@list($this->imgWidth,$this->imgHeight,$type,$this->imgAttr)=@getimagesize($this->imgFile);
179
			$this->imgType=$this->type[$type];
180
		}
181
182
		/**
183
		 * Create the image resource
184
		 * @access Private
185
		 * @return Boolean
186
		 */
187
		function _createImage()
188
		{
189
			$this->_getImageInfo($this->imgFile);
190
			if($this->imgType=='GIF')
191
			{
192
				$this->_img=@imagecreatefromgif($this->imgFile);
193
			}
194
			elseif($this->imgType=='JPG')
195
			{
196
				$this->_img=@imagecreatefromjpeg($this->imgFile);
197
			}
198
			elseif($this->imgType=='PNG')
199
			{
200
				$this->_img=@imagecreatefrompng($this->imgFile);
201
			}
202
			if(!$this->_img || !@is_resource($this->_img))
203
			{
204
				$this->_error="Error loading ".$this->imgFile;
205
				return false;
206
			}
207
			return true;
208
		}
209
210
		/**
211
		 * Function is used to resize the image
212
		 *
213
		 * @access Private
214
		 * @param Number $width
215
		 * @param Number $height
216
		 * @param String $newfile
217
		 * @return Boolean
218
		 */
219
		function _resize($width,$height,$newfile=NULL)
220
		{
221
			if (!function_exists("imagecreate"))
222
			{
223
				$this->_error="Error: GD Library is not available.";
224
				return false;
225
			}
226
227
			$newimg=@imagecreatetruecolor($width,$height);
228
			//imagecolortransparent( $newimg, imagecolorat( $newimg, 0, 0 ) );
229
230
			if($this->imgType=='GIF' || $this->imgType=='PNG')
231
			{
232
				/** Code to keep transparency of image **/
233
				$colorcount = imagecolorstotal($this->_img);
234
				if ($colorcount == 0) $colorcount = 256;
235
				imagetruecolortopalette($newimg,true,$colorcount);
236
				imagepalettecopy($newimg,$this->_img);
237
				$transparentcolor = imagecolortransparent($this->_img);
238
				imagefill($newimg,0,0,$transparentcolor);
239
				imagecolortransparent($newimg,$transparentcolor);
240
			}
241
242
			@imagecopyresampled ( $newimg, $this->_img, 0,0,0,0, $width, $height, $this->imgWidth,$this->imgHeight);
243
244
245
246
			if($newfile===HAR_AUTO_NAME)
247
			{
248
				if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches))
249
			   		$newfile=@substr_replace($this->imgFile,"_har",-@strlen($matches[0]),0);
250
			}
251
			elseif(!empty($newfile))
252
			{
253
				if(!@preg_match("/\..*+$/",@basename($newfile)))
254
				{
255
					if(@preg_match("/\..*+$/",@basename($this->imgFile),$matches))
256
					   $newfile=$newfile.$matches[0];
257
				}
258
			}
259
260
			if($this->imgType=='GIF')
261
			{
262
				if(!empty($newfile))
263
					@imagegif($newimg,$newfile);
264
				else
265
				{
266
					@header("Content-type: image/gif");
267
					@imagegif($newimg);
268
				}
269
			}
270
			elseif($this->imgType=='JPG')
271
			{
272
				if(!empty($newfile))
273
					@imagejpeg($newimg,$newfile,85);
274
				else
275
				{
276
					@header("Content-type: image/jpeg");
277
					@imagejpeg($newimg);
278
				}
279
			}
280
			elseif($this->imgType=='PNG')
281
			{
282
				if(!empty($newfile))
283
					@imagepng($newimg,$newfile);
284
				else
285
				{
286
					@header("Content-type: image/png");
287
					@imagepng($newimg);
288
				}
289
			}
290
			@imagedestroy($newimg);
291
		}
292
	}
293
?>