ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilMathJaxImage.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 
31 {
32  private const TYPE_PNG = 'png';
33  private const TYPE_SVG = 'svg';
34 
39  protected Filesystem $fs;
40 
44  protected string $basepath = '/temp/tex';
45 
49  protected string $tex;
50 
54  protected string $suffix;
55 
59  protected string $salt;
60 
66  public function __construct(string $a_tex, string $a_type, int $a_dpi)
67  {
68  global $DIC;
69 
70  $this->fs = $DIC->filesystem()->web();
71  $this->tex = $a_tex;
72 
73  switch ($a_type) {
74  case self::TYPE_PNG:
75  $this->suffix = '.png';
76  break;
77  case self::TYPE_SVG:
78  $this->suffix = '.svg';
79  break;
80  default:
81  throw new ilMathJaxException('imagetype not supported');
82  }
83 
84  $this->salt = '#' . $a_dpi;
85  }
86 
90  protected function filedir(): string
91  {
92  $hash = md5($this->tex . $this->salt);
93  return $this->basepath
94  . '/' . substr($hash, 0, 4)
95  . '/' . substr($hash, 4, 4);
96  }
97 
101  protected function filepath(): string
102  {
103  $hash = md5($this->tex . $this->salt);
104  return $this->filedir() . '/' . $hash . $this->suffix;
105  }
106 
110  public function absolutePath(): string
111  {
112  return CLIENT_WEB_DIR . $this->filepath();
113  }
114 
118  public function exists(): bool
119  {
120  return $this->fs->has($this->filepath());
121  }
122 
126  public function read(): string
127  {
128  return $this->fs->read($this->filepath());
129  }
130 
135  public function write(string $a_content): void
136  {
137  // set the directory access of the whole relative file to visible
138  // this is needed if TeX is used in certificates
139  // the ILIAS java server must have read access to the files for the PDF generation
140  // it may run with a different user account
141  $dir = '';
142  foreach (explode('/', $this->filedir()) as $part) {
143  if (!empty($part)) {
144  $dir = $dir . '/' . $part;
145  }
146  if (!$this->fs->hasDir($dir)) {
147  $this->fs->createDir($dir, Visibility::PUBLIC_ACCESS);
148  } else {
149  $this->fs->setVisibility($dir, Visibility::PUBLIC_ACCESS);
150  }
151  }
152  $this->fs->put($this->filepath(), $a_content);
153  }
154 
158  public function getCacheSize(): string
159  {
160  $size = 0;
161  if ($this->fs->hasDir($this->basepath)) {
162  foreach ($this->fs->finder()->in([$this->basepath])->files() as $meta) {
163  $size += $this->fs->getSize($meta->getPath(), 1)->inBytes();
164  }
165  }
166 
167  $type = array("K", "M", "G", "T", "P", "E", "Z", "Y");
168  $size /= 1000;
169  $counter = 0;
170  while ($size >= 1000) {
171  $size /= 1000;
172  $counter++;
173  }
174 
175  return (round($size, 1) . " " . $type[$counter] . "B");
176  }
177 
181  public function clearCache(): void
182  {
183  $this->fs->deleteDir($this->basepath);
184  }
185 }
Rendered MathJax image Supports image types SVG or PNG Files are stored in the web file system of ili...
filepath()
Get the relative file path of the image.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
absolutePath()
Get the absolute path of the image.
clearCache()
Delete all files from the cache.
getCacheSize()
Get the total size of the cache with an appropriate unit for display.
read()
Read the content of a cached image.
global $DIC
Definition: shib_login.php:22
const CLIENT_WEB_DIR
Definition: constants.php:47
__construct(string $a_tex, string $a_type, int $a_dpi)
exists()
Check if an image is cached.
filedir()
Get the relative directory path of the image.
write(string $a_content)
Save the content of a cached image.