-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
147 lines (143 loc) · 5.94 KB
/
index.php
File metadata and controls
147 lines (143 loc) · 5.94 KB
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
<?php
namespace {
if (!\extension_loaded('gd')) {
\abort(\i('Missing %s extension.', ['PHP <a href="https://www.php.net/manual/en/book.image.php" rel="nofollow" target="_blank">gd</a>']));
}
function image(...$lot) {
return \Image::from(...$lot);
}
if (!\is_dir($folder = \LOT . \D . 'image')) {
\mkdir($folder, 0775, true);
}
}
namespace x\image {
function link($content) {
// Get link from `<img>` tag
if (false !== ($a = \strpos($content, '<img')) && \strspn($content, " \n\r\t", $a + 4)) {
if (false !== ($b = \strpos($content, '>', $a))) {
$v = ' ' . \strtr(\trim(\substr($content, $a += 4, $b - $a)), ["\n" => ' ', "\r" => ' ', "\t" => ' ']);
if (false !== ($c = \strpos($v, ' src='))) {
return \htmlspecialchars_decode(\trim(\strstr(\substr($v, $c + 5) . ' ', ' ', true), '\'"'));
}
}
// Get link from `<video>` tag
} else if (false !== ($a = \strpos($content, '<video')) && \strspn($content, " \n\r\t", $a + 6)) {
if (false !== ($b = \strpos($content, '>', $a))) {
$v = ' ' . \strtr(\trim(\substr($content, $a += 6, $b - $a)), ["\n" => ' ', "\r" => ' ', "\t" => ' ']);
if (false !== ($c = \strpos($v, ' poster='))) {
return \htmlspecialchars_decode(\trim(\strstr(\substr($v, $c + 8) . ' ', ' ', true), '\'"'));
}
}
}
return null;
}
function links($content) {
$i = 0;
$r = [];
while (false !== ($n = \strpos($content, '<', $i))) {
if (null !== ($link = link(\substr($content, $n)))) {
$r[] = $link;
}
$i = $n + 1;
}
return \array_unique($r);
}
function page__image($image) {
// Skip if `image` data has been set!
if ($image) {
return \long($image);
}
// Get link from `content` data
return ($v = $this->content) ? link($v) : null;
}
function page__images($images) {
// Skip if `images` data has been set!
if ($images) {
foreach ($images as &$image) {
$image = \long($image);
}
unset($image);
return $images;
}
// Get link(s) from `content` data
return ($v = $this->content) ? links($v) : [];
}
function route($content, $path, $query, $hash) {
if (null !== $content) {
return $content;
}
$route = \trim($state->x->image->route ?? 'image', '/');
if (!\is_file($file = \LOT . \D . 'image' . \D . ($path = \substr($path, \strlen($route) + 1)))) {
return $content;
}
if (false !== \strpos(',' . x() . ',', ',' . \strtolower(\pathinfo($file, \PATHINFO_EXTENSION)) . ',')) {
return \Hook::fire('route.image', [$content, $path, $query, $hash]);
}
return $content;
}
function route__image($content, $path) {
if (null !== $content) {
return $content;
}
$age = 60 * 60 * 24 * 365; // Cache output for 1 year
\status(200, [
'cache-control' => 'max-age=' . $age . ', private',
'expires' => \gmdate('D, d M Y H:i:s', $age + $_SERVER['REQUEST_TIME']) . ' GMT',
'pragma' => 'private'
]);
\type(\mime_content_type($file = \LOT . \D . 'image' . \D . \trim($path ?? "", '/')));
echo \file_get_contents($file);
exit;
}
$path = \trim($link->path ?? $state->route ?? 'index', '/');
$route = \trim($state->x->image->route ?? 'image', '/');
if (0 === \strpos($path, $route . '/') && \is_file(\LOT . \D . 'image' . \D . \substr($path, \strlen($route) + 1))) {
\Hook::set('route', __NAMESPACE__ . "\\route", 0);
\Hook::set('route.image', __NAMESPACE__ . "\\route__image", 100);
}
\Hook::set('page.image', __NAMESPACE__ . "\\page__image", 2.1);
\Hook::set('page.images', __NAMESPACE__ . "\\page__images", 2.1);
}
namespace x\image\page__image {
function crop($image, array $lot = []) {
if (!$lot || !$image || !\is_string($image)) {
return $image;
}
$image = \substr($image, 0, \strcspn($image, '?&#'));
$w = \ceil($lot[0]);
$h = \ceil($lot[1] ?? $w);
$q = $lot[2] ?? -1;
$x = \strtolower(\pathinfo($image, \PATHINFO_EXTENSION) ?: 'jpg');
$path = \To::path(\long($image));
$store = \LOT . \D . 'image' . \D . 't' . \D . $w . ($h !== $w ? \D . $h : "") . \D . \hash('xxh3', $image . '%' . $q) . '.' . $x;
if (\is_file($store)) {
$image = \To::link($store); // Return the image cache link
} else if (false !== \strpos(',' . \x\image\x() . ',', ',' . $x . ',')) {
$blob = new \Image(\is_file($path) ? $path : $image);
// `$page->image($w, $h, $q)`
$blob->crop($w, $h)->blob($store, $q); // Generate image cache
$image = \To::link($store); // Return the image cache link
} else if (\is_file($path)) {
$image = \To::link($path);
}
// Convert direct image link from folder `.\lot\image` to its proxy image link
\extract(\lot(), \EXTR_SKIP);
if ($image && 0 === \strpos($image, $v = \long('/lot/image/'))) {
$image = \substr_replace($image, \long('/' . \trim($state->x->image->route ?? 'image', '/') . '/'), 0, \strlen($v));
}
return $image;
}
\Hook::set('page.image', __NAMESPACE__ . "\\crop", 2.2);
}
namespace x\image\page__images {
function crop(array $images, array $lot = []) {
foreach ($images as &$image) {
$image = \x\image\page__image\crop($image, $lot);
}
return $images;
}
\Hook::set('page.images', __NAMESPACE__ . "\\crop", 2.2);
if (\defined("\\TEST") && 'x.image' === \TEST && \is_file($test = __DIR__ . \D . 'test.php')) {
require $test;
}
}