ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilLTIConsumeProviderIcon.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
30{
31 public const MAX_ICON_SIZE = 32;
32
33 protected static array $RELATIVE_DIRECTORY_PATH = [
34 'lti_data', 'provider_icon'
35 ];
36
37 protected static array $SUPPORTED_FILE_EXTENSIONS = [
38 'png', 'jpg', 'jpeg', 'svg'
39 ];
40
44 protected int $providerId;
45
49 protected string $filename;
50
55 public function __construct(int $providerId, string $filename = '')
56 {
57 global $DIC; /* @var \ILIAS\DI\Container $DIC */
58
59 $this->providerId = $providerId;
60 $this->filename = $filename;
61
63 }
64
65 public function buildFilename(string $fileExtension): string
66 {
67 return $this->providerId . "." . $fileExtension;
68 }
69
70 public function getFilename(): string
71 {
72 return $this->filename;
73 }
74
75 public function setFilename(string $filename): void
76 {
77 $this->filename = $filename;
78 }
79
80 public function getRelativeDirectory(): string
81 {
82 return implode(DIRECTORY_SEPARATOR, self::$RELATIVE_DIRECTORY_PATH);
83 }
84
85 public function getRelativeFilePath(): string
86 {
87 return implode(DIRECTORY_SEPARATOR, [
88 $this->getRelativeDirectory(), $this->getFilename()
89 ]);
90 }
91
92 public function getAbsoluteFilePath(): string
93 {
94 return implode(DIRECTORY_SEPARATOR, [
96 ]);
97 }
98
102 protected function ensureExistingRelativeDirectory(): void
103 {
104 global $DIC; /* @var \ILIAS\DI\Container $DIC */
105
106 if (!$DIC->filesystem()->web()->has($this->getRelativeDirectory())) {
107 $DIC->filesystem()->web()->createDir($this->getRelativeDirectory());
108 }
109 }
110
111 public function exists(): bool
112 {
113 global $DIC; /* @var \ILIAS\DI\Container $DIC */
114
115 if ($this->getFilename() === '') {
116 return false;
117 }
118
119 return $DIC->filesystem()->web()->has($this->getRelativeFilePath());
120 }
121
126 protected function delete(): void
127 {
128 global $DIC; /* @var \ILIAS\DI\Container $DIC */
129
130 if ($DIC->filesystem()->web()->has($this->getRelativeFilePath())) {
131 $DIC->filesystem()->web()->delete($this->getRelativeFilePath());
132 }
133
134 $this->setFilename('');
135 }
136
137 protected function convert(): void
138 {
139 // convert to square with same side length (optimal for tile view)
140
141 list($width, $height, $type, $attr) = getimagesize($this->getAbsoluteFilePath());
142 $minSize = min($width, $height);
143
144 if (self::MAX_ICON_SIZE) {
145 $minSize = min($minSize, self::MAX_ICON_SIZE);
146 }
147
148 $convertCmd = "{$this->getAbsoluteFilePath()}[0]";
149 $convertCmd .= " -geometry {$minSize}x{$minSize}^ -gravity center";
150 $convertCmd .= " -extent {$minSize}x{$minSize}";
151 $convertCmd .= " {$this->getAbsoluteFilePath()}";
152
153 ilShellUtil::execConvert($convertCmd);
154 }
155
161 protected function save(string $uploadFile): void
162 {
163 global $DIC; /* @var \ILIAS\DI\Container $DIC */
164
165 if ($DIC->upload()->hasUploads()) {
166 if (!$DIC->upload()->hasBeenProcessed()) {
167 $DIC->upload()->process();
168 }
169
170 /* @var \ILIAS\FileUpload\DTO\UploadResult $result */
171
172 $results = $DIC->upload()->getResults();
173
174 if (isset($results[$uploadFile])) {
175 $result = $results[$uploadFile];
176
177 if ($result->isOK()) {
178 $fileExtentsion = pathinfo($result->getName(), PATHINFO_EXTENSION);
179 $this->setFilename($this->buildFilename($fileExtentsion));
180
181 $DIC->upload()->moveOneFileTo(
182 $result,
183 $this->getRelativeDirectory(),
184 \ILIAS\FileUpload\Location::WEB,
185 $this->getFileName(),
186 true
187 );
188
189 if ($fileExtentsion !== "svg") {
190 $this->convert();
191 }
192 }
193 }
194 }
195 }
196
202 public function handleUploadInputSubission(ilImageFileInputGUI $fileInput, array $fileData): void
203 {
204 global $DIC;
205
206 if ($fileInput->getDeletionFlag()) {
207 $this->delete();
208 }
209
210 if ($fileData['tmp_name']) {
211 $this->save($fileData['tmp_name']);
212 }
213 }
214
218 public static function getSupportedFileExtensions(): array
219 {
221 }
222}
static getWebspaceDir(string $mode="filesystem")
get webspace directory
This class represents an image file property in a property form.
__construct(int $providerId, string $filename='')
ilLTIConsumeProviderIcon constructor.
handleUploadInputSubission(ilImageFileInputGUI $fileInput, array $fileData)
static execConvert(string $args)
execute convert command
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
$results
global $DIC
Definition: shib_login.php:26