ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ThumbsManager.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 
31 {
32  protected \ILIAS\MediaObjects\MediaObjectManager $media_manager;
35 
36  public function __construct(
37  protected InternalDataService $data,
38  protected InternalDomainService $domain
39  ) {
40  $this->media_manager = $this->domain->mediaObject();
41  $this->image_converters = new Images(true);
42  $this->output_options = new ImageOutputOptions();
43  }
44 
45  protected function getThumbPath(): string
46  {
47  return "thumbs/Standard.png";
48  }
49 
53  public function getThumbSrc(int $mob_id): string
54  {
55  $mob = new \ilObjMediaObject($mob_id);
56  $item = $mob->getMediaItem("Standard");
57  // for svg return standard src
58  if ($item?->getFormat() === "image/svg+xml") {
59  return $mob->getStandardSrc();
60  }
61 
62  // if thumb exists -> return
63  $thumb_path = $this->getThumbPath();
64  if ($this->media_manager->hasLocalFile($mob_id, $thumb_path)) {
65  return $this->media_manager->getLocalSrc($mob_id, $thumb_path);
66  }
67 
68  // if preview exists -> return
69  $preview_src = $this->getPreviewSrc($mob_id);
70  if ($preview_src !== "") {
71  return $preview_src;
72  }
73 
74  // if not tried already, create thumb and return
75  if ($item?->getLocationType() === "LocalFile") {
76  if ($item?->getThumbTried() !== "y") {
77  $this->createThumb(
78  $mob_id,
79  $item?->getLocation(),
80  $item?->getFormat(),
81  $thumb_path
82  );
83  $item?->writeThumbTried("y");
84  if ($this->media_manager->hasLocalFile($mob_id, $thumb_path)) {
85  return $this->media_manager->getLocalSrc($mob_id, $thumb_path);
86  }
87  }
88  } else {
89  if (str_starts_with($item?->getFormat(), "image/")) {
90  return $item?->getLocation();
91  }
92  }
93 
94  // send generic thumb src
95  return \ilUtil::getImagePath("standard/icon_mob.svg");
96  }
97 
98  protected function createThumb(
99  int $mob_id,
100  string $location,
101  string $format,
102  string $target_location,
103  ): void {
104  $is_image = is_int(strpos($format, "image/"));
105  if ($is_image) {
106  if (!$this->media_manager->hasLocalFile($mob_id, $location)) {
107  return;
108  }
109  $width = $height = \ilObjMediaObject::DEFAULT_PREVIEW_SIZE;
110  $image_quality = 90;
111 
112  // the zip stream is not seekable, which is needed by Imagick
113  // so we create a seekable stream first
114  $tempStream = fopen('php://temp', 'w+');
115  stream_copy_to_stream($this->media_manager->getLocationStream($mob_id, $location)->detach(), $tempStream);
116  rewind($tempStream);
117  $stream = new Stream($tempStream);
118 
119  $converter = $this->image_converters->resizeToFixedSize(
120  $stream,
121  $width,
122  $height,
123  true,
124  $this->output_options
125  ->withQuality($image_quality)
126  ->withFormat(ImageOutputOptions::FORMAT_PNG)
127  );
128  $this->media_manager->addStream(
129  $mob_id,
130  $target_location,
131  $converter->getStream()
132  );
133  fclose($tempStream);
134  }
135  }
136 
140  public function getPreviewSrc(int $mob_id): string
141  {
142  $ppics = array(
143  "mob_vpreview.png",
144  "mob_vpreview.jpg",
145  "mob_vpreview.jpeg");
146  foreach ($ppics as $pic) {
147  if ($this->media_manager->hasLocalFile($mob_id, $pic)) {
148  return $this->media_manager->getLocalSrc($mob_id, $pic);
149  }
150  }
151  return "";
152  }
153 }
$location
Definition: buildRTE.php:22
getThumbSrc(int $mob_id)
For use in browser src of images.
getPreviewSrc(int $mob_id)
For use in browser src of images.
__construct(protected InternalDataService $data, protected InternalDomainService $domain)
ILIAS MediaObjects MediaObjectManager $media_manager
createThumb(int $mob_id, string $location, string $format, string $target_location,)