59 $this->invalidAction =
false;
60 $this->notFound =
false;
61 $this->rendered =
false;
65 if (isset($this->options[
'baseUrl']) && isset($this->options[
'cachePath'])) {
66 $baseUrl = rtrim($this->options[
'baseUrl'],
'/');
67 $this->cachePath = $this->options[
'cachePath'];
68 $relativePath = $this->options[
'relPath'] ??
'';
69 } elseif (defined(
'DEPAGE_PATH') && defined(
'DEPAGE_CACHE_PATH')) {
71 $info = parse_url(DEPAGE_BASE);
72 $baseUrl = rtrim($info[
'path'],
'/');
74 $this->cachePath = \DEPAGE_CACHE_PATH .
"graphics/";
77 $scriptParts = explode(
"/", $_SERVER[
"SCRIPT_NAME"]);
78 $uriParts = explode(
"/", $_SERVER[
"REQUEST_URI"]);
80 if (strpos($_SERVER[
"SCRIPT_NAME"],
"/lib/") !==
false) {
81 for ($i = 0; $i < count($uriParts); $i++) {
83 if ($uriParts[$i] ==
"lib") {
88 for ($i = 0; $i < count($uriParts); $i++) {
90 if ($scriptParts[$i] != $uriParts[$i]) {
95 $baseUrl = implode(
"/", array_slice($uriParts, 0, $i));
96 if (isset($this->options[
'relPath'])) {
97 $relativePath = $this->options[
'relPath'];
99 $relativePath = str_repeat(
"../", count($scriptParts) - $i - 1);
101 $this->cachePath = $relativePath .
"lib/cache/graphics/";
103 $baseUrlStatic = $baseUrl;
104 if (isset($this->options[
'baseUrlStatic'])) {
105 $baseUrlStatic = rtrim($this->options[
'baseUrlStatic'],
'/');
110 if ($baseUrl ==
"" && $baseUrlStatic ==
"") {
111 $imgUrl = substr($url, 1);
112 } elseif (strpos($url, $baseUrlStatic) === 0) {
113 $imgUrl = substr($url, strlen($baseUrlStatic) + 1);
114 } elseif (strpos($url, $baseUrl) === 0) {
115 $imgUrl = substr($url, strlen($baseUrl) + 1);
120 $success = preg_match(
"/(.*\.(jpg|jpeg|gif|png|webp|eps|tif|tiff|pdf|svg))\.([^\\\]*)\.(jpg|jpeg|gif|png|webp)/i", $imgUrl, $matches);
123 $this->invalidAction =
true;
128 $this->
id = rawurldecode($matches[0]);
129 $this->srcImg = $relativePath . rawurldecode($matches[1]);
130 $this->outImg = $this->cachePath . rawurldecode($matches[0]);
140 $actions = explode(
".", $actionString);
143 $regex = implode(
"|", array_keys($this->aliases));
144 preg_match(
"/^($regex)/i", $action, $matches);
146 if (isset($matches[1]) && isset($this->aliases[$matches[1]])) {
147 $func = $this->aliases[$matches[1]];
148 $params = substr($action, strlen($matches[1]));
155 $params = preg_split(
"/[-x,]+/", $params, -1, PREG_SPLIT_NO_EMPTY);
157 if ($func ==
"addBackground") {
158 if (!in_array($params[0], [
"transparent",
"checkerboard"])) {
159 $params[0] =
"#{$params[0]}";
162 foreach ($params as $i => &$p) {
164 if ($p == 0 && $i < 2) {
170 $this->actions[] = [$func, $params];
172 $this->invalidAction =
true;
183 public function render($url =
null): self
186 $url = $_SERVER[
"REQUEST_URI"];
190 if ($this->invalidAction) {
194 $outDir = dirname($this->outImg);
195 if (!is_dir($outDir)) {
196 mkdir($outDir, 0755,
true);
198 if (file_exists($this->outImg) && filemtime($this->outImg) >= filemtime($this->srcImg)) {
207 foreach ($this->actions as $action) {
208 list($func, $params) = $action;
209 if (is_callable([$graphics, $func])) {
210 call_user_func_array([$graphics, $func], $params);
215 $graphics->render($this->srcImg, $this->outImg);
217 $this->rendered =
true;
219 $this->notFound =
true;
222 $this->invalidAction =
true;