ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ImageFileRepo.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Style\Content;
22
26use Generator;
33
35{
36 protected const DIR_PATH = "sty/sty_%id%/images";
37
41
42 public function __construct(
46 protected IRSSWrapper $irss
47 ) {
48 $this->web_files = $web_files;
49 $this->factory = $factory;
50 $this->upload = $upload;
51 }
52
53 // get image directory
54 protected function dir(int $style_id): string
55 {
56 return str_replace("%id%", (string) $style_id, self::DIR_PATH);
57 }
58
65 public function getImages(
66 int $style_id,
67 string $rid,
68 bool $include_size_info = false,
69 bool $include_legacy_dir = true
70 ): Generator {
71 $has_images = false;
72 if ($rid !== "") {
73 $unzip = $this->irss->getContainerZip($rid);
74 $uri = $this->irss->stream($rid)->getMetadata("uri");
75 $zip_archive = new \ZipArchive();
76 $zip_archive->open($uri, \ZipArchive::RDONLY);
77
78 foreach ($unzip->getPaths() as $path) {
79 if (str_starts_with($path, ".")) {
80 continue;
81 }
82 if (!str_starts_with($path, "images")) {
83 continue;
84 }
85 if (!in_array(strtolower(pathinfo($path, PATHINFO_EXTENSION)), ["jpg", "png", "gif", "svg"])) {
86 continue;
87 }
88 $att = $zip_archive->statName($path);
89 if ($include_size_info) {
90 $full_path = $this->irss->getContainerUri($rid, $path);
91 try {
92 $stream = $this->irss->getStreamOfContainerEntry($rid, $path);
93 $content = $stream->getContents();
94 $image_size = getimagesizefromstring($content);
95 } catch (\Exception $e) {
96 }
97 $width = $image_size[0] ?? 0;
98 $height = $image_size[1] ?? 0;
99 } else {
100 $width = 0;
101 $height = 0;
102 }
103 $has_images = true;
104 yield $this->factory->image(
105 $this->irss->getContainerUri($rid, $path),
106 new DataSize($att["size"], DataSize::KB),
107 $width,
108 $height
109 );
110 }
111 }
112
113 if ($has_images || !$include_legacy_dir) {
114 return;
115 }
116
117 $dir = $this->dir($style_id);
118 if ($this->web_files->hasDir($dir)) {
119 foreach ($this->web_files->listContents($dir) as $meta) {
120 if ($meta->isFile()) {
121 $size = $this->web_files->getSize(
122 $meta->getPath(),
124 );
125 $image_size = getimagesize($this->getWebPath($meta->getPath()));
126 $width = $image_size[0] ?? 0;
127 $height = $image_size[1] ?? 0;
128 yield $this->factory->image(
129 $meta->getPath(),
130 $size,
131 $width,
132 $height
133 );
134 }
135 }
136 }
137 }
138
139 public function hasLegacyDir(
140 int $style_id
141 ): bool {
142 $dir = $this->dir($style_id);
143 if ($this->web_files->hasDir($dir)) {
144 return true;
145 }
146 return false;
147 }
148
149 public function hasImages(
150 int $style_id,
151 string $rid
152 ): bool {
153 $images = iterator_to_array($this->getImages($style_id, $rid, false, false));
154 return count($images) > 0;
155 }
156
157 public function getImageStream(
158 string $rid,
159 string $image
160 ): ZIPStream {
161 return $this->irss->getStreamOfContainerEntry(
162 $rid,
163 "images/" . $image
164 );
165 }
166
167 public function addStream(
168 string $rid,
169 string $image,
170 FileStream $stream
171 ): void {
172 $this->irss->addStreamToContainer(
173 $rid,
174 $stream,
175 "images/" . $image
176 );
177 }
178
179 // get full web path for relative file path
180 public function getWebPath(string $path): string
181 {
182 if (str_starts_with($path, "http")) {
183 return $path;
184 }
185 return ILIAS_WEB_DIR . "/" . CLIENT_ID . "/" . $path;
186 }
187
188 // get image data object by filename
189 public function getByFilename(int $style_id, string $rid, string $filename): ?Image
190 {
192 foreach ($this->getImages($style_id, $rid) as $i) {
193 if ($i->getFilename() == $filename) {
194 return $i;
195 }
196 }
197 return null;
198 }
199
200 // delete image
201 public function deleteImageByFilename(int $style_id, string $filename): void
202 {
203 $dir = $this->dir($style_id);
204 $this->web_files->delete($dir . "/" . $filename);
205 }
206
207 public function importFromUploadResult(
208 string $rid,
209 UploadResult $result,
210 ): void {
211 $this->irss->addUploadToContainer(
212 $rid,
213 $result
214 );
215 }
216}
factory()
$filename
Definition: buildRTE.php:78
This class provides the data size with additional information to remove the work to calculate the siz...
Definition: DataSize.php:31
getImages(int $style_id, string $rid, bool $include_size_info=false, bool $include_legacy_dir=true)
Get images of style.
addStream(string $rid, string $image, FileStream $stream)
hasImages(int $style_id, string $rid)
getImageStream(string $rid, string $image)
deleteImageByFilename(int $style_id, string $filename)
importFromUploadResult(string $rid, UploadResult $result,)
__construct(InternalDataService $factory, Filesystem\Filesystem $web_files, FileUpload $upload, protected IRSSWrapper $irss)
const CLIENT_ID
Definition: constants.php:41
const ILIAS_WEB_DIR
Definition: constants.php:45
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface Location.
Definition: Location.php:33
The filesystem interface provides the public interface for the Filesystem service API consumer.
Definition: Filesystem.php:37
The base interface for all filesystem streams.
Definition: FileStream.php:32
$path
Definition: ltiservices.php:30
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...