-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWebP.ocmod.xml
69 lines (56 loc) · 2.33 KB
/
WebP.ocmod.xml
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
<?xml version="1.0" encoding="utf-8"?>
<modification>
<name>WebP</name>
<code>WebP</code>
<version>1.1.0</version>
<author>stickpro</author>
<link>https://stick-design.ru</link>
<file path="catalog/model/tool/image.php">
<operation>
<search><![CDATA[$image_new = 'cache/']]></search>
<add position="after" offset="1"><![CDATA[ $image_new_webp = 'cachewebp/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . (int)$width . 'x' . (int)$height . '.webp'; ]]></add>
</operation>
<operation>
<search><![CDATA[ $imagepath_parts = explode('/', $image_new); ]]></search>
<add position="before" ><![CDATA[
if (!is_file(DIR_IMAGE . $image_new_webp) || (filectime(DIR_IMAGE . $image_new) > filectime(DIR_IMAGE . $image_new_webp))) {
$path = '';
$directories = explode('/', dirname($image_new_webp));
foreach ($directories as $directory) {
$path = $path . '/' . $directory;
if (!is_dir(DIR_IMAGE . $path)) {
@mkdir(DIR_IMAGE . $path, 0777);
}
}
$command = "cwebp -q 100 '" . DIR_IMAGE . $image_new . "' -o '". DIR_IMAGE . $image_new_webp . "'";
exec($command);
} ]]></add>
</operation>
</file>
<file path="system/library/response.php">
<operation>
<search><![CDATA[ private function compress($data, $level = 0) ]]></search>
<add position="before" ><![CDATA[
public function webpRebuild($output) {
$uri = '';
if (isset($_SERVER['REQUEST_URI'])) {
$uri = $_SERVER['REQUEST_URI'];
}
if (stripos($uri, 'admin') === false) {
if (isset($_SERVER['HTTP_ACCEPT']) && isset($_SERVER['HTTP_USER_AGENT'])) {
if( strpos( $_SERVER['HTTP_ACCEPT'], 'image/webp' ) !== false ) {
$re = '/(cache)(.*)(\.jpg\"|\.png\"|\.JPG\"|\.PNG\"|\.jpeg\"|\.JPEG\")/U';
$subst = '$1webp$2.webp';
$this->output = preg_replace($re, $subst, $this->output);
}
}
}
}
]]></add>
</operation>
<operation>
<search><![CDATA[ $output = $this ]]></search>
<add position="before" ><![CDATA[ $this->webpRebuild($this->output); ]]></add>
</operation>
</file>
</modification>