ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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 ): Generator {
70 if ($rid !== "") {
71 $unzip = $this->irss->getContainerZip($rid);
72 $uri = $this->irss->stream($rid)->getMetadata("uri");
73 $zip_archive = new \ZipArchive();
74 $zip_archive->open($uri, \ZipArchive::RDONLY);
75
76 foreach ($unzip->getPaths() as $path) {
77 if (str_starts_with($path, ".")) {
78 continue;
79 }
80 if (!str_starts_with($path, "images")) {
81 continue;
82 }
83 if (!in_array(strtolower(pathinfo($path, PATHINFO_EXTENSION)), ["jpg", "png", "gif", "svg"])) {
84 continue;
85 }
86 $att = $zip_archive->statName($path);
87 if ($include_size_info) {
88 $full_path = $this->irss->getContainerUri($rid, $path);
89 try {
90 $stream = $this->irss->getStreamOfContainerEntry($rid, $path);
91 $content = $stream->getContents();
92 $image_size = getimagesizefromstring($content);
93 } catch (\Exception $e) {
94 }
95 $width = $image_size[0] ?? 0;
96 $height = $image_size[1] ?? 0;
97 } else {
98 $width = 0;
99 $height = 0;
100 }
101 yield $this->factory->image(
102 $this->irss->getContainerUri($rid, $path),
103 new DataSize($att["size"], DataSize::KB),
104 $width,
105 $height
106 );
107 }
108 }
109
110
111 $dir = $this->dir($style_id);
112 if ($this->web_files->hasDir($dir)) {
113 foreach ($this->web_files->listContents($dir) as $meta) {
114 if ($meta->isFile()) {
115 $size = $this->web_files->getSize(
116 $meta->getPath(),
118 );
119 $image_size = getimagesize($this->getWebPath($meta->getPath()));
120 $width = $image_size[0] ?? 0;
121 $height = $image_size[1] ?? 0;
122 yield $this->factory->image(
123 $meta->getPath(),
124 $size,
125 $width,
126 $height
127 );
128 }
129 }
130 }
131 }
132
133 public function getImageStream(
134 string $rid,
135 string $image
136 ): ZIPStream {
137 return $this->irss->getStreamOfContainerEntry(
138 $rid,
139 "images/" . $image
140 );
141 }
142
143 public function addStream(
144 string $rid,
145 string $image,
146 FileStream $stream
147 ): void {
148 $this->irss->addStreamToContainer(
149 $rid,
150 $stream,
151 "images/" . $image
152 );
153 }
154
155 // get full web path for relative file path
156 public function getWebPath(string $path): string
157 {
158 if (str_starts_with($path, "http")) {
159 return $path;
160 }
161 return ILIAS_WEB_DIR . "/" . CLIENT_ID . "/" . $path;
162 }
163
164 // get image data object by filename
165 public function getByFilename(int $style_id, string $rid, string $filename): ?Image
166 {
168 foreach ($this->getImages($style_id, $rid) as $i) {
169 if ($i->getFilename() == $filename) {
170 return $i;
171 }
172 }
173 return null;
174 }
175
176 // delete image
177 public function deleteImageByFilename(int $style_id, string $filename): void
178 {
179 $dir = $this->dir($style_id);
180 $this->web_files->delete($dir . "/" . $filename);
181 }
182
183 public function importFromUploadResult(
184 string $rid,
185 UploadResult $result,
186 ): void {
187 $this->irss->addUploadToContainer(
188 $rid,
189 $result
190 );
191 }
192}
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
addStream(string $rid, string $image, FileStream $stream)
getImageStream(string $rid, string $image)
getImages(int $style_id, string $rid, bool $include_size_info=false)
Get images of style.
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...
if(!file_exists('../ilias.ini.php'))