-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththumbnail.php
More file actions
143 lines (107 loc) · 3.89 KB
/
Copy paththumbnail.php
File metadata and controls
143 lines (107 loc) · 3.89 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
<?php
class Thumbnail {
// These are maximum values, either
// one may end up being less
var $new_width;
var $new_height;
var $old_width;
var $old_height;
// Filename/extension (foo.bar)
var $infile;
var $outfile;
// Full or relative path to file
// (/www/foo/bar), (foo/bar)
var $indir = THUMB_INDIR;
var $outdir = THUMB_OUTDIR;
// Quality setting of output JPEG
var $quality = THUMB_QUALITY;
// String error gets placed here if we bork somewhere
var $error = 1;
// Constructor
function thumbnail($width=THUMB_DEF_WIDTH, $height=0) {
if($width == 0) { $width = 0; }
if($height == 0) { $height = 0; }
$this->set_size($width, $height);
}
// Calculate the width and height of the new image (thumbnail)
// based on the size of the old image if one of $new_width or
// $new_height is specified, or absolutely if both are
// specified.
function calculate_dimensions($old_width, $old_height) {
$this->old_width = $old_width;
$this->old_height = $old_height;
if($this->new_width != 0 && $this->new_height != 0) {
;
}
elseif($this->new_width == 0) {
$this->new_width = $this->new_height / ($old_height/$old_width);
}
elseif($this->new_height == 0) {
$this->new_height = $this->new_width / ($old_width/$old_height);
}
}
function set_size($width=0, $height=0) {
if($width == 0 && $height == 0) {
$this->error = "Width and height both set to 0.";
return 0;
}
$this->new_width = $width;
$this->new_height = $height;
return 1;
}
function set_quality($quality) {
if($quality < 0 or $quality > 100) {
$this->error = "Quality of output image cannot be less than 0 or greater than 100";
return 0;
}
$this->quality = $quality;
return 1;
}
function create($infile, $outfile="") {
$this->infile = $infile;
// $size is an array containing the image size
// 0 = Width, 1 = Height
if (! ($size = GetImageSize("$this->indir/$infile")) ) {
$this->error = "Could Not Open Image To Get Size.";
return 0;
}
$this->calculate_dimensions($size[0], $size[1]);
//
$arr = explode(".", $infile);
if (sizeof($arr) < 2) {
$this->error = "Invalid filename for thumbnail outfile.";
return 0;
}
// Set $this->outfile to the specified output filename
// or default it to (filename-.ext)_tn.ext
if ($outfile != "") {
$this->outfile = $outfile;
} else {
$this->outfile = sprintf("%s-%d-%d-%d-%d_tn.%s",
$arr[0], $this->old_width, $this->old_height, $this->new_width,
$this->new_height, $arr[1]);
}
if (file_exists("$this->outdir/$this->outfile"))
return $this->outfile;
$thumbnail = imagecreate($this->new_width, $this->new_height);
// error checking below!!!!! *crash*
$old_image = imagecreatefromjpeg("$this->indir/$infile");
imagecopyresized($thumbnail, $old_image, 0, 0, 0, 0,
$this->new_width, $this->new_height,
$this->old_width, $this->old_height);
if(!imagejpeg($thumbnail, "$this->outdir/$this->outfile", $this->quality)) {
$this->error = "Couldn't output thumbnail, imagejpeg() failure.";
return 0;
}
return $this->outfile;
}
}
/*
$thumb = new thumb(0, 100);
$file1 = $thumb->create("foo.jpg");
$thumb->set_size(100, 200);
$file2 = $thumb->create("foo.jpg");
echo "<IMG SRC='/~dragonk/php/temp/image_cache/$file1'>";
echo "<IMG SRC='/~dragonk/php/temp/image_cache/$file2'>";
*/
?>