Repository navigation
Expand file tree
/
Copy pathclass.Thumbnail.inc.php
More file actions
451 lines (413 loc) · 11.7 KB
/
Copy pathclass.Thumbnail.inc.php
File metadata and controls
451 lines (413 loc) · 11.7 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
//+----------------------------------------------------------------------+
//| WAMP (XP-SP2/2.2/5.2/5.1.0) |
//+----------------------------------------------------------------------+
//| Copyright(c) 2001-2008 Michael Wimmer |
//+----------------------------------------------------------------------+
//| Licence: GNU General Public License v3 |
//+----------------------------------------------------------------------+
//| Authors: Michael Wimmer <flaimo@gmail.com> |
//+----------------------------------------------------------------------+
//
// $Id$
/**
* @package Thumbnail
*/
/**
* Creates a thumbnail from a source image with GD2 functions
*
* Tested with Apache 2.2 and PHP 5.2.
* Last change: 2008-04-06
*
* @access public
* @author Michael Wimmer <flaimo@gmail.com>
* @copyright Michael Wimmer
* @link http://code.google.com/p/flaimo-php
* @package Thumbnail
* @version 2.001
*/
namespace SlightPHP;
class Thumbnail {
/*-------------------*/
/* V A R I A B L E S */
/*-------------------*/
/**
* possible image formats
*
* @var array
*/
protected $formats = array('gif' => 1, 'jpg' => 2, 'png' => 3, 'wbmp' => 15,
'string' => 999);
/**
* maximal height of the generated thumbnail
*
* @var int
*/
protected $thumb_max_height = 100;
/**
* maximal width of the generated thumbnail
*
* @var int
*/
protected $thumb_max_width = 100;
/**
* quality or speed when generating the thumbnail
*
* @var boolean
*/
protected $quality_thumb = TRUE;
/**
* path/filename of imagefile
*
* @var string
*/
protected $image_path;
/**
* @var int
*/
protected $image_width;
/**
* @var int
*/
protected $image_height;
/**
* image format of source
*
* @var int
*/
protected $image_type;
/**
* @var int
*/
protected $thumbnail_height;
/**
* @var int
*/
protected $thumbnail_width;
/**
* image format of the thumbnail
*
* @var int
*/
protected $thumbnail_type = 3;
/**
* @var resource
*/
protected $image;
/**
* @var resource
*/
protected $thumbnail;
const VERSION = '2.001';
/*-----------------------*/
/* C O N S T R U C T O R */
/*-----------------------*/
/**
* Constructor
*
* @param string $file path/filename of picture or stream from a DB field
* @return void
* @uses $image_path
*/
function __construct($file = '') {
$this->image_path = (string) $file;
} // end constructor
/*-------------------*/
/* F U N C T I O N S */
/*-------------------*/
/**
* sets the output type of the thumbnail
*
* @param string $format gif, jpg, png, wbmp
* @return void
* @uses $thumbnail_type
* @uses $formats
*/
public function setOutputFormat($format = 'png') {
if (array_key_exists(trim($format), $this->formats)) {
$this->thumbnail_type = $this->formats[trim($format)];
} // end if
} // end function
/**
* sets the max. height of the thumbnail
*
* @param int $height
* @return boolean
* @uses readSourceImageData()
* @uses $image_height
* @uses $thumb_max_height
*/
public function setMaxHeight($height = 0) {
$this->readSourceImageData();
$height = (int) $height;
if ($height < $this->image_height && $height > 0) {
$this->thumb_max_height = $height;
}else{
$this->thumb_max_height = $this->image_height;
}
return true;
} // end function
/**
* sets the max. width of the thumbnail
*
* @param int $width
* @return boolean
* @uses readSourceImageData()
* @uses $image_width
* @uses $thumb_max_width
*/
public function setMaxWidth($width = 0) {
$this->readSourceImageData();
$width = (int) $width;
if ($width < $this->image_width && $width > 0) {
$this->thumb_max_width = $width;
}else{
$this->thumb_max_width = $this->image_width;
}
return true;
} // end function
/**
* sets the max. width and height of the thumbnail
*
* passes values to the functions setMaxHeight() and setMaxWidth()
*
* @param int $width
* @param int $height
* @return boolean
* @uses setMaxHeight()
* @uses setMaxWidth()
*/
public function setMaxSize($width = 0, $height = 0) {
$this->setMaxWidth($width);
$this->setMaxHeight($height);
return true;
} // end function
/**
* whether to create thumbs fast or with good quality
*
* @param boolean $boolean
* @return void
* @uses $quality_thumb
*/
public function setQualityOutput($boolean = TRUE) {
$this->quality_thumb = (boolean) $boolean;
} // end function
/**
* reads metadata of the source image
*
* @return void
* @uses $image_width
* @uses $image_height
* @uses $image_type
* @uses $formats
*/
protected function readSourceImageData() {
if (!file_exists($this->image_path)) { // if source pic wasnt found
$this->image_path = 'error_pic';
$this->image_width =& $this->thumb_max_width;
$this->image_height =& $this->thumb_max_height;
$this->image = @imagecreatetruecolor($this->image_width, $this->image_height)
or die ('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($this->image, 255, 255, 255);
imagestring($this->image, 1, 2,
($this->image_height / 2 - 10),
"Could't find", $text_color);
imagestring($this->image, 1, 2,
($this->image_height / 2 - 4),
'source image', $text_color);
imagestring($this->image, 1, 2,
($this->image_height / 2 + 4),
'(Thumbnail V' . self::VERSION . ')', $text_color);
} else {
if (!isset($this->image_width)) {
list($this->image_width, $this->image_height, $this->image_type, $attr) = getimagesize($this->image_path);
unset($attr);
if (!in_array($this->image_type, $this->formats)) {
die("Can't create thumbnail from '" . $this->image_type . "' source: " . $this->image_path);
} // end if
} // end if
} // end if
} // end function
/**
* reads the source image into a variable
*
* @return void
* @uses $image
* @uses readSourceImageData()
* @uses $image_type
* @uses $image_path
*/
protected function readSourceImage() {
if (!isset($this->image)) {
$this->readSourceImageData();
switch ($this->image_type) {
case 1:
$this->image = imagecreatefromgif($this->image_path);
break;
case 2:
$this->image = imagecreatefromjpeg($this->image_path);
break;
case 3:
$this->image = imagecreatefrompng($this->image_path);
break;
case 15:
$this->image = imagecreatefromwbmp($this->image_path);
break;
case 999:
default:
$this->image = imagecreatefromstring($this->image_path);
break;
} // end switch
imagesavealpha($this->image,true);
} // end if
} // end function
/**
* sets the actual width and height of the thumbnail based on the source image size and the max limits for the thumbnail
*
* @return void
* @uses readSourceImageData()
* @uses $image_height
* @uses $thumb_max_height
* @uses $image_width
* @uses $thumb_max_width
*/
protected function setThumbnailSize() {
$this->readSourceImageData();
if (($this->image_height > $this->thumb_max_height)
|| ($this->image_width > $this->thumb_max_width)) {
$sizefactor = (double) (($this->image_height > $this->image_width) ? ($this->thumb_max_height / $this->image_height) : ($this->thumb_max_width / $this->image_width));
} else {
$sizefactor = (int) 1;
} // end if
$this->thumbnail_width = (int) ($this->image_width * $sizefactor);
$this->thumbnail_height = (int) ($this->image_height * $sizefactor);
$sizefactor = (int) 1;
if ($this->image_height > $this->image_width) {
if ($this->thumbnail_width > $this->thumb_max_width) {
$sizefactor = (double) ($this->thumb_max_width / $this->thumbnail_width);
} // end if
} else {
if ($this->thumbnail_height > $this->thumb_max_height) {
$sizefactor = (double) ($this->thumb_max_height / $this->thumbnail_height);
} // end if
} // end if // end if
$this->thumbnail_width = (int) ($this->thumbnail_width * $sizefactor);
$this->thumbnail_height = (int) ($this->thumbnail_height * $sizefactor);
unset($sizefactor);
} // end function
/**
* creates the thumbnail and saves it to a variable
*
* @return void
* @uses setThumbnailSize()
* @uses readSourceImage()
* @uses $thumbnail
* @uses $thumbnail_width
* @uses $thumbnail_height
* @uses $quality_thumb
* @uses $image
* @uses $image_width
* @uses $image_height
*/
protected function createThumbnail() {
$this->setThumbnailSize();
$this->readSourceImage();
if (!isset($this->thumbnail)) {
$this->thumbnail = imagecreatetruecolor($this->thumbnail_width,
$this->thumbnail_height);
imagealphablending($this->thumbnail,false);
imagesavealpha($this->thumbnail,true);
if ($this->quality_thumb === TRUE) {
imagecopyresampled($this->thumbnail, $this->image, 0, 0, 0, 0,
$this->thumbnail_width, $this->thumbnail_height,
$this->image_width, $this->image_height);
} else {
imagecopyresized($this->thumbnail, $this->image, 0, 0, 0, 0,
$this->thumbnail_width, $this->thumbnail_height,
$this->image_width, $this->image_height);
} // end if
} // end if
} // end function
/**
* outputs the thumbnail to the browser
*
* @param string $format
* @param int $quality
* @return void
* @uses setOutputFormat()
* @uses createThumbnail()
* @uses $thumbnail_type
* @uses $thumbnail
*/
public function outputThumbnail($format = 'png', $quality = 75) {
$this->setOutputFormat($format);
$this->createThumbnail();
switch ($this->thumbnail_type) {
case 1:
header('Content-type: image/gif');
imagegif($this->thumbnail);
break;
case 2:
$quality = (int) $quality;
if ($quality < 0 || $quality > 100) {
$quality = 75;
} // end if
header('Content-type: image/jpeg');
imagejpeg($this->thumbnail, '', $quality);
break;
case 3:
header('Content-type: image/png');
imagepng($this->thumbnail);
break;
case 15:
header('Content-type: image/vnd.wap.wbmp');
imagewbmp($this->thumbnail);
break;
} // end switch
imagedestroy($this->thumbnail);
imagedestroy($this->image);
} // end function
/**
* returns the variable with the thumbnail image
*
* @return mixed
* @uses setOutputFormat()
* @uses createThumbnail()
* @uses $thumbnail
*/
public function returnThumbnail() {
$this->setOutputFormat();
$this->createThumbnail();
return $this->thumbnail;
} // end function
/**
* returns the height of the thumbnail
*
* @return int
* @uses $thumbnail_height
*/
public function getThumbHeight() {
$this->createThumbnail();
return (int) $this->thumbnail_height;
} // end function
/**
* returns the width of the thumbnail
*
* @return int
* @uses $thumbnail_width
*/
public function getThumbWidth() {
$this->createThumbnail();
return (int) $this->thumbnail_width;
} // end function
/**
* returns the width of the thumbnail
*
* @return int
* @uses $thumbnail_width
*/
public function getPictureName() {
return (string) $this->image_path;
} // end function
} // end class Thumbnail