-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thumbnail.inc
270 lines (247 loc) · 5.56 KB
/
Thumbnail.inc
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
<?php
/**
* @package SPLIB
* @version $Id: ThumbNail.php,v 1.10 2003/09/23 19:59:15 harry Exp $
*/
/**
* Thumbnail<br />
* Resizes images to thumbnails
* @package SPLIB
* @access public
* @todo bug fix for multiple images
* @todo PHP < 4.3.0 compatibility
*/
class Thumbnail {
/**
* Maximum width of the thumbnail in pixels
* @access private
* @var int
*/
var $maxWidth;
/**
* Maximum height of the thumbnail in pixels
* @access private
* @var int
*/
var $maxHeight;
/**
* Whether to scale image to fit thumbnail (true) or
* strech to fit (false)
* @access private
* @var boolean
*/
var $scale;
/**
* Whether to inflate images smaller the the thumbnail
* @access private
* @var boolean
*/
var $inflate;
/**
* List of accepted image types based on MIME description
* @access private
* @var array
*/
var $types;
/**
* Stores function names for each image type e.g. imagecreatefromjpeg
* @access private
* @var array
*/
var $imgLoaders;
/**
* Stores function names for each image type e.g. imagejpeg
* @access private
* @var array
*/
var $imgCreators;
/**
* The source image
* @access private
* @var resource
*/
var $source;
/**
* Width of source image in pixels
* @access private
* @var int
*/
var $sourceWidth;
/**
* Height of source image in pixels
* @access private
* @var int
*/
var $sourceHeight;
/**
* MIME type of source image
* @access private
* @var string
*/
var $sourceMime;
/**
* The thumbnail
* @access private
* @var resource
*/
var $thumb;
/**
* Width of thumbnail in pixels
* @access private
* @var int
*/
var $thumbWidth;
/**
* Height of thumbnail in pixels
* @access private
* @var int
*/
var $thumbHeight;
/**
* Thumbnail constructor
* @param int max width of thumbnail
* @param int max height of thumbnail
* @param boolean (optional) if true image scales
* @param boolean (optional) if true inflate small images
* @access public
*/
function Thumbnail ($maxWidth,$maxHeight,$scale=true,$inflate=true) {
$this->maxWidth=$maxWidth;
$this->maxHeight=$maxHeight;
$this->scale=$scale;
$this->inflate=$inflate;
// Consider modifying these to add to handle other images
$this->types=array('image/jpeg','image/png','image/gif');
$this->imgLoaders=array(
'image/jpeg'=>'imagecreatefromjpeg',
'image/png'=>'imagecreatefrompng',
'image/gif'=>'imagecreatefromgif'
);
$this->imgCreators=array(
'image/jpeg'=>'imagejpeg',
'image/png'=>'imagepng',
'image/gif'=>'imagegif'
);
}
/**
* Loads an image from a file
* @param string filename (with path) of image
* @return boolean
* @access public
*/
function loadFile ($image) {
if ( !$dims=@GetImageSize($image) ) {
trigger_error('Could not find image '.$image);
return false;
}
if ( in_array($dims['mime'],$this->types) ) {
$loader=$this->imgLoaders[$dims['mime']];
$this->source=$loader($image);
$this->sourceWidth=$dims[0];
$this->sourceHeight=$dims[1];
$this->sourceMime=$dims['mime'];
$this->initThumb();
return true;
} else {
trigger_error('Image MIME type '.$dims['mime'].' not supported');
return false;
}
}
/**
* Loads an image from a string (e.g. database)
* @param string the image
* @param mime mime type of the image
* @return boolean
* @access public
*/
function loadData ($image,$mime) {
if ( in_array($mime,$this->types) ) {
$this->source=imagecreatefromstring($image);
$this->sourceWidth=imagesx($this->source);
$this->sourceHeight=imagesy($this->source);
$this->sourceMime=$mime;
$this->initThumb();
return true;
} else {
trigger_error('Image MIME type '.$mime.' not supported');
return false;
}
}
/**
* If a filename is provides, creates the thumbnail using that name
* If not, the image is output to the browser
* @param string (optional) filename to create image with
* @return boolean
* @access public
*/
function buildThumb ($file=null) {
$creator=$this->imgCreators[$this->sourceMime];
if ( isset ( $file ) ) {
return $creator($this->thumb,$file);
} else {
return $creator($this->thumb);
}
}
/**
* Returns the mime type for the thumbnail
* @return string
* @access public
*/
function getMime () {
return $this->sourceMime;
}
/**
* Returns the width of the thumbnail
* @return int
* @access public
*/
function getThumbWidth() {
return $this->thumbWidth;
}
/**
* Returns the height of the thumbnail
* @return int
* @access public
*/
function getThumbHeight() {
return $this->thumbHeight;
}
/**
* Creates the thumbnail
* @return void
* @access private
*/
function initThumb () {
if ( $this->scale ) {
if ( $this->sourceWidth > $this->sourceHeight ) {
$this->thumbWidth=$this->maxWidth;
$this->thumbHeight=floor(
$this->sourceHeight*($this->maxWidth/$this->sourceWidth)
);
} else if ( $this->sourceWidth < $this->sourceHeight ) {
$this->thumbHeight=$this->maxHeight;
$this->thumbWidth=floor(
$this->sourceWidth*($this->maxHeight/$this->sourceHeight)
);
} else {
$this->thumbWidth=$this->maxWidth;
$this->thumbHeight=$this->maxHeight;
}
} else {
$this->thumbWidth=$this->maxWidth;
$this->thumbHeight=$this->maxHeight;
}
$this->thumb=imagecreatetruecolor($this->thumbWidth,
$this->thumbHeight);
if ( $this->sourceWidth <= $this->maxWidth &&
$this->sourceHeight <= $this->maxHeight &&
$this->inflate == false ) {
$this->thumb=& $this->source;
} else {
imagecopyresampled( $this->thumb, $this->source, 0, 0, 0, 0,
$this->thumbWidth, $this->thumbHeight,
$this->sourceWidth, $this->sourceHeight );
}
}
}
?>