forked from noeljackson/PhotoStack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
thumb.php
executable file
·204 lines (176 loc) · 7.09 KB
/
thumb.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
<?php
/**
* Creates and caches a thumbnail of the specified size for the specified image.
*/
//require config class
require_once "includes/config.class.php";
if(get_magic_quotes_gpc())
new Thumb(stripslashes($_REQUEST['gallery']),stripslashes($_REQUEST["image"]), $_REQUEST["width"], $_REQUEST["height"], isset($_REQUEST["force"]));
else
new Thumb($_REQUEST['gallery'],$_REQUEST["image"], $_REQUEST["width"], $_REQUEST["height"], isset($_REQUEST["force"]));
class Thumb {
function Thumb($gallery, $image, $max_width, $max_height, $force_size) {
//create config object
$this->config = new configuration();
//security check: make sure requested file is in galleries directory
$gal_path = realpath($this->config->pathto_galleries);
$img_path = realpath($this->config->pathto_galleries.$gallery."/".$image);
$ext = strtolower(pathinfo($img_path, PATHINFO_EXTENSION));
if(substr($img_path,0,strlen($gal_path)) != $gal_path) header("HTTP/1.0 404 Not Found");
$extensions = explode("|", strtolower($this->config->recognised_extensions));
$movie_extensions = explode("|", strtolower($this->config->movie_extensions));
if( !in_array($ext, $extensions) ){
die("Specified file is not a recognised image file");
}
if(in_array($ext, $movie_extensions)) $movie = true;
$image_path = realpath($this->config->pathto_galleries."$gallery/$image");
$thumb_path = $this->config->pathto_cache.$max_width."x".$max_height.($force_size?"f":"").strtr("-$gallery-$image",":/?\\","----");
$movie_path = $this->config->pathto_cache.md5(strtr("-$gallery-$image",":/?\\","----")).'.flv';
if(in_array($ext, $movie_extensions)) { $thumb_path = $thumb_path.'.jpg'; }
$imageModified = @filemtime($image_path);
$thumbModified = @filemtime($thumb_path);
if($_GET['movie']) {
$imageModified = @filemtime($movie_path);
$thumb_path = $movie_path;
header("Content-type: video/x-flv");
} else {
//send appropriate headers
switch($ext) {
case 'gif' : header("Content-type: image/gif"); break;
case 'png' : header("Content-type: image/png"); break;
default: header("Content-type: image/jpeg"); break;
}
}
#header('Content-Disposition: inline; filename='.$_GET['image']);
//if thumbnail is newer than image then output cached thumbnail and exit
if($imageModified<$thumbModified) {
# header("Last-Modified: ".gmdate("D, d M Y H:i:s",$thumbModified)." GMT");
readfile($thumb_path);
exit;
} else {
$this->make_thumb($gallery, $image, $image_path, $thumb_path, $max_width, $max_height, $force_size, $movie, $movie_path);
}
}
function make_thumb($gallery, $image, $image_path, $thumb_path, $max_width, $max_height, $force_size, $movie = null, $movie_path = null) {
$this->config = new configuration();
if($movie) {
include_once('includes/getid3/getid3.php');
$getID3 = new getID3;
$fileinfo = $getID3->analyze($image_path);
getid3_lib::CopyTagsToComments($fileinfo);
$image_width = $fileinfo['video']['resolution_x'];
$image_height = $fileinfo['video']['resolution_y'];
} else {
$thumbQuality = $this->config->thumbnail_quality;
list($image_width, $image_height, $image_type) = GetImageSize($image_path);
}
//if aspect ratio is to be constrained set crop size
if($force_size) {
$newAspect = $max_width/$max_height;
$oldAspect = $image_width/$image_height;
if($newAspect > $oldAspect) {
$cropWidth = $image_width;
$cropHeight = round($oldAspect/$newAspect * $image_height);
} else {
$cropWidth = round($newAspect/$oldAspect * $image_width);
$cropHeight = $image_height;
}
//else crop size is image size
} else {
$cropWidth = $image_width;
$cropHeight = $image_height;
}
//set cropping offset
$cropX = floor(($image_width-$cropWidth)/2);
$cropY = floor(($image_height-$cropHeight)/2);
//compute width and height of thumbnail to create
if($cropWidth >= $max_width && ($cropHeight < $max_height || ($cropHeight > $max_height && round($cropWidth/$cropHeight * $max_height) > $max_width))) {
$thumbWidth = $max_width;
$thumbHeight = round($cropHeight/$cropWidth * $max_width);
} elseif($cropHeight >= $max_height) {
$thumbWidth = round($cropWidth/$cropHeight * $max_height);
$thumbHeight = $max_height;
} else {
//image is smaller than required dimensions so output it and exit
readfile($image_path);
exit;
}
if($movie) {
$thumbHeight = floor($thumbHeight/2) *2;
$thumbWidth = floor($thumbWidth/2) *2;
if($_GET['movie']) {
exec($this->config->pathto_ffmpeg.' -i '.escapeshellarg($image_path).' -f flv '.escapeshellarg($movie_path));
exec('/bin/cat '.escapeshellarg($movie_path).' | '.$this->config->pathto_flvtool.' -U stdin '.escapeshellarg($movie_path));
readfile($movie_path);
exit;
}
exec($this->config->pathto_ffmpeg.' -i '.escapeshellarg($image_path).' -s '.$thumbWidth.'x'.$thumbHeight.' -f mjpeg -t 1 -ss 3 '.escapeshellarg($thumb_path));
readfile($thumb_path);
exit;
}
switch($this->config->thumbnail_software) {
case "im" : //use ImageMagick
// hack for square thumbs;
if(($thumbWidth == $thumbHeight) or $force_size) {
$thumbsize = $thumbWidth;
if($image_height > $image_width) {
$cropY = -($thumbsize / 2);
$cropX = 0;
$thumbcommand = "{$thumbsize}x";
} else {
$cropY = -($thumbsize / 2);
$cropX = 0;
$thumbcommand = "x{$thumbsize}";
}
} else {
$thumbcommand = $thumbWidth.'x'.$thumbHeight;
}
$cmd = '"'.$this->config->pathto_convert.'"';
if($force_size) $cmd .= " -gravity center -crop {$thumbWidth}x{$thumbHeight}!+0+0";
$cmd .= " -resize {$thumbcommand}";
if($image_type == 2) $cmd .= " -quality $thumbQuality";
if($this->config->progressive_thumbs) $cmd .= " -interlace Plane";
if($this->config->remove_jpeg_profile) $cmd .= ' +profile "*"';
$cmd .= ' '.escapeshellarg($image_path).' '.escapeshellarg($thumb_path);
exec($cmd);
readfile($thumb_path);
exit;
break;
case "gd2" :
default : //use GD by default
//read in image as appropriate type
switch($image_type) {
case 1 : $image = ImageCreateFromGIF($image_path); break;
case 3 : $image = ImageCreateFromPNG($image_path); break;
case 2 :
default: $image = ImageCreateFromJPEG($image_path); break;
}
//create blank truecolor image
$thumb = ImageCreateTrueColor($thumbWidth,$thumbHeight);
//resize image with resampling
ImageCopyResampled(
$thumb, $image,
0, 0, $cropX, $cropY,
$thumbWidth, $thumbHeight, $cropWidth, $cropHeight);
//set image interlacing
ImageInterlace($thumb, $this->config->progressive_thumbs);
//output image of appropriate type
switch($image_type) {
case 1 :
//GIF images are output as PNG
case 3 :
ImagePNG($thumb);
ImagePNG($thumb,$thumb_path);
break;
case 2 :
default:
ImageJPEG($thumb,"",$thumbQuality);
ImageJPEG($thumb,$thumb_path,$thumbQuality);
break;
}
ImageDestroy($image);
ImageDestroy($thumb);
}
}
}
?>