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