1 |
<?php |
2 |
// Image thumbnail0r (with an "0" :]) |
3 |
// mike[@]filespanker.com |
4 |
// |
5 |
// PARAMETERS: |
6 |
// |
7 |
// img: |
8 |
// The image file as relative to THIS script. |
9 |
// h: |
10 |
// The thumbnail's height. Default: 30 |
11 |
// w: |
12 |
// The thumbnail's width. Default: 30 |
13 |
// mode: |
14 |
// 1 = stretch: The image is resized to height and width |
15 |
|
16 |
// 0[default] = proportioned: The image is shrunken, but keeps proportions |
17 |
// type: |
18 |
// [optional] |
19 |
// jpg = JPEG |
20 |
// gif = GIF |
21 |
// png = PNG |
22 |
// If this is not set, it is determined by its file extension. |
23 |
// |
24 |
// This script's functions rely completely on your gd lib version. |
25 |
// |
26 |
// So, if I recall correctly: |
27 |
// gd v1.5 or lower : GIF |
28 |
// gd v1.6 or higher: PNG |
29 |
// gd v1.8 or higher: PNG and JPEG |
30 |
// |
31 |
// So, all three image types should never work on the same gd lib :[ |
32 |
// You can thank Unisys for that. |
33 |
// |
34 |
// Before mailing me, please actually look at the code. |
35 |
//Theres not much I could have really screwed up, and its probably an |
36 |
// issue with your gd library. Try up/downgrading it. |
37 |
|
38 |
// Configuration: |
39 |
|
40 |
|
41 |
//// CODE |
42 |
|
43 |
if( !isset($w) ) { |
44 |
$w = 50; |
45 |
} |
46 |
|
47 |
if( !isset($h) ) { |
48 |
$h = 50; |
49 |
} |
50 |
|
51 |
|
52 |
SetType($mode, 'integer'); |
53 |
SetType($w, 'integer'); |
54 |
SetType($h, 'integer'); |
55 |
SetType($img, 'string' ); |
56 |
|
57 |
function percent($p, $w) { |
58 |
return(real)(100 * ($p / $w)); |
59 |
} |
60 |
|
61 |
function unpercent($percent, $whole) { |
62 |
return(real)(($percent * $whole) / 100); |
63 |
} |
64 |
|
65 |
// Initialization |
66 |
|
67 |
// Make sure the file exists... |
68 |
if( !file_exists($img) ) { |
69 |
echo "Error: could not find file: $img."; |
70 |
exit(); |
71 |
} |
72 |
|
73 |
// If the user defined a type to use. |
74 |
if( !isset($type) ) { |
75 |
$ext = explode('.', $img); |
76 |
$ext = $ext[count($ext)-1]; |
77 |
switch( strtolower($ext) ) { |
78 |
case 'jpeg' : |
79 |
$type = 'jpg'; |
80 |
break; |
81 |
default : |
82 |
$type = $ext; |
83 |
break; |
84 |
} |
85 |
} |
86 |
|
87 |
// Create the image... |
88 |
switch( strtolower($type) ) { |
89 |
case 'jpg': |
90 |
$tmp = imagecreatefromjpeg($img); |
91 |
break; |
92 |
|
93 |
case 'gif': |
94 |
$tmp = @imagecreatefromgif($img); |
95 |
break; |
96 |
|
97 |
case 'png': |
98 |
$tmp = @imagecreatefrompng($img); |
99 |
break; |
100 |
|
101 |
default: |
102 |
echo 'Error: Unrecognized image format.'; |
103 |
exit(); |
104 |
break; |
105 |
} |
106 |
|
107 |
if( $tmp ) { |
108 |
// Resize it |
109 |
|
110 |
$ow = imagesx ($tmp); // Original image width |
111 |
$oh = imagesy ($tmp); // Original image height |
112 |
|
113 |
if( $mode ) { |
114 |
// Just smash it up to fit the dimensions |
115 |
$nw = $w; |
116 |
$nh = $h; |
117 |
} else { |
118 |
// Make it proportional. |
119 |
if( $ow > $oh ) { |
120 |
$nw = $w; |
121 |
$nh = unpercent(percent($nw, $ow), $oh); |
122 |
} else if( $oh > $ow ) { |
123 |
$nh = $h; |
124 |
$nw = unpercent(percent($nh, $oh), $ow); |
125 |
} else { |
126 |
$nh = $h; |
127 |
$oh = $w; |
128 |
} |
129 |
} |
130 |
|
131 |
$out = imagecreate($nw, $nh); |
132 |
imagecopyresized($out, $tmp, 0, 0, 0, 0, $nw, $nh, $ow, $oh); |
133 |
imagedestroy($tmp); |
134 |
} else { |
135 |
echo 'Could not create image resource.'; |
136 |
exit; |
137 |
} |
138 |
|
139 |
|
140 |
if( $out ) { |
141 |
switch( strtolower($type) ) { |
142 |
case 'jpg': |
143 |
header('Content-type: image/jpeg'); |
144 |
imagejpeg($out); |
145 |
break; |
146 |
|
147 |
case 'gif': |
148 |
header('Content-type: image/gif'); |
149 |
imagegif($out); |
150 |
break; |
151 |
|
152 |
case 'png': |
153 |
header('Content-type: image/png'); |
154 |
imagepng($out); |
155 |
break; |
156 |
} |
157 |
imagedestroy($out); |
158 |
} else { |
159 |
echo 'ERROR: Could not create resized image.'; |
160 |
} |
161 |
?> |
162 |
|