1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flynt\TimberDynamicResize; |
4
|
|
|
|
5
|
|
|
use Twig_SimpleFilter; |
6
|
|
|
use Timber; |
7
|
|
|
use Routes; |
8
|
|
|
|
9
|
|
|
const DB_VERSION = '1.0'; |
10
|
|
|
const TABLE_NAME = 'resized_images'; |
11
|
|
|
const IMAGE_ROUTE = 'dynamic-images'; |
12
|
|
|
const IMAGE_PATH_SEPARATOR = 'dynamic'; |
13
|
|
|
|
14
|
|
|
function getTableName() |
15
|
|
|
{ |
16
|
|
|
global $wpdb; |
17
|
|
|
return $wpdb->prefix . TABLE_NAME; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
call_user_func(function () { |
21
|
|
|
$optionName = TABLE_NAME . '_db_version'; |
22
|
|
|
|
23
|
|
|
$installedVersion = get_option($optionName); |
|
|
|
|
24
|
|
|
|
25
|
|
|
if ($installedVersion !== DB_VERSION) { |
26
|
|
|
global $wpdb; |
27
|
|
|
$tableName = getTableName(); |
28
|
|
|
|
29
|
|
|
$charsetCollate = $wpdb->get_charset_collate(); |
30
|
|
|
|
31
|
|
|
$sql = "CREATE TABLE $tableName ( |
32
|
|
|
url varchar(511), |
33
|
|
|
arguments text, |
34
|
|
|
PRIMARY KEY (url) |
35
|
|
|
) $charsetCollate;"; |
36
|
|
|
|
37
|
|
|
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
|
|
|
|
38
|
|
|
dbDelta($sql); |
|
|
|
|
39
|
|
|
|
40
|
|
|
update_option($optionName, DB_VERSION); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
}); |
43
|
|
|
|
44
|
|
|
add_action('timber/twig/filters', function ($twig) { |
|
|
|
|
45
|
|
|
$twig->addFilter(new Twig_SimpleFilter('resizeDynamic', function ($src, $w, $h = 0, $crop = 'default', $force = false) { |
46
|
|
|
$resizeOp = new Timber\Image\Operation\Resize($w, $h, $crop); |
47
|
|
|
$fileinfo = pathinfo($src); |
48
|
|
|
$resizedUrl = $resizeOp->filename($fileinfo['dirname'] . '/' . $fileinfo['filename'], $fileinfo['extension']); |
49
|
|
|
|
50
|
|
|
$arguments = [ |
51
|
|
|
'src' => $src, |
52
|
|
|
'w' => $w, |
53
|
|
|
'h' => $h, |
54
|
|
|
'crop' => $crop, |
55
|
|
|
'force' => $force, |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
global $wpdb; |
59
|
|
|
$tableName = getTableName(); |
60
|
|
|
$wpdb->query($wpdb->prepare("REPLACE INTO {$tableName} VALUES (%s, %s)", [$resizedUrl, json_encode($arguments)])); |
61
|
|
|
|
62
|
|
|
return str_replace('/app/uploads/', '/app/uploads/' . IMAGE_PATH_SEPARATOR . '/', $resizedUrl); |
63
|
|
|
})); |
64
|
|
|
|
65
|
|
|
return $twig; |
66
|
|
|
}); |
67
|
|
|
|
68
|
|
|
Routes::map(IMAGE_ROUTE, function () { |
69
|
|
|
$src = str_replace('/app/uploads/' . IMAGE_PATH_SEPARATOR . '/', '/app/uploads/', home_url($_GET['src'] ?? '')); |
|
|
|
|
70
|
|
|
|
71
|
|
|
global $wpdb; |
72
|
|
|
$tableName = getTableName(); |
73
|
|
|
$resizedImage = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$tableName} WHERE url = %s", $src)); |
74
|
|
|
|
75
|
|
|
if (empty($resizedImage)) { |
76
|
|
|
header("HTTP/1.0 404 Not Found"); |
77
|
|
|
exit; |
78
|
|
|
} |
79
|
|
|
$urlParts = wp_parse_url($src); |
|
|
|
|
80
|
|
|
$homeUrl = home_url(); |
81
|
|
|
$localDev = parse_url($homeUrl)['host'] !== $urlParts['host']; |
82
|
|
|
if ($localDev) { |
83
|
|
|
$src = http_build_url($homeUrl, ['path' => $urlParts['path']]); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
$moveImageFunction = function ($location) { |
86
|
|
|
return str_replace('/app/uploads/', '/app/uploads/' . IMAGE_PATH_SEPARATOR . '/', $location); |
87
|
|
|
}; |
88
|
|
|
add_filter('timber/image/new_url', $moveImageFunction); |
|
|
|
|
89
|
|
|
add_filter('timber/image/new_path', $moveImageFunction); |
90
|
|
|
$arguments = json_decode($resizedImage->arguments, true); |
91
|
|
|
$url = Timber\ImageHelper::resize($arguments['src'], (int) $arguments['w'], (int) $arguments['h'], $arguments['crop'], false); |
92
|
|
|
|
93
|
|
|
remove_filter('timber/image/new_url', $moveImageFunction); |
|
|
|
|
94
|
|
|
remove_filter('timber/image/new_path', $moveImageFunction); |
95
|
|
|
|
96
|
|
|
Timber\ImageHelper::img_to_webp($url); |
97
|
|
|
|
98
|
|
|
if ($localDev) { |
99
|
|
|
unset($urlParts['path']); |
100
|
|
|
$url = http_build_url($url, $urlParts); |
101
|
|
|
} |
102
|
|
|
header("Location: {$url}", true, 301); |
103
|
|
|
exit; |
104
|
|
|
}); |
105
|
|
|
|