ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilBadgeImage.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Badge;
22
23use ilBadge;
31use Exception;
32
34{
35 public const IMAGE_SIZE_XS = 4;
36 public const IMAGE_SIZE_S = 3;
37 public const IMAGE_SIZE_M = 2;
38 public const IMAGE_SIZE_L = 1;
39 public const IMAGE_SIZE_XL = 0;
40 public const IMAGE_URL_COUNT = 5;
41
45
46 public function __construct(
47 Services $resourceStorage,
48 FileUpload $uploadService,
50 ) {
51 $this->resource_storage = $resourceStorage;
52 $this->upload_service = $uploadService;
53 $this->main_template = $main_template;
54 }
55
56 public function getImageFromBadge(ilBadge $badge, int $size = self::IMAGE_SIZE_XS): string
57 {
58 return $this->getImageFromResourceId($badge, $size);
59 }
60
61 public function getImageFromResourceId(
62 ilBadge $badge,
63 int $size = self::IMAGE_SIZE_XS
64 ): string {
65 $image_src = '';
66
67 if ($badge->getImageRid() !== '' && $badge->getImageRid() !== null) {
68 $identification = $this->resource_storage->manage()->find($badge->getImageRid());
69 if ($identification !== null) {
70 $image_src = $this->resource_storage->consume()->src($identification)->getSrc();
71 }
72 } elseif ($badge->getImage()) {
73 $image_src = ilWACSignedPath::signFile($badge->getImagePath());
74 }
75
76 return $image_src;
77 }
78
79
80 public function processImageUpload(ilBadge $badge): void
81 {
82 try {
83 $array_result = $this->upload_service->getResults();
84 $array_result = array_pop($array_result);
85 $stakeholder = new ilBadgeFileStakeholder();
86 $identification = $this->resource_storage->manage()->upload($array_result, $stakeholder);
87 $badge->setImageRid($identification->serialize());
88 $badge->update();
89 } catch (IllegalStateException $e) {
90 $this->main_template->setOnScreenMessage('failure', $e->getMessage(), true);
91 }
92 }
93
97 public function cloneBadgeImageByRid(ResourceIdentification $identification): string
98 {
99 return $this->resource_storage->manage()->clone($identification)->serialize();
100 }
101}
ilGlobalTemplateInterface $main_template
getImageFromResourceId(ilBadge $badge, int $size=self::IMAGE_SIZE_XS)
cloneBadgeImageByRid(ResourceIdentification $identification)
processImageUpload(ilBadge $badge)
__construct(Services $resourceStorage, FileUpload $uploadService, ilGlobalTemplateInterface $main_template)
getImageFromBadge(ilBadge $badge, int $size=self::IMAGE_SIZE_XS)
Class Services.
Definition: Services.php:38
getImagePath(bool $a_full_path=true)
setImageRid(?string $image_rid)
Class ilWACSignedPath.
static signFile(string $path_to_file)