ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMediaItem.php
Go to the documentation of this file.
1 <?php
2 
25 {
26  protected string $tried_thumb = "";
27  protected string $text_representation = "";
28  protected ilDBInterface $db;
29  protected ilLanguage $lng;
30  protected \ILIAS\Filesystem\Util\Convert\LegacyImages $image_converter;
31 
32  public int $id = 0;
33  public string $purpose = "";
34  public string $location = "";
35  public string $location_type = "";
36  public string $format = "";
37  public string $width = "";
38  public string $height = "";
39  public string $caption = "";
40  public string $halign = "";
41  public array $parameters = [];
42  public int $mob_id = 0;
43  public int $nr = 0;
44  public array $mapareas = [];
45  public int $map_cnt = 0;
49  public $map_image = null; // image map work copy image
50  public int $color1; // map area line color 1
51  public int $color2; // map area line color 2
52  protected int $duration = 0;
53  protected string $upload_hash = '';
54 
55  public function __construct(
56  int $a_id = 0
57  ) {
58  global $DIC;
59 
60  $this->db = $DIC->database();
61  $this->lng = $DIC->language();
62  $this->image_converter = $DIC->fileConverters()->legacyImages();
63  $this->parameters = array();
64  $this->mapareas = array();
65  $this->map_cnt = 0;
66 
67  if ($a_id != 0) {
68  $this->setId($a_id);
69  $this->read();
70  }
71  }
72 
76  public function setId(int $a_id): void
77  {
78  $this->id = $a_id;
79  }
80 
81  public function getId(): int
82  {
83  return $this->id;
84  }
85 
89  public function setMobId(int $a_mob_id): void
90  {
91  $this->mob_id = $a_mob_id;
92  }
93 
94  public function getMobId(): int
95  {
96  return $this->mob_id;
97  }
98 
102  public function setNr(int $a_nr): void
103  {
104  $this->nr = $a_nr;
105  }
106 
107  public function getNr(): int
108  {
109  return $this->nr;
110  }
111 
119  private static function getGDSupportedImageType(string $a_desired_type): string
120  {
121  $a_desired_type = strtolower($a_desired_type);
122  // get supported Image Types
123  $im_types = ImageTypes();
124 
125  switch ($a_desired_type) {
126  case "jpg":
127  case "jpeg":
128  if ($im_types & IMG_JPG) {
129  return "jpg";
130  }
131  if ($im_types & IMG_GIF) {
132  return "gif";
133  }
134  if ($im_types & IMG_PNG) {
135  return "png";
136  }
137  break;
138 
139  case "gif":
140  if ($im_types & IMG_GIF) {
141  return "gif";
142  }
143  if ($im_types & IMG_JPG) {
144  return "jpg";
145  }
146  if ($im_types & IMG_PNG) {
147  return "png";
148  }
149  break;
150 
151  case "svg":
152  case "png":
153  if ($im_types & IMG_PNG) {
154  return "png";
155  }
156  if ($im_types & IMG_JPG) {
157  return "jpg";
158  }
159  if ($im_types & IMG_GIF) {
160  return "gif";
161  }
162  break;
163  }
164 
165  return "";
166  }
167 
168  public function setDuration(int $a_val): void
169  {
170  $this->duration = $a_val;
171  }
172 
173  public function getDuration(): int
174  {
175  return $this->duration;
176  }
177 
178  public function setTextRepresentation(string $a_val): void
179  {
180  $this->text_representation = $a_val;
181  }
182 
183  public function getTextRepresentation(): string
184  {
186  }
187 
188  public function setUploadHash(string $a_val): void
189  {
190  $this->upload_hash = $a_val;
191  }
192 
193  public function getUploadHash(): string
194  {
195  return $this->upload_hash;
196  }
197 
198  public function create(): void
199  {
200  $ilDB = $this->db;
201 
202  $item_id = $ilDB->nextId("media_item");
203  $query = "INSERT INTO media_item (id,mob_id, purpose, location, " .
204  "location_type, format, width, " .
205  "height, halign, caption, nr, text_representation, upload_hash, duration) VALUES " .
206  "(" .
207  $ilDB->quote($item_id, "integer") . "," .
208  $ilDB->quote($this->getMobId(), "integer") . "," .
209  $ilDB->quote($this->getPurpose(), "text") . "," .
210  $ilDB->quote($this->getLocation(), "text") . "," .
211  $ilDB->quote($this->getLocationType(), "text") . "," .
212  $ilDB->quote($this->getFormat(), "text") . "," .
213  $ilDB->quote($this->getWidth(), "text") . "," .
214  $ilDB->quote($this->getHeight(), "text") . "," .
215  $ilDB->quote($this->getHAlign(), "text") . "," .
216  $ilDB->quote($this->getCaption(), "text") . "," .
217  $ilDB->quote($this->getNr(), "integer") . "," .
218  $ilDB->quote($this->getTextRepresentation(), "text") . "," .
219  $ilDB->quote($this->getUploadHash(), "text") . "," .
220  $ilDB->quote($this->getDuration(), "integer") .
221  ")";
222  $ilDB->manipulate($query);
223 
224  $this->setId($item_id);
225 
226  // create mob parameters
227  $params = $this->getParameters();
228  foreach ($params as $name => $value) {
229  $query = "INSERT INTO mob_parameter (med_item_id, name, value) VALUES " .
230  "(" . $ilDB->quote($item_id, "integer") . "," .
231  $ilDB->quote($name, "text") . "," .
232  $ilDB->quote($value, "text") . ")";
233  $ilDB->manipulate($query);
234  }
235 
236  // create map areas
237  for ($i = 0; $i < count($this->mapareas); $i++) {
238  if (is_object($this->mapareas[$i])) {
239  $this->mapareas[$i]->setItemId($this->getId());
240  $this->mapareas[$i]->setNr($i + 1);
241  $this->mapareas[$i]->create();
242  }
243  }
244  }
245 
246  public function update(): void
247  {
248  $ilDB = $this->db;
249 
250  $query = "UPDATE media_item SET " .
251  " mob_id = " . $ilDB->quote($this->getMobId(), "integer") . "," .
252  " purpose = " . $ilDB->quote($this->getPurpose(), "text") . "," .
253  " location = " . $ilDB->quote($this->getLocation(), "text") . "," .
254  " location_type = " . $ilDB->quote($this->getLocationType(), "text") . "," .
255  " format = " . $ilDB->quote($this->getFormat(), "text") . "," .
256  " width = " . $ilDB->quote($this->getWidth(), "text") . "," .
257  " height = " . $ilDB->quote($this->getHeight(), "text") . "," .
258  " halign = " . $ilDB->quote($this->getHAlign(), "text") . "," .
259  " caption = " . $ilDB->quote($this->getCaption(), "text") . "," .
260  " nr = " . $ilDB->quote($this->getNr(), "integer") . "," .
261  " text_representation = " . $ilDB->quote($this->getTextRepresentation(), "text") . "," .
262  " upload_hash = " . $ilDB->quote($this->getUploadHash(), "text") . "," .
263  " duration = " . $ilDB->quote($this->getDuration(), "integer") .
264  " WHERE id = " . $ilDB->quote($this->getId(), "integer");
265  $ilDB->manipulate($query);
266 
267  // delete mob parameters
268  $query = "DELETE FROM mob_parameter WHERE med_item_id = " .
269  $ilDB->quote($this->getId(), "integer");
270  $ilDB->manipulate($query);
271 
272  // create mob parameters
273  $params = $this->getParameters();
274  foreach ($params as $name => $value) {
275  $query = "INSERT INTO mob_parameter (med_item_id, name, value) VALUES " .
276  "(" . $ilDB->quote($this->getId(), "integer") . "," .
277  $ilDB->quote($name, "text") . "," .
278  $ilDB->quote($value, "text") . ")";
279  $ilDB->manipulate($query);
280  }
281  }
282 
283  public function writeParameter(
284  string $a_name,
285  string $a_value
286  ): void {
287  $ilDB = $this->db;
288 
289  $query = "INSERT INTO mob_parameter (med_item_id, name, value) VALUES " .
290  "(" . $ilDB->quote($this->getId(), "integer") . "," .
291  $ilDB->quote($a_name, "text") . "," .
292  $ilDB->quote($a_value, "text") . ")";
293  $ilDB->manipulate($query);
294  }
295 
299  public function read(): void
300  {
301  $ilDB = $this->db;
302 
303  $item_id = $this->getId();
304  $mob_id = $this->getMobId();
305  $nr = $this->getNr();
306  $query = "";
307  if ($item_id > 0) {
308  $query = "SELECT * FROM media_item WHERE id = " .
309  $ilDB->quote($this->getId(), "integer");
310  } elseif ($mob_id > 0 && $nr > 0) {
311  $query = "SELECT * FROM media_item WHERE mob_id = " .
312  $ilDB->quote($this->getMobId(), "integer") . " " .
313  "AND nr=" . $ilDB->quote($this->getNr(), "integer");
314  }
315  if ($query != "") {
316  $item_set = $ilDB->query($query);
317  $item_rec = $ilDB->fetchAssoc($item_set);
318 
319  $this->setLocation((string) $item_rec["location"]);
320  $this->setLocationType((string) $item_rec["location_type"]);
321  $this->setFormat((string) $item_rec["format"]);
322  $this->setWidth((string) $item_rec["width"]);
323  $this->setHeight((string) $item_rec["height"]);
324  $this->setHAlign((string) $item_rec["halign"]);
325  $this->setCaption((string) $item_rec["caption"]);
326  $this->setPurpose((string) $item_rec["purpose"]);
327  $this->setNr((int) $item_rec["nr"]);
328  $this->setMobId((int) $item_rec["mob_id"]);
329  $this->setId((int) $item_rec["id"]);
330  $this->setThumbTried((string) $item_rec["tried_thumb"]);
331  $this->setTextRepresentation((string) $item_rec["text_representation"]);
332  $this->setUploadHash((string) $item_rec["upload_hash"]);
333  $this->setDuration((int) $item_rec["duration"]);
334 
335  // get item parameter
336  $query = "SELECT * FROM mob_parameter WHERE med_item_id = " .
337  $ilDB->quote($this->getId(), "integer");
338  $par_set = $ilDB->query($query);
339  while ($par_rec = $ilDB->fetchAssoc($par_set)) {
340  $this->setParameter($par_rec["name"], $par_rec["value"]);
341  }
342 
343  // get item map areas
344  $max = ilMapArea::_getMaxNr($this->getId());
345  for ($i = 1; $i <= $max; $i++) {
346  $area = new ilMapArea($this->getId(), $i);
347  $this->addMapArea($area);
348  }
349  }
350  }
351 
355  public function writeThumbTried(string $a_tried): void
356  {
357  $ilDB = $this->db;
358 
359  $q = "UPDATE media_item SET tried_thumb = " .
360  $ilDB->quote($a_tried, "text") .
361  " WHERE id = " . $ilDB->quote($this->getId(), "integer");
362 
363  $ilDB->manipulate($q);
364  }
365 
366  public static function _lookupLocationForMobId(
367  int $a_mob_id,
368  string $a_purpose
369  ): string {
370  global $DIC;
371 
372  $ilDB = $DIC->database();
373 
374  // read media_object record
375  $query = "SELECT * FROM media_item WHERE mob_id = " .
376  $ilDB->quote($a_mob_id, "integer") . " " .
377  "AND purpose = " . $ilDB->quote($a_purpose, "text");
378  $set = $ilDB->query($query);
379  if ($rec = $ilDB->fetchAssoc($set)) {
380  return $rec["location"];
381  }
382 
383  return "";
384  }
385 
386  public static function _lookupMobId(
387  int $a_med_id
388  ): int {
389  global $DIC;
390 
391  $ilDB = $DIC->database();
392 
393  // read media_object record
394  $query = "SELECT * FROM media_item WHERE id = " .
395  $ilDB->quote($a_med_id, "integer");
396  $set = $ilDB->query($query);
397  if ($rec = $ilDB->fetchAssoc($set)) {
398  return (int) $rec["mob_id"];
399  }
400 
401  return 0;
402  }
403 
410  public static function _getMediaItemsOfMObId(
411  int $a_mobId,
412  string $a_purpose
413  ): ?array {
414  global $DIC;
415 
416  $ilDB = $DIC->database();
417 
418  // read media_object record
419  $query = "SELECT * FROM media_item WHERE mob_id = " .
420  $ilDB->quote($a_mobId, "integer") . " " .
421  "AND purpose=" . $ilDB->quote($a_purpose, "text") . " ORDER BY nr";
422  $item_set = $ilDB->query($query);
423 
424  while ($item_rec = $ilDB->fetchAssoc($item_set)) {
425  return $item_rec;
426  }
427  return null;
428  }
429 
433  public static function _getMediaItemsOfMOb(
434  ilObjMediaObject $a_mob
435  ): void {
436  global $DIC;
437 
438  $ilDB = $DIC->database();
439 
440  // read media_object record
441  $query = "SELECT * FROM media_item WHERE mob_id = " .
442  $ilDB->quote($a_mob->getId(), "integer") . " " .
443  "ORDER BY nr";
444  $item_set = $ilDB->query($query);
445  while ($item_rec = $ilDB->fetchAssoc($item_set)) {
446  $media_item = new ilMediaItem();
447  $media_item->setNr((int) $item_rec["nr"]);
448  $media_item->setId((int) $item_rec["id"]);
449  $media_item->setLocation((string) $item_rec["location"]);
450  $media_item->setLocationType((string) $item_rec["location_type"]);
451  $media_item->setFormat((string) $item_rec["format"]);
452  $media_item->setWidth((string) $item_rec["width"]);
453  $media_item->setHeight((string) $item_rec["height"]);
454  $media_item->setHAlign((string) $item_rec["halign"]);
455  $media_item->setCaption((string) $item_rec["caption"]);
456  $media_item->setPurpose((string) $item_rec["purpose"]);
457  $media_item->setMobId((int) $item_rec["mob_id"]);
458  $media_item->setThumbTried((string) $item_rec["tried_thumb"]);
459  $media_item->setTextRepresentation((string) $item_rec["text_representation"]);
460  $media_item->setUploadHash((string) $item_rec["upload_hash"]);
461  $media_item->setDuration((int) $item_rec["duration"]);
462 
463  // get item parameter
464  $query = "SELECT * FROM mob_parameter WHERE med_item_id = " .
465  $ilDB->quote($item_rec["id"], "integer");
466  $par_set = $ilDB->query($query);
467  while ($par_rec = $ilDB->fetchAssoc($par_set)) {
468  $media_item->setParameter($par_rec["name"], $par_rec["value"]);
469  }
470 
471  // get item map areas
472  $max = ilMapArea::_getMaxNr($media_item->getId());
473  for ($i = 1; $i <= $max; $i++) {
474  $area = new ilMapArea($media_item->getId(), $i);
475  $media_item->addMapArea($area);
476  }
477 
478  // add media item to media object
479  $a_mob->addMediaItem($media_item);
480  }
481  }
482 
483  public static function deleteAllItemsOfMob(int $a_mob_id): void
484  {
485  global $DIC;
486 
487  $ilDB = $DIC->database();
488 
489  // iterate all media items ob mob
490  $query = "SELECT * FROM media_item WHERE mob_id = " .
491  $ilDB->quote($a_mob_id, "integer");
492  $item_set = $ilDB->query($query);
493  while ($item_rec = $ilDB->fetchAssoc($item_set)) {
494  // delete all parameters of media item
495  $query = "DELETE FROM mob_parameter WHERE med_item_id = " .
496  $ilDB->quote($item_rec["id"], "integer");
497  $ilDB->manipulate($query);
498 
499  // delete all map areas of media item
500  $query = "DELETE FROM map_area WHERE item_id = " .
501  $ilDB->quote($item_rec["id"], "integer");
502  $ilDB->manipulate($query);
503  }
504 
505  // delete media items
506  $query = "DELETE FROM media_item WHERE mob_id = " .
507  $ilDB->quote($a_mob_id, "integer");
508  $ilDB->manipulate($query);
509  }
510 
511  public function setPurpose(string $a_purpose): void
512  {
513  $this->purpose = $a_purpose;
514  }
515 
516  public function getPurpose(): string
517  {
518  return $this->purpose;
519  }
520 
521  public function setLocation(string $a_location): void
522  {
523  $this->location = $a_location;
524  }
525 
526  public function getLocation(): string
527  {
528  return $this->location;
529  }
530 
531  public function setLocationType(string $a_type): void
532  {
533  $this->location_type = $a_type;
534  }
535 
536  public function getLocationType(): string
537  {
538  return $this->location_type;
539  }
540 
541  public function setFormat(string $a_format): void
542  {
543  $this->format = $a_format;
544  }
545 
546  public function getFormat(): string
547  {
548  return $this->format;
549  }
550 
551  public function setThumbTried(string $a_tried): void
552  {
553  $this->tried_thumb = $a_tried;
554  }
555 
556  public function getThumbTried(): string
557  {
558  return $this->tried_thumb;
559  }
560 
561  public function addMapArea(ilMapArea $a_map_area): void
562  {
563  $this->mapareas[$this->map_cnt] = $a_map_area;
564  $this->map_cnt++;
565  }
566 
567  public function deleteMapArea(int $nr): void
568  {
569  for ($i = 1; $i <= $this->map_cnt; $i++) {
570  if ($i > $nr) {
571  $this->mapareas[$i - 2] = $this->mapareas[$i - 1];
572  $this->mapareas[$i - 2]->setNr($i - 1);
573  }
574  }
575  if ($nr <= $this->map_cnt) {
576  unset($this->mapareas[$this->map_cnt - 1]);
577  $this->map_cnt--;
578  }
579  }
580 
581  public function getMapArea(int $nr): ?ilMapArea
582  {
583  return $this->mapareas[$nr - 1] ?? null;
584  }
585 
586  public function getMapAreas(): array
587  {
588  return $this->mapareas;
589  }
590 
591  public function getWidth(): string
592  {
593  return $this->width;
594  }
595 
596  public function setWidth(string $a_width): void
597  {
598  $this->width = $a_width;
599  }
600 
601  public function getHeight(): string
602  {
603  return $this->height;
604  }
605 
606  public function setHeight(string $a_height): void
607  {
608  $this->height = $a_height;
609  }
610 
611  public function getOriginalSize(): ?array
612  {
613  $mob_dir = ilObjMediaObject::_getDirectory($this->getMobId());
614 
615  if (ilUtil::deducibleSize($this->getFormat())) {
616  if ($this->getLocationType() == "LocalFile") {
617  $loc = $mob_dir . "/" . $this->getLocation();
618  } else {
619  $loc = $this->getLocation();
620  }
621 
622  $size = ilMediaImageUtil::getImageSize($loc);
623  if ($size[0] > 0 && $size[1] > 0) {
624  return array("width" => $size[0], "height" => $size[1]);
625  }
626  }
627 
628  return null;
629  }
630 
631  public function setCaption(string $a_caption): void
632  {
633  $this->caption = $a_caption;
634  }
635 
636  public function getCaption(): string
637  {
638  return $this->caption;
639  }
640 
644  public function setHAlign(string $a_halign): void
645  {
646  $this->halign = $a_halign;
647  }
648 
649  public function getHAlign(): string
650  {
651  return $this->halign;
652  }
653 
654  public function setParameter(
655  string $a_name,
656  string $a_value
657  ): void {
658  if (self::checkParameter($a_name, $a_value)) {
659  $this->parameters[$a_name] = $a_value;
660  }
661  }
662 
663  public function resetParameters(): void
664  {
665  $this->parameters = [];
666  }
667 
671  public function setParameters(string $a_par): void
672  {
673  $this->resetParameters();
674  $par_arr = ilUtil::extractParameterString($a_par);
675  if (is_array($par_arr)) {
676  foreach ($par_arr as $par => $val) {
677  $this->setParameter($par, $val);
678  }
679  }
680  }
681 
685  public static function checkParameter(
686  string $a_par,
687  string $a_val
688  ): bool {
689  // do not allow event attributes
690  if (substr(strtolower(trim($a_par)), 0, 2) == "on") {
691  return false;
692  }
693  // no javascript in value
694  if (is_int(strpos(strtolower($a_val), "javascript"))) {
695  return false;
696  }
697  // do not allow to change the src attribute
698  if (strtolower(trim($a_par)) == "src") {
699  return false;
700  }
701 
702  return true;
703  }
704 
705  public function getParameters(): array
706  {
707  return $this->parameters;
708  }
709 
710  public function getParameterString(): string
711  {
712  if (is_array($this->parameters)) {
713  $target_arr = [];
714  foreach ($this->parameters as $par => $val) {
715  $target_arr[] = "$par=\"$val\"";
716  }
717  return implode(", ", $target_arr);
718  }
719  return "";
720  }
721 
722  public function getParameter(string $a_name): string
723  {
724  return (string) ($this->parameters[$a_name] ?? "");
725  }
726 
730  public function getWorkDirectory(): string
731  {
732  return ilFileUtils::getDataDir() . "/map_workfiles/item_" . $this->getId();
733  }
734 
738  public function createWorkDirectory(): void
739  {
740  if (!is_dir(ilFileUtils::getDataDir() . "/map_workfiles")) {
742  }
743  $work_dir = $this->getWorkDirectory();
744  if (!is_dir($work_dir)) {
745  ilFileUtils::createDirectory($work_dir);
746  }
747  }
748 
752  public function getSuffix(): string
753  {
754  $loc_arr = explode(".", $this->getLocation());
755 
756  return $loc_arr[count($loc_arr) - 1];
757  }
758 
762  public function getMapWorkCopyType(): string
763  {
764  return self::getGDSupportedImageType($this->getSuffix());
765  }
766 
771  public function getMapWorkCopyName(
772  bool $a_reference_copy = false
773  ): string {
774  $file_arr = explode("/", $this->getLocation());
775  $o_file = $file_arr[count($file_arr) - 1];
776  $file_arr = explode(".", $o_file);
777  unset($file_arr[count($file_arr) - 1]);
778  $file = implode(".", $file_arr);
779 
780  if (!$a_reference_copy) {
781  return $this->getWorkDirectory() . "/" . $file . "." . $this->getMapWorkCopyType();
782  } else {
783  return $this->getWorkDirectory() . "/l_copy_" . $o_file;
784  }
785  }
786 
790  public function getDirectory(): string
791  {
792  return ilObjMediaObject::_getDirectory($this->getMobId());
793  }
794 
798  public function getThumbnailDirectory(
799  string $a_mode = "filesystem"
800  ): string {
801  return ilObjMediaObject::_getThumbnailDirectory($this->getMobId(), $a_mode);
802  }
803 
807  public function getThumbnailTarget(
808  string $a_size = ""
809  ): string {
810  $jpeg_file = $this->getThumbnailDirectory() . "/" .
811  $this->getPurpose() . ".jpeg";
812  $format = "png";
813  if (is_file($jpeg_file)) {
814  $format = "jpeg";
815  }
816  if (is_int(strpos($this->getFormat(), "image"))) {
817  $thumb_file = $this->getThumbnailDirectory() . "/" .
818  $this->getPurpose() . "." . $format;
819  $thumb_file_small = $this->getThumbnailDirectory() . "/" .
820  $this->getPurpose() . "_small." . $format;
821  // generate thumbnail (if not tried before)
822  if ($this->getThumbTried() == "n" && $this->getLocationType() == "LocalFile" && $this->getFormat() !== "image/svg+xml") {
823  if (is_file($thumb_file)) {
824  unlink($thumb_file);
825  }
826  if (is_file($thumb_file_small)) {
827  unlink($thumb_file_small);
828  }
829  $this->writeThumbTried("y");
831  $med_file = $this->getDirectory() . "/" . $this->getLocation();
832 
833  if (is_file($med_file)) {
834  $mob = new ilObjMediaObject($this->getMobId());
835  $mob->makeThumbnail($this->getLocation(), $this->getPurpose() . "." . $format, $format, "80");
836  $mob->makeThumbnail($this->getLocation(), $this->getPurpose() . "_small." . $format, $format, "40");
837  }
838  }
839  if ($this->getFormat() === "image/svg+xml") {
840  return ilObjMediaObject::_getURL($this->getMobId()) . "/" . $this->getLocation();
841  }
842  if ($a_size == "small") {
843  if (is_file($thumb_file_small)) {
844  $random = new \ilRandom();
845  return $this->getThumbnailDirectory("output") . "/" .
846  $this->getPurpose() . "_small." . $format . "?dummy=" . $random->int(1, 999999);
847  }
848  } else {
849  if (is_file($thumb_file)) {
850  $random = new \ilRandom();
851  return $this->getThumbnailDirectory("output") . "/" .
852  $this->getPurpose() . "." . $format . "?dummy=" . $random->int(1, 999999);
853  }
854  }
855  }
856 
857  return "";
858  }
859 
865  public function copyOriginal(): void
866  {
867  $lng = $this->lng;
868  $this->createWorkDirectory();
869 
870  if ($this->getLocationType() !== "Reference") {
871  $this->image_converter->convertToFormat(
872  $this->getDirectory() . "/" . $this->getLocation(),
873  $this->getMapWorkCopyName(),
874  $this->getMapWorkCopyType(),
875  $this->getWidth() === '' ? null : $this->getWidth(),
876  $this->getHeight() === '' ? null : $this->getHeight()
877  );
878  } else {
879  // first copy the external file, if necessary
880  if (!is_file($this->getMapWorkCopyName(true)) || (filesize($this->getMapWorkCopyName(true)) == 0)) {
881  $handle = fopen($this->getLocation(), "r");
882  $lcopy = fopen($this->getMapWorkCopyName(true), "w");
883  if ($handle && $lcopy) {
884  while (!feof($handle)) {
885  $content = fread($handle, 4096);
886  fwrite($lcopy, $content);
887  }
888  }
889  fclose($lcopy);
890  fclose($handle);
891  }
892 
893  // now, create working copy
894  $this->image_converter->convertToFormat(
895  $this->getMapWorkCopyName(true),
896  $this->getMapWorkCopyName(),
897  $this->getMapWorkCopyType(),
898  $this->getWidth() === '' ? null : $this->getWidth(),
899  $this->getHeight() === '' ? null : $this->getHeight()
900  );
901  }
902  if (!is_file($this->getMapWorkCopyName())) {
903  throw new ilMapEditingException($lng->txt("cont_map_file_not_generated"));
904  }
905  }
906 
912  public function makeMapWorkCopy(
913  int $a_area_nr = 0,
914  bool $a_exclude = false
915  ): void {
916  $lng = $this->lng;
917 
918  $this->copyOriginal();
919  $this->buildMapWorkImage();
920 
921  // determine ratios
922  $size = getimagesize($this->getMapWorkCopyName());
923  $x_ratio = 1;
924  if ($size[0] > 0 && $this->getWidth() > 0) {
925  $x_ratio = $this->getWidth() / $size[0];
926  }
927  $y_ratio = 1;
928  if ($size[1] > 0 && $this->getHeight() > 0) {
929  $y_ratio = $this->getHeight() / $size[1];
930  }
931 
932  // draw map areas
933  for ($i = 0; $i < count($this->mapareas); $i++) {
934  if (((($i + 1) == $a_area_nr) && !$a_exclude) ||
935  ((($i + 1) != $a_area_nr) && $a_exclude) ||
936  ($a_area_nr == 0)
937  ) {
938  $area = $this->mapareas[$i];
939  $area->draw(
940  $this->getMapWorkImage(),
941  $this->color1,
942  $this->color2,
943  true,
944  $x_ratio,
945  $y_ratio
946  );
947  }
948  }
949 
950  $this->saveMapWorkImage();
951  }
952 
958  public function addAreaToMapWorkCopy(
959  string $a_shape,
960  string $a_coords
961  ): void {
962  $this->buildMapWorkImage();
963 
964  // determine ratios
965  $size = getimagesize($this->getMapWorkCopyName());
966  $x_ratio = 1;
967  if ($size[0] > 0 && $this->getWidth() > 0) {
968  $x_ratio = $this->getWidth() / $size[0];
969  }
970  $y_ratio = 1;
971  if ($size[1] > 0 && $this->getHeight() > 0) {
972  $y_ratio = $this->getHeight() / $size[1];
973  }
974 
975  // add new area to work image
976  $area = new ilMapArea();
977  $area->setShape($a_shape);
978  $area->setCoords($a_coords);
979  $area->draw(
980  $this->getMapWorkImage(),
981  $this->color1,
982  $this->color2,
983  false,
984  $x_ratio,
985  $y_ratio
986  );
987 
988  $this->saveMapWorkImage();
989  }
990 
994  public function outputMapWorkCopy(): void
995  {
996  if ($this->getMapWorkCopyType() != "") {
997  header("Pragma: no-cache");
998  header("Expires: 0");
999  header("Content-type: image/" . strtolower($this->getMapWorkCopyType()));
1000  readfile($this->getMapWorkCopyName());
1001  }
1002  exit;
1003  }
1004 
1008  public function buildMapWorkImage(): void
1009  {
1010  $im_type = strtolower($this->getMapWorkCopyType());
1011 
1012  switch ($im_type) {
1013  case "gif":
1014  $this->map_image = imagecreatefromgif($this->getMapWorkCopyName());
1015  break;
1016 
1017  case "jpg":
1018  case "jpeg":
1019  $this->map_image = imagecreatefromjpeg($this->getMapWorkCopyName());
1020  break;
1021 
1022  case "png":
1023  $this->map_image = imagecreatefrompng($this->getMapWorkCopyName());
1024  break;
1025  }
1026 
1027  // try to allocate black and white as color. if this is not possible, get the closest colors
1028  if (imagecolorstotal($this->map_image) > 250) {
1029  $this->color1 = imagecolorclosest($this->map_image, 0, 0, 0);
1030  $this->color2 = imagecolorclosest($this->map_image, 255, 255, 255);
1031  } else {
1032  $this->color1 = imagecolorallocate($this->map_image, 0, 0, 0);
1033  $this->color2 = imagecolorallocate($this->map_image, 255, 255, 255);
1034  }
1035  }
1036 
1040  public function saveMapWorkImage(): void
1041  {
1042  $im_type = strtolower($this->getMapWorkCopyType());
1043 
1044  // save image work-copy and free memory
1045  switch ($im_type) {
1046  case "gif":
1047  imagegif($this->map_image, $this->getMapWorkCopyName());
1048  break;
1049 
1050  case "jpg":
1051  case "jpeg":
1052  imagejpeg($this->map_image, $this->getMapWorkCopyName());
1053  break;
1054 
1055  case "png":
1056  imagepng($this->map_image, $this->getMapWorkCopyName());
1057  break;
1058  }
1059 
1060  imagedestroy($this->map_image);
1061  }
1062 
1066  public function getMapWorkImage()
1067  {
1068  return $this->map_image;
1069  }
1070 
1074  public function getMapAreasXML(
1075  bool $a_insert_inst = false,
1076  int $a_inst = 0
1077  ): string {
1078  $xml = "";
1079 
1080  // build xml of map areas
1081  for ($i = 0; $i < count($this->mapareas); $i++) {
1082  $area = $this->mapareas[$i];
1083 
1084  // highlight mode
1085  $hm = "";
1086  if ($area->getHighlightMode() != "") {
1087  $hm = ' HighlightMode="' . $area->getHighlightMode() . '" ';
1088  $hcl = ($area->getHighlightClass() != "")
1089  ? $area->getHighlightClass()
1090  : "Accented";
1091  $hm .= 'HighlightClass="' . $hcl . '" ';
1092  }
1093 
1094  $xml .= "<MapArea Shape=\"" . $area->getShape() . "\" Coords=\"" . $area->getCoords() . "\" " . $hm . ">";
1095  if ($area->getLinkType() == IL_INT_LINK) {
1096  $target_frame = $area->getTargetFrame();
1097 
1098  if ($area->getType() == "GlossaryItem" && $target_frame == "") {
1099  $target_frame = "Glossary";
1100  }
1101 
1102  $tf_str = ($target_frame == "")
1103  ? ""
1104  : "TargetFrame=\"" . $target_frame . "\"";
1105 
1106  $xml .= "<IntLink Target=\"" . $area->getTarget($a_insert_inst, $a_inst) . "\" Type=\"" .
1107  $area->getType() . "\" $tf_str>";
1108  // see bug 17893 and http://stackoverflow.com/questions/4026502/xml-error-at-ampersand
1109  $xml .= htmlspecialchars($area->getTitle(), ENT_QUOTES);
1110  $xml .= "</IntLink>";
1111  } else {
1112  $xml .= "<ExtLink Href=\"" . str_replace("&", "&amp;", $area->getHref()) . "\" Title=\"" .
1113  str_replace("&", "&amp;", $area->getExtTitle()) . "\">";
1114  $xml .= str_replace("&", "&amp;", $area->getTitle());
1115  $xml .= "</ExtLink>";
1116  }
1117  $xml .= "</MapArea>";
1118  }
1119  return $xml;
1120  }
1121 
1126  public static function _resolveMapAreaLinks(
1127  int $a_mob_id
1128  ): void {
1129  global $DIC;
1130 
1131  $ilDB = $DIC->database();
1132 
1133  //echo "mediaItems::resolve<br>";
1134  // read media_object record
1135  $query = "SELECT * FROM media_item WHERE mob_id = " .
1136  $ilDB->quote($a_mob_id, "integer") . " " .
1137  "ORDER BY nr";
1138  $item_set = $ilDB->query($query);
1139  while ($item_rec = $ilDB->fetchAssoc($item_set)) {
1140  ilMapArea::_resolveIntLinks($item_rec["id"]);
1141  }
1142  }
1143 
1148  public static function _getMapAreasIntLinks(
1149  int $a_mob_id
1150  ): array {
1151  global $DIC;
1152 
1153  $ilDB = $DIC->database();
1154 
1155  // read media_items records
1156  $query = "SELECT * FROM media_item WHERE mob_id = " .
1157  $ilDB->quote($a_mob_id, "integer") . " ORDER BY nr";
1158 
1159  $item_set = $ilDB->query($query);
1160  $links = array();
1161  while ($item_rec = $ilDB->fetchAssoc($item_set)) {
1162  $map_links = ilMapArea::_getIntLinks($item_rec["id"]);
1163  foreach ($map_links as $key => $map_link) {
1164  $links[$key] = $map_link;
1165  }
1166  }
1167  return $links;
1168  }
1169 
1173  public function extractUrlParameters(): void
1174  {
1176  $this->getLocation(),
1177  $this->getParameters()
1178  );
1179  foreach ($par as $k => $v) {
1180  $this->setParameter($k, $v);
1181  }
1182  }
1183 
1184  public function determineDuration(): void
1185  {
1186  $ana = new ilMediaAnalyzer();
1187 
1190  $meta = ilExternalMediaAnalyzer::getVimeoMetadata($par["id"]);
1191  if ($meta["duration"] > 0) {
1192  $this->setDuration((int) $meta["duration"]);
1193  }
1194  } else {
1195  $file = ($this->getLocationType() == "Reference")
1196  ? $this->getLocation()
1197  : ilObjMediaObject::_getDirectory($this->getMobId()) . "/" . $this->getLocation();
1198 
1199  $remote = false;
1200 
1201  try {
1202  if (substr($file, 0, 4) == "http") {
1203  if ($fp_remote = fopen($file, 'rb')) {
1204  $tmpdir = ilFileUtils::ilTempnam();
1205  ilFileUtils::makeDir($tmpdir);
1206  $localtempfilename = tempnam($tmpdir, 'getID3');
1207  if ($fp_local = fopen($localtempfilename, 'wb')) {
1208  while ($buffer = fread($fp_remote, 8192)) {
1209  fwrite($fp_local, $buffer);
1210  }
1211  fclose($fp_local);
1212  $file = $localtempfilename;
1213  }
1214  fclose($fp_remote);
1215  }
1216  }
1217 
1218  $ana->setFile($file);
1219  $ana->analyzeFile();
1220  $this->setDuration((int) $ana->getPlaytimeSeconds());
1221 
1222  if ($remote) {
1223  unlink($localtempfilename);
1224  }
1225  } catch (Exception $e) {
1226  }
1227  }
1228  }
1229 
1235  public static function getMediaItemsForUploadHash(
1236  string $a_hash
1237  ): array {
1238  global $DIC;
1239 
1240  $db = $DIC->database();
1241 
1242  $set = $db->queryF(
1243  "SELECT * FROM media_item " .
1244  " WHERE upload_hash = %s ",
1245  array("text"),
1246  array($a_hash)
1247  );
1248  $media_items = array();
1249  while ($rec = $db->fetchAssoc($set)) {
1250  $media_items[] = $rec;
1251  }
1252  return $media_items;
1253  }
1254 }
getThumbnailDirectory(string $a_mode="filesystem")
get media file directory
static _getThumbnailDirectory(int $a_mob_id, string $a_mode="filesystem")
get directory for files of media object
setPurpose(string $a_purpose)
getSuffix()
get location suffix
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
string $text_representation
exit
Definition: login.php:29
getMapWorkCopyType()
get image type of image map work copy
ilLanguage $lng
static _lookupLocationForMobId(int $a_mob_id, string $a_purpose)
static extractUrlParameters(string $a_location, array $a_parameter)
Extract URL information to parameter array.
__construct(int $a_id=0)
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
writeThumbTried(string $a_tried)
write thumbnail creation try data ("y"/"n")
getWorkDirectory()
get work directory for image map editing
fetchAssoc(ilDBStatement $statement)
addMapArea(ilMapArea $a_map_area)
getParameter(string $a_name)
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:33
copyOriginal()
Copy the original file for map editing to the working directory.
read()
read media item data (item id or (mob_id and nr) must be set)
setParameter(string $a_name, string $a_value)
static checkParameter(string $a_par, string $a_val)
Check parameter (filter javascript related and other unsafe parameters/values)
static _getMaxNr(int $a_item_id)
get maximum nr of media item (static)
addMediaItem(ilMediaItem $a_item)
buildMapWorkImage()
build image map work image
static _getIntLinks(int $a_item_id)
get all internal links of a media items map areas
static _createThumbnailDirectory(int $a_obj_id)
Create thumbnail directory.
setDuration(int $a_val)
setLocation(string $a_location)
static _getMediaItemsOfMOb(ilObjMediaObject $a_mob)
Read media items into(!) media object (static)
static isVimeo(string $a_location)
Identify Vimeo links.
setFormat(string $a_format)
setCaption(string $a_caption)
writeParameter(string $a_name, string $a_value)
static _resolveIntLinks(int $a_item_id)
resolve internal links of an item id
static deleteAllItemsOfMob(int $a_mob_id)
ILIAS Filesystem Util Convert LegacyImages $image_converter
setHAlign(string $a_halign)
set horizontal align
global $DIC
Definition: feed.php:28
ilDBInterface $db
setMobId(int $a_mob_id)
set id of parent media object
static _getDirectory(int $a_mob_id)
Get absolute directory.
addAreaToMapWorkCopy(string $a_shape, string $a_coords)
draw a new area in work image
const IL_INT_LINK
getMapWorkCopyName(bool $a_reference_copy=false)
Get name of image map work copy file.
getDirectory()
get media file directory
static _lookupMobId(int $a_med_id)
makeMapWorkCopy(int $a_area_nr=0, bool $a_exclude=false)
make map work copy of image
static getMediaItemsForUploadHash(string $a_hash)
Get media items for upload hash.
static createDirectory(string $a_dir, int $a_mod=0755)
create directory
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
string $key
Consumer key/client ID value.
Definition: System.php:193
deleteMapArea(int $nr)
static _getMapAreasIntLinks(int $a_mob_id)
get all internal links of map areas of a mob
setTextRepresentation(string $a_val)
static getDataDir()
get data directory (outside webspace)
extractUrlParameters()
Extract parameters of special external references to parameter array.
setHeight(string $a_height)
setUploadHash(string $a_val)
queryF(string $query, array $types, array $values)
static _resolveMapAreaLinks(int $a_mob_id)
resolve internal links of all media items of a media object
createWorkDirectory()
create work directory for image map editing
saveMapWorkImage()
save image map work image as file
setThumbTried(string $a_tried)
static deducibleSize(string $a_mime)
checks if mime type is provided by getimagesize()
static getImageSize(string $a_location)
Get image size from location.
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
Class ilMapArea.
static extractParameterString(string $a_parstr)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$q
Definition: shib_logout.php:21
setWidth(string $a_width)
setNr(int $a_nr)
set number of media item within media object
static _getURL(int $a_mob_id)
get directory for files of media object
outputMapWorkCopy()
output raw map work copy file
setLocationType(string $a_type)
static extractVimeoParameters(string $a_location)
Extract Vimeo Parameter.
static _getMediaItemsOfMObId(int $a_mobId, string $a_purpose)
read media item with specific purpose and mobId
setParameters(string $a_par)
set all parameters via parameter string (format: par1="value1", par2="value2", ...)
setId(int $a_id)
set media item id
getMapAreasXML(bool $a_insert_inst=false, int $a_inst=0)
get xml code of media items&#39; areas
getThumbnailTarget(string $a_size="")
get thumbnail target
static getGDSupportedImageType(string $a_desired_type)
returns the best supported image type by this PHP build
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...