-
Notifications
You must be signed in to change notification settings - Fork 110
/
thumbnail_lib.php
executable file
·432 lines (380 loc) · 14.6 KB
/
thumbnail_lib.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
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
<?php
# Movable Type (r) (C) Six Apart Ltd. All Rights Reserved.
# This code cannot be redistributed without permission from www.sixapart.com.
# For more information, consult your Movable Type license.
#
# $Id$
require_once('class.exception.php');
class Thumbnail {
private $src_file;
private $src_w;
private $src_h;
private $src_type;
private $src_img;
private $src_attr;
private $dest_img;
private $dest_w;
private $dest_h;
private $dest_scale;
private $dest_square;
private $dest_id;
private $dest_format;
private $dest_type;
private $dest_file;
# construct
public function __construct ($src = null) {
if (!$this->is_available())
throw new MTExtensionNotFoundException('GD');
$this->src_file = null;
$this->src_w = 0;
$this->src_h = 0;
$this->src_type = null;
$this->src_img = null;
$this->src_attr = null;
$this->dest_w = 0;
$this->dest_h = 0;
$this->dest_scale = 0;
$this->dest_square = false;
$this->dest_id = null;
$this->dest_format = '%f-thumb-%wx%h-%i%x';
$this->dest_type = 'auto';
$this->dest_img = null;
$this->dest_file = '';
if (!is_null($src))
$this->src_file = $src;
}
# destruct
public function __destruct () {
if (is_resource($this->src_img))
@imagedestroy($this->src_img);
if (is_resource($this->dest_img))
@imagedestroy($this->dest_img);
}
# property accessor
public function src_file($src = null) {
if (!is_null($src))
$this->src_file = $src;
$src_file = $this->src_file;
if (strtoupper(substr(PHP_OS, 0,3) == 'WIN') && extension_loaded('mbstring')) {
// Changes character-set of filename to SJIS on Windows.
$src_file = mb_convert_encoding($src_file, "SJIS", "auto");
}
return $src_file;
}
public function dest_file($dest = null) {
if (!is_null($dest))
$this->dest_file = $dest;
$dest_file = $this->dest_file;
if (strtoupper(substr(PHP_OS, 0,3) == 'WIN') && extension_loaded('mbstring')) {
// Changes character-set of filename to SJIS on Windows.
$src_file = mb_convert_encoding($dest_file, "SJIS", "auto");
}
return $dest_file;
}
public function width($w = null) {
if (!is_null($w))
$this->dest_w = $w;
return $this->dest_w;
}
public function height($h = null) {
if (!is_null($h))
$this->dest_h = $h;
return $this->dest_h;
}
public function scale($scale = null) {
if (!is_null($scale))
$this->dest_scale = $scale;
return $this->dest_scale;
}
public function square($square = null) {
if (!is_null($square))
$this->dest_square = $square;
return $this->dest_square;
}
public function id($id = null) {
if (!is_null($id))
$this->dest_id = $id;
return $this->dest_id;
}
public function format($fmt = null) {
if (!is_null($fmt))
$this->dest_format = $fmt;
return $this->dest_format;
}
public function type($type = null) {
if (!is_null($type))
$this->dest_type = $type;
return $this->dest_type;
}
public function dest() {
$dest_file = $this->dest_file();
if ( strtoupper(substr(PHP_OS, 0,3) == 'WIN') && extension_loaded('mbstring') ) {
// Changes character-set of filename to 'UTF-8' on Windows.
$dest_file = mb_convert_encoding($dest_file, mb_internal_encoding(), "auto");
}
return $dest_file;
}
private function load_image () {
$src_file = $this->src_file();
if (empty($src_file)) return false;
if (!file_exists($src_file)) return false;
# Get source image information
list($this->src_w, $this->src_h, $this->src_type, $this->src_attr) = getimagesize($src_file);
switch($this->src_type) {
case IMAGETYPE_GIF:
$this->src_img = @imagecreatefromgif($src_file);
break;
case IMAGETYPE_JPEG:
$this->src_img = @imagecreatefromjpeg($src_file);
break;
case IMAGETYPE_PNG:
$this->src_img = @imagecreatefrompng($src_file);
break;
case IMAGETYPE_WEBP:
$this->src_img = @imagecreatefromwebp($src_file);
break;
default: #Unsupported format
throw new MTUnsupportedImageTypeException($src_file);
}
if (empty($this->src_img)) return false;
return true;
}
# Can we use function of GD?
public function is_available () {
return extension_loaded('gd');
}
# Calculate image size
# This function returns array object.
# [0] thumbnail width
# [1] thumbnail height
# [2] thumbnail width for file name
# [3] thumbnail width for file name
private function _calculate_size () {
# Calculate thumbnail size
$thumb_w = $this->src_w;
$thumb_h = $this->src_h;
$thumb_w_name = $this->src_w;
$thumb_h_name = $this->src_h;
if ($this->dest_scale > 0) {
$thumb_w = intval($this->src_w * $this->dest_scale / 100);
$thumb_h = intval($this->src_h * $this->dest_scale / 100);
$thumb_w_name = $thumb_w;
$thumb_h_name = $thumb_h;
} elseif ($this->dest_square) {
if ($this->dest_w > 0) {
$thumb_w = $this->dest_w;
$thumb_h = $this->dest_w;
$thumb_w_name = $this->dest_w;
$thumb_h_name = $this->dest_w;
} elseif($this->dest_h > 0) {
$thumb_w = $this->dest_h;
$thumb_h = $this->dest_h;
$thumb_w_name = $this->dest_h;
$thumb_h_name = $this->dest_h;
} else {
// dest_w and dest_h is unset;
if($this->src_w > $this->src_h){
$thumb_w = $this->src_h;
$thumb_h = $this->src_h;
$thumb_w_name = $this->src_h;
$thumb_h_name = $this->src_h;
} else {
$thumb_w = $this->src_w;
$thumb_h = $this->src_w;
$thumb_w_name = $this->src_w;
$thumb_h_name = $this->src_w;
}
}
} elseif ($this->dest_w > 0 || $this->dest_h > 0) {
$thumb_w_name = 'auto';
$thumb_h_name = 'auto';
$x = $this->dest_w;
$y = $this->dest_h;
$pct = $this->dest_w > 0 ? ($x / $thumb_w) : ($y / $thumb_h);
$thumb_w = (int)($thumb_w * $pct);
$thumb_h = (int)($thumb_h * $pct);
if ($this->dest_w > 0 && $thumb_w == $this->dest_w ) $thumb_w_name = $this->dest_w;
if ($this->dest_h > 0 && $thumb_h == $this->dest_h ) $thumb_h_name = $this->dest_h;
}
return array($thumb_w, $thumb_h, $thumb_w_name, $thumb_h_name);
}
private function _make_dest_name ($w, $h) {
if ($this->dest_type == 'auto') {
$output = $this->src_type;
} elseif (strtolower($this->dest_type) == 'gif') {
$output = IMAGETYPE_GIF;
} elseif (strtolower($this->dest_type) == 'jpeg') {
$output = IMAGETYPE_JPEG;
} elseif (strtolower($this->dest_type) == 'png') {
$output = IMAGETYPE_PNG;
} elseif (strtolower($this->dest_type) == 'webp') {
$output = IMAGETYPE_WEBP;
} else {
$output = $this->src_type;
}
switch($output) {
case IMAGETYPE_GIF:
$ext = '.gif';
break;
case IMAGETYPE_JPEG:
$ext = '.jpg';
break;
case IMAGETYPE_PNG:
$ext = '.png';
break;
case IMAGETYPE_WEBP:
$ext = '.webp';
break;
default:
$ext = image_type_to_extension($output);
}
$pathinfo = pathinfo($this->src_file());
$basename = basename($pathinfo['basename'], '.'.$pathinfo['extension']);
$patterns[0] = '/%w/';
$patterns[1] = '/%h/';
$patterns[2] = '/%f/';
$patterns[3] = '/%i/';
$patterns[4] = '/%x/';
$replacement[0] = $w;
$replacement[1] = $h;
$replacement[2] = $basename;
$replacement[3] = $this->dest_id;
$replacement[4] = $ext;
return preg_replace($patterns, $replacement, $this->dest_format);
}
# Load or generate a thumbnail.
public function get_thumbnail ($args = null) {
# Parse args
if (!empty($args)) {
if (!empty($args['dest']))
$this->dest_file = $args['dest'];
if (!empty($args['width']))
$this->dest_w = $args['width'];
if (!empty($args['height']))
$this->dest_h = $args['height'];
if (!empty($args['id']))
$this->dest_id = $args['id'];
if (!empty($args['scale']))
$this->dest_scale = $args['scale'];
if (!empty($args['format']))
$this->dest_format = $args['format'];
if (!empty($args['dest_type']))
$this->dest_type = $args['dest_type'];
if (!empty($args['square']))
$this->dest_square = $args['square'];
}
# Load source image
if (!$this->load_image()) return false;
# Calculate thumbnail size
list ($thumb_w, $thumb_h, $thumb_w_name, $thumb_h_name) = $this->_calculate_size();
$this->dest_w = $thumb_w;
$this->dest_h = $thumb_h;
# Decide a destination file name
if (empty($this->dest_file)) {
$this->dest_file($this->_make_dest_name($thumb_w_name, $thumb_h_name));
}
# Generate
$dest_file = $this->dest_file();
if (file_exists($dest_file)) {
if ($this->src_w == $this->src_h) {
$compulsive_resize = 0;
}
else {
list ($tmp_w, $tmp_h) = getimagesize($dest_file);
$ds = $this->dest_square;
$compulsive_resize =
(($ds && $tmp_w != $tmp_h) || (!$ds && $tmp_w == $tmp_h))
? 1 : 0 ;
}
}
else {
$compulsive_resize = 1;
}
if ($compulsive_resize) {
$dir_name = dirname($dest_file);
if (!file_exists($dir_name))
mkpath($dir_name, 0777);
if (!is_writable($dir_name)) {
return false;
}
# if square modifier is enable, crop & resize
$src_x = 0;
$src_y = 0;
$target_w = $this->src_w;
$target_h = $this->src_h;
if ($this->dest_square) {
if ($this->src_w > $this->src_h) {
$src_x = (int)($this->src_w - $this->src_h) / 2;
$src_y = 0;
$target_w = $this->src_h;
} else {
$src_x = 0;
$src_y = (int)($this->src_h - $this->src_w) / 2;
$target_h = $this->src_w;
}
}
# Create dest image
$this->dest_img = imagecreatetruecolor ( $thumb_w, $thumb_h );
# Check transparent color support
# Code from https://github.com/maxim/smart_resize_image/blob/master/smart_resize_image.function.php
if ( ( $this->src_type == 1) || ( $this->src_type == 3 ) ) {
$trnprt_indx = imagecolortransparent( $this->src_img );
// If we have a specific transparent color
if ($trnprt_indx >= 0) {
// Get the original image's transparent color's RGB values
$trnprt_color = imagecolorsforindex( $this->src_img, $trnprt_indx );
// Allocate the same color in the new image resource
$trnprt_indx = imagecolorallocate( $this->dest_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
// Completely fill the background of the new image with allocated color.
imagefill( $this->dest_img, 0, 0, $trnprt_indx );
// Set the background color for new image to transparent
imagecolortransparent( $this->dest_img, $trnprt_indx );
} elseif ( $this->src_type == 3 ) {
// Always make a transparent background color for PNGs that don't have one allocated already
// Turn off transparency blending (temporarily)
imagealphablending( $this->dest_img, false );
// Create a new transparent color for image
$color = imagecolorallocatealpha( $this->dest_img, 0, 0, 0, 127 );
// Completely fill the background of the new image with allocated color.
imagefill( $this->dest_img, 0, 0, $color );
// Restore transparency blending
imagesavealpha( $this->dest_img, true );
}
}
# Create thumbnail
$result = imagecopyresampled ( $this->dest_img, $this->src_img, 0, 0, $src_x, $src_y,
$thumb_w, $thumb_h, $target_w, $target_h);
$output = $this->src_type;
if ($this->dest_type != 'auto') {
if ( strtolower($this->dest_type) == 'gif' )
$output = IMAGETYPE_GIF;
elseif ( strtolower($this->dest_type) == 'jpeg' )
$output = IMAGETYPE_JPEG;
elseif ( strtolower($this->dest_type) == 'png' )
$output = IMAGETYPE_PNG;
elseif ( strtolower($this->dest_type) == 'webp' )
$output = IMAGETYPE_WEBP;
else
$output = $this->src_type;
}
switch($output) {
case IMAGETYPE_GIF:
imagegif($this->dest_img, $dest_file);
break;
case IMAGETYPE_JPEG:
imagejpeg($this->dest_img, $dest_file);
break;
case IMAGETYPE_PNG:
imagepng($this->dest_img, $dest_file);
break;
case IMAGETYPE_WEBP:
imagewebp($this->dest_img, $dest_file);
break;
}
@imagedestroy($this->dest_img);
}
@imagedestroy($this->src_img);
return true;
}
}
?>