ILIAS  release_8 Revision v8.25-1-g13de6a5eca6
class.ilObjectTileImage.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
28
30{
32 protected int $obj_id;
33 protected Filesystem $web;
35 protected string $ext;
36
38 {
39 $this->service = $service;
40 $this->obj_id = $obj_id;
41 $this->web = $service->filesystem()->web();
42 $this->upload = $service->upload();
43 $this->ext = ilContainer::_lookupContainerSetting($obj_id, 'tile_image');
44 }
45
46 public function getExtension(): string
47 {
48 return $this->ext;
49 }
50
51 public function copy(int $target_obj_id): void
52 {
53 if (!$this->exists()) {
54 ilContainer::_deleteContainerSettings($target_obj_id, 'tile_image');
55 return;
56 }
57
58 try {
59 $this->web->copy(
60 $this->getRelativePath(),
61 preg_replace(
62 '/(' . "tile_image_" . ')(\d*)\/(.*)$/',
63 '${1}' . $target_obj_id . '/${3}',
64 $this->getRelativePath()
65 )
66 );
67
68 ilContainer::_writeContainerSetting($target_obj_id, 'tile_image', $this->getExtension());
69 } catch (Exception $e) {
70 ilContainer::_deleteContainerSettings($target_obj_id, 'tile_image');
71 }
72 }
73
74 public function delete(): void
75 {
76 if ($this->web->hasDir($this->getRelativeDirectory())) {
77 try {
78 $this->web->deleteDir($this->getRelativeDirectory());
79 } catch (Exception $e) {
80 }
81 }
82
83 ilContainer::_deleteContainerSettings($this->obj_id, 'tile_image');
84 }
85
86 public function saveFromHttpRequest(string $tmp_name): void
87 {
88 $this->createDirectory();
89
90 $file_name = $this->getRelativePath();
91 if ($this->web->has($file_name)) {
92 $this->web->delete($file_name);
93 }
94
95 if ($this->upload->hasUploads()) {
96 if (!$this->upload->hasBeenProcessed()) {
97 $this->upload->process();
98 }
99
101 $results = $this->upload->getResults();
102 if (isset($results[$tmp_name])) {
103 $result = $results[$tmp_name];
104 $this->ext = pathinfo($result->getName(), PATHINFO_EXTENSION);
105 $file_name = $this->getRelativePath();
106 if ($result->isOK()) {
107 $this->upload->moveOneFileTo(
108 $result,
109 $this->getRelativeDirectory(),
110 Location::WEB,
111 $this->getFileName(),
112 true
113 );
114
115
116 $fullpath = CLIENT_WEB_DIR . '/' . $this->getRelativeDirectory() . '/' . $this->getFileName();
117 [$width, $height, , ] = getimagesize($fullpath);
118 $min = min($width, $height);
120 $fullpath .
121 "[0] -geometry " .
122 $min .
123 "x" .
124 $min .
125 "^ -gravity center -extent " .
126 $min .
127 "x" .
128 $min .
129 " " .
130 $fullpath
131 );
132 }
133 }
134 }
135 $this->persistImageState($file_name);
136 }
137
138 protected function persistImageState(string $filename): void
139 {
140 $ext = pathinfo($filename, PATHINFO_EXTENSION);
141
142 if ($this->web->has($filename)) {
143 ilContainer::_writeContainerSetting($this->obj_id, 'tile_image', $ext);
144 } else {
145 ilContainer::_deleteContainerSettings($this->obj_id, 'tile_image');
146 }
147 }
148
152 protected function createDirectory(): void
153 {
154 $this->web->createDir($this->getRelativeDirectory());
155 }
156
157 public function getRelativeDirectory(): string
158 {
159 return implode(
160 DIRECTORY_SEPARATOR,
161 [
162 'obj_data',
163 'tile_image',
164 'tile_image_' . $this->obj_id
165 ]
166 );
167 }
168
169 protected function getFileName(): string
170 {
171 return 'tile_image.' . $this->getExtension();
172 }
173
174 protected function getRelativePath(): string
175 {
176 return implode(
177 DIRECTORY_SEPARATOR,
178 [
179 $this->getRelativeDirectory(),
180 $this->getFileName()
181 ]
182 );
183 }
184
185 public function exists(): bool
186 {
187 if (!ilContainer::_lookupContainerSetting($this->obj_id, 'tile_image', '0')) {
188 return false;
189 }
190
191 return $this->web->has($this->getRelativePath());
192 }
193
194 public function getFullPath(): string
195 {
196 // TODO: Currently there is no option to get the relative base directory of a filesystem
197 return implode(
198 DIRECTORY_SEPARATOR,
199 [
201 $this->getRelativePath()
202 ]
203 );
204 }
205
206 public function createFromImportDir(string $source_dir, string $ext): void
207 {
208 $target_dir = implode(
209 DIRECTORY_SEPARATOR,
210 [
212 $this->getRelativeDirectory()
213 ]
214 );
215 $sourceFS = LegacyPathHelper::deriveFilesystemFrom($source_dir);
216 $targetFS = LegacyPathHelper::deriveFilesystemFrom($target_dir);
217
218 $sourceDir = LegacyPathHelper::createRelativePath($source_dir);
219 $targetDir = LegacyPathHelper::createRelativePath($target_dir);
220
221
222 $sourceList = $sourceFS->listContents($sourceDir, true);
223
224 foreach ($sourceList as $item) {
225 if ($item->isDir()) {
226 continue;
227 }
228 try {
229 $itemPath = $targetDir . '/' . substr($item->getPath(), strlen($sourceDir));
230 $stream = $sourceFS->readStream($item->getPath());
231 $targetFS->writeStream($itemPath, $stream);
233 // Do nothing with that type of exception
234 }
235 }
236
237 ilContainer::_writeContainerSetting($this->obj_id, 'tile_image', $ext);
238 }
239}
$filename
Definition: buildRTE.php:78
static _writeContainerSetting(int $a_id, string $a_keyword, string $a_value)
static _lookupContainerSetting(int $a_id, string $a_keyword, string $a_default_value=null)
static _deleteContainerSettings(int $a_id, string $a_keyword="", bool $a_keyword_like=false)
static getWebspaceDir(string $mode="filesystem")
get webspace directory
createFromImportDir(string $source_dir, string $ext)
__construct(ilObjectService $service, int $obj_id)
copy(int $target_obj_id)
persistImageState(string $filename)
static execConvert(string $args)
execute convert command
const CLIENT_WEB_DIR
Definition: constants.php:47
Interface Location.
Definition: Location.php:30
Interface Filesystem.
Definition: Filesystem.php:40
saveFromHttpRequest(string $tmp_name)
Save image from request.
Class FlySystemFileAccessTest \Provider\FlySystem @runTestsInSeparateProcesses @preserveGlobalState d...
$results