ILIAS  release_8 Revision v8.24
class.ilMathJaxImage.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
29{
30 private const TYPE_PNG = 'png';
31 private const TYPE_SVG = 'svg';
32
37 protected \ILIAS\Filesystem\Filesystem $fs;
38
42 protected string $basepath = '/temp/tex';
43
47 protected string $tex;
48
52 protected string $suffix;
53
57 protected string $salt;
58
64 public function __construct(string $a_tex, string $a_type, int $a_dpi)
65 {
66 global $DIC;
67
68 $this->fs = $DIC->filesystem()->web();
69 $this->tex = $a_tex;
70
71 switch ($a_type) {
72 case self::TYPE_PNG:
73 $this->suffix = '.png';
74 break;
75 case self::TYPE_SVG:
76 $this->suffix = '.svg';
77 break;
78 default:
79 throw new ilMathJaxException('imagetype not supported');
80 }
81
82 $this->salt = '#' . $a_dpi;
83 }
84
88 protected function filepath(): string
89 {
90 $hash = md5($this->tex . $this->salt);
91 return $this->basepath
92 . '/' . substr($hash, 0, 4)
93 . '/' . substr($hash, 4, 4)
94 . '/' . $hash . $this->suffix;
95 }
96
100 public function absolutePath(): string
101 {
102 return CLIENT_WEB_DIR . $this->filepath();
103 }
104
108 public function exists(): bool
109 {
110 return $this->fs->has($this->filepath());
111 }
112
116 public function read(): string
117 {
118 return $this->fs->read($this->filepath());
119 }
120
125 public function write(string $a_content): void
126 {
127 $this->fs->put($this->filepath(), $a_content);
128 }
129
133 public function getCacheSize(): string
134 {
135 $size = 0;
136 if ($this->fs->hasDir($this->basepath)) {
137 foreach ($this->fs->finder()->in([$this->basepath])->files() as $meta) {
138 $size += $this->fs->getSize($meta->getPath(), 1)->inBytes();
139 }
140 }
141
142 $type = array("K", "M", "G", "T", "P", "E", "Z", "Y");
143 $size /= 1000;
144 $counter = 0;
145 while ($size >= 1000) {
146 $size /= 1000;
147 $counter++;
148 }
149
150 return (round($size, 1) . " " . $type[$counter] . "B");
151 }
152
156 public function clearCache(): void
157 {
158 $this->fs->deleteDir($this->basepath);
159 }
160}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:32
Rendered MathJax image Supports image types SVG or PNG Files are stored in the web file system of ili...
ILIAS Filesystem Filesystem $fs
read()
Read the content of a cached image.
absolutePath()
Get the absolute path of the image.
write(string $a_content)
Save the content of a cached image.
__construct(string $a_tex, string $a_type, int $a_dpi)
exists()
Check if an image is cached.
clearCache()
Delete all files from the cache.
getCacheSize()
Get the total size of the cache with an appropriate unit for display.
filepath()
Create the relative file path of the image.
const CLIENT_WEB_DIR
Definition: constants.php:47
global $DIC
Definition: feed.php:28
$type