-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsmush.php
100 lines (66 loc) · 2.58 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
<?php
require_once('config.php');
$dir = DIR_IMAGE . 'catalog/';
/* Изпращане на POST заявка към
* сървъра за компресия на NitroSmush
*/
function _file_api($args) {
$source = $args['source'];
$quality = $args['quality'];
$data = "";
$boundary = "---------------------" . substr(md5(mt_rand(0, 32000)), 0, 10);
$data .= "--" . $boundary . "\n";
$data .= "Content-Disposition: form-data; name=\"quality\"\n\n";
$data .= $quality."\n";
$data .= "--" . $boundary . "\n";
$fileContents = file_get_contents($source);
$data .= "Content-Disposition: form-data; name=\"image\"; filename=\"" . basename($source) . "\"\n";
$data .= "Content-Type: " . _get_mime($source) . "\n";
$data .= "Content-Transfer-Encoding: binary\n\n";
$data .= $fileContents."\n";
$data .= "--" . $boundary . "\n";
$params = array('http' => array(
'method' => 'POST',
'header' => 'Content-Type: multipart/form-data; boundary='.$boundary,
'content' => $data
));
$url = 'http://nitrosmush.com/api.php';
$ctx = stream_context_create($params);
$fp = fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("There was a problem with $url");
}
$json = @stream_get_contents($fp);
fclose($fp);
if ($json === false) {
throw new Exception("Problem reading data from $url");
}
$response = json_decode($json, true);
if (!empty($response['error'])) {
throw new Exception($response['error']);
}
return $response['result_file'];
}
/* Извличане на MIME тип на текущото изображение */
function _get_mime($file) {
if (preg_match('~\.gif$~i', $file)) {
return 'image/gif';
} else if (preg_match('~\.jpe?g$~i', $file)) {
return 'image/jpeg';
} else if (preg_match('~\.png$~i', $file)) {
return 'image/png';
} else {
throw new Exception("Invalid extension of " . $file);
}
}
/* Начало на сканирането на директорията и обработване на изображенията */
$files = glob($dir . '*.{jpeg,gif,png}', GLOB_BRACE);
foreach ($files as $file)
{
$array = array(
'source' => $file,
'quality' => 100
);
$result = _file_api($array);
file_put_contents($file, file_get_contents($result));
}