aboutsummaryrefslogtreecommitdiff
path: root/h-source/Library/Image/Gd/Thumbnail.php
blob: bb698918caeedccd2b47116bae3cbfe4c7da29b6 (plain) (blame)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php

// EasyGiant is a PHP framework for creating and managing dynamic content
//
// Copyright (C) 2009 - 2014  Antonio Gallo (info@laboratoriolibero.com)
// See COPYRIGHT.txt and LICENSE.txt.
//
// This file is part of EasyGiant
//
// EasyGiant is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// EasyGiant is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with EasyGiant.  If not, see <http://www.gnu.org/licenses/>.

if (!defined('EG')) die('Direct access not allowed!');

//class to create a thumbnail
class Image_Gd_Thumbnail
{
	const DS = DIRECTORY_SEPARATOR;
	
	private $params = array(); //parameters of the object
	private $basePath = null; //the path of the folder inside which the images are saved
	
	public function __construct($basePath,$params = null)
	{
		$finalChar = $basePath[strlen($basePath) - 1];
		if (strcmp($finalChar,self::DS) !== 0) $basePath .= self::DS;
		
		$this->basePath = $basePath;
		
		$defaultParams = array(
			'imgWidth'		=>	null,
			'imgHeight'		=>	null,
			'defaultImage'	=>	null,
			'cropImage'		=>	'no',
			'horizAlign'	=>	'left',
			'vertAlign'		=>	'top',
			'resample'		=>	'yes',
			'function'		=>	'none',
			'outputFormat'	=>	'jpeg',
			'backgroundColor' => null, //must be hex color
		);

		//set the $this->params array
		if (is_array($params))
		{
			foreach ($params as $key => $value)
			{
				$defaultParams[$key] = $value;
			}
		}
		$this->params = $defaultParams;
	}
	
	public function getSourceCoordinates($direction, $oldDim, $dim)
	{
		if ($direction === "x")
		{
			switch ($this->params['horizAlign'])
			{
				case 'left':
					$coordSrc = 0;
					break;
				case 'right':
					$coordSrc = floor(($oldDim-$dim));
					break;
				case 'center':
					$coordSrc = floor(($oldDim-$dim)/2);
					break;
				default:
					$coordSrc = $this->params['horizAlign'];
			}
		}
		else
		{
			switch ($this->params['vertAlign'])
			{
				case 'top':
					$coordSrc = 0;
					break;
				case 'bottom':
					$coordSrc = floor(($oldDim-$dim));
					break;
				case 'center':
					$coordSrc = floor(($oldDim-$dim)/2);
					break;
				default:
					$coordSrc = $this->params['vertAlign'];
			}
		}
		
		return $coordSrc;
	}
	
	//create the thumbnail
	//$imageName: the name of the file inside $this->basePath
	//$outputFile: the name of the output file
	public function render($imageFile, $outputFile = null)
	{
		$imagePath = $this->basePath . basename($imageFile);
		
		if (!file_exists($imagePath) and isset($this->params['defaultImage'])) $imagePath = $this->params['defaultImage'];

		$img = null;
		$type = 'jpeg';
		$contentType = 'image/jpeg';
		
		if (file_exists($imagePath))
		{
			$extArray = explode('.', $imagePath);
			$ext = strtolower(end($extArray));

			if (strcmp($ext,'jpg') === 0 or strcmp($ext,'jpeg') === 0) {
				$img = @imagecreatefromjpeg($imagePath);
				$type = 'jpeg';
				$contentType = 'image/jpeg';
			} else if (strcmp($ext,'png') === 0) {
				$img = @imagecreatefrompng($imagePath);
				$type = 'png';
				$contentType = 'image/png';
			} else if (strcmp($ext,'gif') === 0) {
				$img = @imagecreatefromgif($imagePath);
				$type = 'gif';
				$contentType = 'image/gif';
			}
		}
		
		//If an image was successfully loaded, test the image for size
		if ($img)
		{
			//image size
			$width = imagesx($img);
			$height = imagesy($img);

			if (!isset($this->params['imgWidth']))	$this->params['imgWidth'] = $width;
			if (!isset($this->params['imgHeight']))	$this->params['imgHeight'] = $height;
			
			if ($this->params['cropImage'] === 'no')
			{
				$scale = min($this->params['imgWidth']/$width, $this->params['imgHeight']/$height);
			}
			else if ($this->params['cropImage'] === 'yes')
			{
				$scale = max($this->params['imgWidth']/$width, $this->params['imgHeight']/$height);
			}

			$xSrc = 0; //x coordinate of source image
			$ySrc = 0; //y coordinate of source image
			
			$xDst = 0; //x coordinate of destination image
			$yDst = 0; //y coordinate of destination image
			
			if ($this->params['cropImage'] === 'no')
			{
				if ($scale <= 1)
				{
					$newWidth = $backWidth = floor($scale*$width);
					$newHeight = $backHeight = floor($scale*$height);
					
					if ($this->params['backgroundColor'])
					{
						$backWidth = $this->params['imgWidth'];
						$backHeight = $this->params['imgHeight'];
						
						if ($backWidth > $newWidth)
						{
							$xDst = floor(($backWidth-$newWidth)/2);
						}
						else if ($backHeight > $newHeight)
						{
							$yDst = floor(($backHeight-$newHeight)/2);
						}
					}
				}
				else
				{
					$newWidth = $backWidth = $width;
					$newHeight = $backHeight = $height;
					
					if ($this->params['backgroundColor'])
					{
						$backWidth = $this->params['imgWidth'];
						$backHeight = $this->params['imgHeight'];
						
						$xDst = floor(($backWidth-$newWidth)/2);
						$yDst = floor(($backHeight-$newHeight)/2);
					}
				}
			}
			else if ($this->params['cropImage'] === 'yes')
			{
				if ($scale < 1)
				{
					$newWidth = $backWidth = $this->params['imgWidth'];
					$newHeight = $backHeight = $this->params['imgHeight'];
					$oldWidth = $width;
					$oldHeight = $height;
					$width = floor($newWidth/$scale);
					$height = floor($newHeight/$scale);
					
					$xSrc = $this->getSourceCoordinates("x",$oldWidth,$width);
					$ySrc = $this->getSourceCoordinates("y",$oldHeight,$height);
					
				}
				else
				{
					$oldWidth = $width;
					$oldHeight = $height;
					
					if ($width <= $this->params['imgWidth'] and $height <= $this->params['imgHeight'])
					{
						$newWidth = $backWidth = $width;
						$newHeight = $backHeight = $height;
						
						if ($this->params['backgroundColor'])
						{
							$backWidth = $this->params['imgWidth'];
							$backHeight = $this->params['imgHeight'];
							
							$xDst = floor(($backWidth-$newWidth)/2);
							$yDst = floor(($backHeight-$newHeight)/2);
						}
					
					}
					else if ($width <= $this->params['imgWidth'])
					{
						$newWidth = $backWidth = $width;
						$newHeight = $backHeight = $height = $this->params['imgHeight'];
						
						$ySrc = $this->getSourceCoordinates("y",$oldHeight,$height);
						
						if ($this->params['backgroundColor'])
						{
							$backWidth = $this->params['imgWidth'];
							$backHeight = $this->params['imgHeight'];
							
							$xDst = floor(($backWidth-$newWidth)/2);
						}
					
					}
					else if ($height <= $this->params['imgHeight'])
					{
						$newHeight = $backHeight = $height;
						$newWidth = $backWidth = $width = $this->params['imgWidth'];
						
						$xSrc = $this->getSourceCoordinates("x",$oldWidth,$width);
						
						if ($this->params['backgroundColor'])
						{
							$backWidth = $this->params['imgWidth'];
							$backHeight = $this->params['imgHeight'];
							
							$yDst = floor(($backHeight-$newHeight)/2);
						}
						
					}
				}
			}

			//temp image
			$tmpImg = imagecreatetruecolor($backWidth, $backHeight);
			
			//set background color if backgroundColor param is not null (hex value)
			if ($this->params['backgroundColor'])
			{
				if (strcmp($this->params['backgroundColor'],"transparent") !== 0)
				{
					$rgbColor = hex2rgb($this->params['backgroundColor']);
					
					$backgroundC = imagecolorallocate($tmpImg,$rgbColor[0],$rgbColor[1],$rgbColor[2]);
				}
				else
				{
					$backgroundC = imagecolortransparent($tmpImg);
				}
				
				imagefill($tmpImg, 0, 0, $backgroundC);
			}
			
			if(strcmp($type,'png') === 0 or strcmp($type,'gif') === 0){
				
				if ($this->params['backgroundColor'])
				{
					imagealphablending($tmpImg, true);
				}
				else
				{
					imagealphablending($tmpImg, false);
				}
				
				imagesavealpha($tmpImg, true);
			}

			if ($this->params['resample'] === 'yes')
			{
				//copy and resample
				imagecopyresampled($tmpImg, $img, $xDst, $yDst, $xSrc, $ySrc,$newWidth, $newHeight, $width, $height);
			}
			else
			{
				//copy and resize
				imagecopyresized($tmpImg, $img, $xDst, $yDst, $xSrc, $ySrc,$newWidth, $newHeight, $width, $height);
			}
			imagedestroy($img);
			$img = $tmpImg;

			if (!function_exists($this->params['function']))
			{
				throw new Exception('Error in <b>'.__METHOD__.'</b>: function <b>'.$this->params['function']. '</b> does not exist');
			}

			$img = call_user_func($this->params['function'],$img);
		}
		
		if (!$img)
		{
			$imgWidth = isset($this->params['imgWidth']) ? $this->params['imgWidth'] : 100;
			$imgHeight = isset($this->params['imgHeight']) ? $this->params['imgHeight'] : 100;
			
			$img = imagecreate($imgWidth, $imgHeight);
			imagecolorallocate($img,200,200,200);
			
		}

		//print the image
		if (!isset($outputFile))
		{
			header("Content-type: $contentType");
		}
		
		if (strcmp($type,'png') === 0)
		{
			imagepng($img,$outputFile,9);
		}
		else if (strcmp($type,'gif') === 0)
		{
			imagegif($img,$outputFile);
		}
		else
		{
			imagejpeg($img,$outputFile,90);
		}
	}
	
}