ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
ThumbsManager.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
32
34{
35 protected \ILIAS\MediaObjects\MediaObjectRepository $repo;
36 protected \ILIAS\MediaObjects\MediaObjectManager $media_manager;
39
40 public function __construct(
41 protected InternalDataService $data,
43 protected InternalDomainService $domain
44 ) {
45 $this->media_manager = $this->domain->mediaObject();
46 $this->image_converters = new Images(true);
47 $this->output_options = new ImageOutputOptions();
48 $this->repo = $repo->mediaObject();
49 }
50
51 protected function getThumbPath(): string
52 {
53 return "thumbs/Standard.png";
54 }
55
59 public function getThumbSrc(int $mob_id): string
60 {
61 $mob = new \ilObjMediaObject($mob_id);
62 $item = $mob->getMediaItem("Standard");
63 // for svg return standard src
64 if ($item?->getFormat() === "image/svg+xml") {
65 return $mob->getStandardSrc();
66 }
67
68 // if thumb exists -> return
69 $thumb_path = $this->getThumbPath();
70 if ($this->media_manager->hasLocalFile($mob_id, $thumb_path)) {
71 return $this->media_manager->getLocalSrc($mob_id, $thumb_path);
72 }
73
74 // if preview exists -> return
75 $preview_src = $this->getPreviewSrc($mob_id);
76 if ($preview_src !== "") {
77 return $preview_src;
78 }
79
80 // if not tried already, create thumb and return
81 if ($item?->getLocationType() === "LocalFile") {
82 if ($item?->getThumbTried() !== "y") {
83 $this->createThumb(
84 $mob_id,
85 $item?->getLocation(),
86 $item?->getFormat(),
87 $thumb_path
88 );
89 $item?->writeThumbTried("y");
90 if ($this->media_manager->hasLocalFile($mob_id, $thumb_path)) {
91 return $this->media_manager->getLocalSrc($mob_id, $thumb_path);
92 }
93 }
94 } else {
95 if (str_starts_with($item?->getFormat(), "image/")) {
96 return $item?->getLocation();
97 }
98 }
99
100 // send generic thumb src
101 return \ilUtil::getImagePath("standard/icon_mob.svg");
102 }
103
104 protected function createThumb(
105 int $mob_id,
106 string $location,
107 string $format,
108 string $target_location,
109 ): void {
110 $is_image = is_int(strpos($format, "image/"));
111 if ($is_image) {
112 if (!$this->media_manager->hasLocalFile($mob_id, $location)) {
113 return;
114 }
115 $width = $height = \ilObjMediaObject::DEFAULT_THUMB_SIZE;
116 $image_quality = 90;
117
118 // the zip stream is not seekable, which is needed by Imagick
119 // so we create a seekable stream first
120 $tempStream = fopen('php://temp', 'w+');
121 stream_copy_to_stream($this->media_manager->getLocationStream($mob_id, $location)->detach(), $tempStream);
122 rewind($tempStream);
123 $stream = new Stream($tempStream);
124
125 $converter = $this->resizeToFixedSize(
126 $stream,
127 $width,
128 $height,
129 true,
130 $this->output_options
131 ->withQuality($image_quality)
133 );
134 $this->media_manager->addStream(
135 $mob_id,
136 $target_location,
137 $converter->getStream()
138 );
139 fclose($tempStream);
140 }
141 }
142
143 public function createPreview(
144 int $mob_id,
145 string $location,
146 bool $local,
147 string $format,
148 int $sec = 1,
149 string $target_location = "mob_vpreview.png"
150 ): void {
151
152 $is_image = is_int(strpos($format, "image/"));
153 $is_video = in_array($format, ["video/mp4", "video/webm"]);
154
155 if ($local) {
156 if ($is_image) {
158 $image_quality = 60;
159
160 // the zip stream is not seekable, which is needed by Imagick
161 // so we create a seekable stream first
162 $tempStream = fopen('php://temp', 'w+');
163 stream_copy_to_stream($this->repo->getLocationStream($mob_id, $location)->detach(), $tempStream);
164 rewind($tempStream);
165 $stream = new Stream($tempStream);
166
167 $converter = $this->resizeToFixedSize(
168 $stream,
169 $width,
170 $height,
171 true,
172 $this->output_options
173 ->withQuality($image_quality)
175 );
176 $this->repo->addStream(
177 $mob_id,
178 $target_location,
179 $converter->getStream()
180 );
181 fclose($tempStream);
182 }
183 if ($is_video) {
184 $zip_uri = $this->repo->getContainerPath($mob_id);
186 $zip_uri,
187 $location,
188 $sec
189 );
190 $png_res = fopen('php://memory', 'r+');
191 fwrite($png_res, $image_str);
192 rewind($png_res);
193 $png_stream = new Stream($png_res);
194 $this->repo->addStream(
195 $mob_id,
196 $target_location,
197 $png_stream
198 );
199 }
200 }
201 }
202
203
204 protected function resizeToFixedSize(
205 Stream $stream,
206 int $width,
207 int $height,
208 bool $crop_if_true_and_resize_if_false,
209 ImageOutputOptions $output_options
210 ): ImageConverter {
211 $conversion_options = (new ImageConversionOptions())
212 ->withMakeTemporaryFiles(false)
213 ->withThrowOnError(false)
214 ->withBackgroundColor('#FFFFFF');
215
216 return new ImageConverter(
217 $conversion_options
218 ->withWidth($width)
219 ->withHeight($height)
220 ->withCrop($crop_if_true_and_resize_if_false)
221 ->withKeepAspectRatio(true),
222 $output_options,
223 $stream
224 );
225 }
226
227
231 public function getPreviewSrc(int $mob_id): string
232 {
233 $ppics = array(
234 "mob_vpreview.png",
235 "mob_vpreview.jpg",
236 "mob_vpreview.jpeg");
237 foreach ($ppics as $pic) {
238 if ($this->media_manager->hasLocalFile($mob_id, $pic)) {
239 return $this->media_manager->getLocalSrc($mob_id, $pic);
240 }
241 }
242 return "";
243 }
244}
$location
Definition: buildRTE.php:22
__construct(protected InternalDataService $data, InternalRepoService $repo, protected InternalDomainService $domain)
createPreview(int $mob_id, string $location, bool $local, string $format, int $sec=1, string $target_location="mob_vpreview.png")
getThumbSrc(int $mob_id)
For use in browser src of images.
createThumb(int $mob_id, string $location, string $format, string $target_location,)
resizeToFixedSize(Stream $stream, int $width, int $height, bool $crop_if_true_and_resize_if_false, ImageOutputOptions $output_options)
ILIAS MediaObjects MediaObjectRepository $repo
getPreviewSrc(int $mob_id)
For use in browser src of images.
ILIAS MediaObjects MediaObjectManager $media_manager
static extractPNGFromVideoInZip(string $zip, string $path, int $sec=1)