-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresizer.php
153 lines (116 loc) · 3.97 KB
/
resizer.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
<?php
/*
image resizer script by Luka Kladaric <[email protected]>
use via .htaccess mod_rewrite rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\.jpg).([0-9x]+)px.jpg$ bin/resizer.php?from=$1&res=$2 [L,NC]
then calling
http://yoursite/images/foo.jpg.700px.jpg
will load the 700px wide version of the image
the script will save the result back to images/foo.jpg.700px.jpg so the next time the URL is
requested, it will go straight to the actual file, skipping the PHP script invocation
supported size params:
700px - 700px wide - scale resize
x300px - 300px tall - scale resize
700x300px - scale & crop optimally (hopefully) to produce EXACTLY 700x300px image
REQUIRES Imagick's convert app
CONFIGURATION
- make sure $approot is correct
- make sure you have /usr/bin/convert
- make sure the RewriteRule has the correct path to the script
- adjust $nice to suite your needs (the default 10 should ensure resizer doesn't hog the spotlight)
*/
// get rid of magic quotes, if set
if (get_magic_quotes_gpc()) {
$_GET = undoMagicQuotes($_GET);
$_POST = undoMagicQuotes($_POST);
$_COOKIE = undoMagicQuotes($_COOKIE);
$_REQUEST = undoMagicQuotes($_REQUEST);
}
chdir($approot = ".."); // force current working directory to app root
$nice = "nice -n 10 ";
$convert = $nice."/usr/bin/convert";
$source = ifsetor($_GET['from']);
if (!is_file($source))
exit("No such file");
$res = trim(ifsetor($_GET['res'], ''));
$target = $source . ".{$res}px.jpg";
if (!$res)
exit ("Bad dimensions");
$resizeto = 0;
if (preg_match("@^x[0-9]+$@", $res)) {
$resizeto = $res;
$hintwidth = $hintheight = 2*$res;
} elseif (preg_match("@([0-9]+)x([0-9]+)@", $res, $matches)) {
$width = $matches[1];
$height = $matches[2];
$hintwidth = 2*$matches[1];
$hintheight = 2*$matches[2];
$newratio = $width/$height;
$oldimg = imagecreatefromjpeg($source);
$oldratio = imagesx($oldimg)/imagesy($oldimg);
unset($oldimg);
if ($newratio < $oldratio) {
$resizeto = "x{$height}";
}
} elseif (isintstr($res)) {
$width = $res;
$height = null;
$hintwidth = $hintheight = 2*$res;
} else {
die ("Bad dimensions");
}
$resizeto = ($resizeto)?$resizeto:$width;
$crop_cmd = ($height)?" -gravity Center -crop {$width}x{$height}+0+0 ":"";
@exec ($x = "{$convert} -define jpeg:size={$hintwidth}x{$hintheight} -thumbnail {$resizeto} {$crop_cmd} \"{$source}[0]\" \"{$target}\"");
// did the convert work?
if (is_file($target)) {
// if it worked, do we know what the original request was? redirect if we do.
if (isset($_SERVER["REQUEST_URI"]))
httpredirect($_SERVER["REQUEST_URI"]);
// otherwise, serve the file through readfile()
@header("Content-Type: image/jpg");
readfile ($target);
die;
}
echo "Image resize failed";
/* support functions */
function ifsetor(&$variable, $default = null) {
if (isset($variable)) {
$tmp = $variable;
} else {
$tmp = $default;
}
return $tmp;
}
function httpredirect($url, $permanent = false) {
// if $url is just a querystring, figure out the
// current page and send the full location
if (substr($url, 0, 1) == '?') {
$baseurl = parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);
$url = $baseurl . $url;
}
if (!headers_sent()) {
if ($permanent)
header( "HTTP/1.1 301 Moved Permanently" );
header("Location: {$url}",true);
} else {
echo "<a href=\"{$url}\">click here to continue</a>";
}
die();
}
function undoMagicQuotes($array, $topLevel=true) {
$newArray = array();
foreach($array as $key => $value) {
if (!$topLevel) {
$key = stripslashes($key);
}
if (is_array($value)) {
$newArray[$key] = undoMagicQuotes($value, false);
}
else {
$newArray[$key] = stripslashes($value);
}
}
return $newArray;
}