-
Notifications
You must be signed in to change notification settings - Fork 2
/
getimage.php
198 lines (166 loc) · 6.67 KB
/
getimage.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
<?php
/*
MINIGAL NANO
- A PHP/HTML/CSS based image gallery script
This script and included files are subject to licensing from Creative Commons (http://creativecommons.org/licenses/by-sa/2.5/)
You may use, edit and redistribute this script, as long as you pay tribute to the original author by NOT removing the linkback to www.minigal.dk ("Powered by MiniGal Nano x.x.x")
MiniGal Nano is created by Thomas Rybak
Copyright 2010 by Thomas Rybak
Support: www.minigal.dk
Community: www.minigal.dk/forum
Please enjoy this free script!
USAGE EXAMPLE:
File: createthumb.php
Example: <img src="createthumb.php?filename=photo.jpg&width=100&height=100">
*/
// error_reporting(E_ALL);
if(!defined("MINIGAL_INTERNAL")) {
define("MINIGAL_INTERNAL", true);
}
require("config.php");
ini_set("memory_limit",$config['memory_limit']);
function rotate_image($filename, $target) {
// Rotate JPG pictures
if (preg_match("/\.jpg$|\.jpeg$/i", $filename) && function_exists('exif_read_data') && function_exists('imagerotate')) {
$exif = exif_read_data($filename);
if (array_key_exists('IFD0', $exif)) {
$ort = $exif['IFD0']['Orientation'];
$degrees = 0;
switch($ort) {
case 6: // 90 rotate right
$degrees = 270;
break;
case 8: // 90 rotate left
$degrees = 90;
break;
}
if ($degrees != 0) return imagerotate($target, $degrees, 0);
}
}
return $target;
}
function create_thumb($filename, $extension, $outfile, $size = 1024, $keepratio = true) {
global $config;
// Define variables
$target = rotate_image($filename);
$xoord = 0;
$yoord = 0;
$height = $size;
$width = $size;
if ($config['caching'] && is_file($outfile) && filemtime($outfile)>=filemtime($filename)) {
readfile($outfile); //Use the cache
return;
}
ob_start();
if ( in_array($extension, $config['supported_video_types']) ) {
// Video thumbnail
passthru ("ffmpegthumbnailer -i " . escapeshellarg($filename) . " -o - -s " . escapeshellarg($size) . " -c jpeg -f" . ($keepratio? "" : " -a"));
} else {
// Image thumbnail
list($width_orig, $height_orig) = GetImageSize($filename);
if ($keepratio) {
// Get new dimensions
$ratio_orig = $width_orig/$height_orig;
if ($width_orig > $height_orig) {
$height = $width/$ratio_orig;
} else {
$width = $height*$ratio_orig;
}
} else {
// square thumbnail
if ($width_orig > $height_orig) { // If the width is greater than the height it’s a horizontal picture
$xoord = ceil(($width_orig-$height_orig)/2);
$width_orig = $height_orig; // Then we read a square frame that equals the width
} else {
$yoord = ceil(($height_orig-$width_orig)/2);
$height_orig = $width_orig;
}
}
if($keepratio && $size > $height_orig && $size > $width_orig) {
readfile($filename);
$outfile = null; //don't cache images that are equal to originals
} else {
// load source image
if ($extension == "jpg" || $extension == "jpeg")
$source = ImageCreateFromJPEG($filename);
else if ($extension == "gif")
$source = ImageCreateFromGIF($filename);
else if ($extension == "png")
$source = ImageCreateFromPNG($filename);
$target = ImageCreatetruecolor($width,$height);
imagecopyresampled($target,$source, 0,0, $xoord,$yoord, $width,$height, $width_orig,$height_orig);
imagedestroy($source);
if ($extension == "jpg" || $extension == "jpeg" || $extension == "png")
ImageJPEG($target,null,90);
else if ($extension == "gif")
ImageGIF($target,null,90);
imagedestroy($target);
}
}
if($outfile)
file_put_contents($outfile,ob_get_contents());
ob_end_flush();
}
$_GET['filename'] = "./" . $_GET['filename'];
if($_GET['mode'] == 'thumb') {
$size=$config['thumb_size'];
$keepratio=false;
} else {
$size=$config['small_size'];
$keepratio=true;
}
// Display error image if file isn't found
if (preg_match("/\.\.\//i", $_GET['filename']) || !is_file($_GET['filename'])) {
header('Content-type: image/jpeg');
$errorimage = ImageCreateFromJPEG('images/questionmark.jpg');
ImageJPEG($errorimage,null,90);
imagedestroy($errorimage);
exit;
}
// Display error image if file exists, but can't be opened
if (substr(decoct(fileperms($_GET['filename'])), -1, strlen(fileperms($_GET['filename']))) < 4 OR substr(decoct(fileperms($_GET['filename'])), -3,1) < 4) {
header('Content-type: image/jpeg');
$errorimage = ImageCreateFromJPEG('images/cannotopen.jpg');
ImageJPEG($errorimage,null,90);
imagedestroy($errorimage);
exit;
}
$extension = strtolower(preg_replace('/^.*\./', '', $_GET['filename']));
if ( !in_array($extension, $config['supported_image_types']) && !in_array($extension, $config['supported_video_types']) ) {
header('Content-type: image/jpeg');
$errorimage = ImageCreateFromJPEG('images/cannotopen.jpg');
ImageJPEG($errorimage,null,90);
imagedestroy($errorimage);
exit;
}
$filetimestamp=max(filemtime($_GET['filename']), filemtime("./getimage.php"), filemtime("./config.php"));
$lastmodified=gmdate("D, d M Y H:i:s \G\M\T", $filetimestamp);
if (isset($_ENV['HTTP_IF_MODIFIED_SINCE']))
$IfModifiedSince = strtotime(substr($_ENV['HTTP_IF_MODIFIED_SINCE'], 5));
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
$IfModifiedSince = strtotime(substr($_SERVER['HTTP_IF_MODIFIED_SINCE'], 5));
if ($IfModifiedSince && $IfModifiedSince >= $filetimestamp) {
header($_SERVER['SERVER_PROTOCOL'] . " 304 Not Modified");
header("Last-Modified: " . $lastmodified);
exit;
}
header("Cache-Control: public, must-revalidate");
header("Vary: Last-Modified");
header("Last-Modified: " . $lastmodified);
if ($extension == 'gif') {
header('Content-type: image/gif');
$cleanext = 'gif';
} else {
header('Content-type: image/jpeg');
$cleanext = 'jpeg';
}
// Create paths for different picture versions
$thumbnail = null;
if($config['caching']) {
$md5sum = md5($_GET['filename']);
$thumbnail = $config['cache_path'] . "/" . $md5sum . "_" . $size . "_" . ($keepratio?"keepratio":"square") . "." . $cleanext;
if(!file_exists($config['cache_path']))
mkdir($config['cache_path']);
}
create_thumb($_GET['filename'], $extension, $thumbnail, $size, $keepratio);
?>