-
Notifications
You must be signed in to change notification settings - Fork 1
/
smush.php
151 lines (122 loc) · 3.85 KB
/
smush.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
<?php
Class smush {
// original, redirects to somewhere else..
// const url = 'http://smush.it/ws.php';
// official but does not work
// const url = 'http://developer.yahoo.com/yslow/smushit/ws.php';
// used at the new page but does not hande uploads
// const url = 'http://smushit.com/ysmush.it/ws.php';
// used at the new page but does not hande uploads
// const url = 'http://smushit.eperf.vip.ac4.yahoo.com/ysmush.it/ws.php';
// working
const url = 'http://ws1.adq.ac4.yahoo.com/ysmush.it/ws.php';
// regexp for check extension
private static $regexp;
/*
*/
static function it($path, $options = array()) {
$regexp = in_array('gifs', $options) ? '/\.(jpg|jpeg|png|gif)$/i' : '/\.(jpg|jpeg|png)$/i';
$quiet = in_array('quiet', $options);
$pretend = in_array('pretend', $options);
$recursive = in_array('recursive', $options);
// create the curl object
$curl = curl_init(self::url);
// set default options
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true
));
// is the path is a folder, we get all images on these folder
$fn = is_dir($path) ? 'folder' : 'file';
// call the method
call_user_func('smush::' . $fn, $curl, $path, $regexp, $quiet, $pretend, $recursive);
// close curl to free memory
curl_close($curl);
}
/*
*/
private static function folder($curl, $path, $regexp, $quiet, $pretend, $recursive) {
// loop through all files on the folder to get images
$it = new DirectoryIterator($path);
foreach ($it as $file)
// ignore the dot file
if (!$file->isDot()) {
$path = $file->getPathname();
// if it's a folder, scan it too
if ($file->isDir()) {
if ($recursive)
self::folder($curl, $path, $regexp, $quiet, $pretend, $recursive);
}
// smush jpg, jpeg and png images
// gif images are converted to gifs if option is setted
elseif (preg_match($regexp, $path)) {
self::file($curl, $path, $regexp, $quiet, $pretend);
if (!$quiet)
echo "\n";
}
}
}
/*
*/
private static function file($curl, $path, $regexp, $quiet, $pretend) {
// check that the file exists
if (!file_exists($path))
throw new Exception('Invalid file path: ' . $path);
// check it is a valid field
elseif (preg_match($regexp, $path)) {
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
'files' => '@' . $path
));
if (!$quiet)
echo " smushing " . $path . "\n";
// call the server app
$response = curl_exec($curl);
// if no response from the server
if ($response === false) {
if (!$quiet)
echo " error: the server has gone\n";
}
// server respond
else {
// decode the json response
$data = json_decode($response);
// if there is some error
if (!empty($data->error)) {
if (!$quiet)
echo " error: " . strtolower($data->error) . "\n";
}
// if optimized size is larget than the original
elseif ($data->src_size < $data->dest_size) {
if (!$quiet)
echo " error: got larger\n";
}
// if optimized size is smaller than 20 bytes (prevent empty images)
elseif ($data->dest_size < 20) {
if (!$quiet)
echo " error: empty file downloaded";
}
// if size are equal
elseif ($data->src_size == $data->dest_size) {
if (!$quiet)
echo " cannot be optimized further";
}
else {
if (!$quiet)
echo str_pad(" " . $data->src_size . " -> " . $data->dest_size, 26, " ") . " = " . round($data->dest_size * 100 / $data->src_size) . "%\n";
// if it's a gif image it is converted to a png file
if (preg_match('/\.gif$/i', $path)) {
unlink($path);
$path = substr($path, 0, -3) . 'png';
}
if ($pretend)
return true;
$content = file_get_contents($data->dest);
return file_put_contents($path, $content);
}
}
}
elseif (!$quiet)
echo " error: invalid file " . $path . "\n";
}
}
?>