ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ImageManager.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Style\Content;
22
25use ilShellUtil;
26use Generator;
32
38{
43 protected int $style_id;
45
46 public function __construct(
47 int $style_id,
50 protected ResourceStakeholder $stakeholder
51 ) {
52 global $DIC;
53 $this->repo = $repo->image();
54 $this->style_repo = $repo->style();
55 $this->access_manager = $access_manager;
56 $this->style_id = $style_id;
57 $this->image_conversion = $DIC->fileConverters()->legacyImages();
58 $this->img_convert = $DIC->fileConverters()->images();
59 }
60
66 public function getImages(bool $include_size_info = false): Generator
67 {
68 $rid = $this->style_repo->readRid($this->style_id);
69 return $this->repo->getImages($this->style_id, $rid, $include_size_info);
70 }
71
72 public function hasLegacyDirAndNoImages(): bool
73 {
74 return $this->repo->hasLegacyDir($this->style_id) &&
75 !$this->repo->hasImages(
76 $this->style_id,
77 $this->style_repo->readRid($this->style_id)
78 );
79 }
80
81 public function filenameExists(string $filename): bool
82 {
84 foreach ($this->getImages() as $i) {
85 if ($i->getFilename() == $filename) {
86 return true;
87 }
88 }
89 return false;
90 }
91
92 // get web data dir path for output
93 public function getWebPath(Image $image): string
94 {
95 return $this->repo->getWebPath($image->getPath());
96 }
97
98 // get image data object by filename
99 public function getByFilename(string $filename): Image
100 {
101 $rid = $this->style_repo->readRid($this->style_id);
102 return $this->repo->getByFilename($this->style_id, $rid, $filename);
103 }
104
105 // resize image
106 public function resizeImage(
107 string $filename,
108 int $width,
109 int $height,
110 bool $constrain_proportions
111 ): void {
112 $rid = $this->style_repo->readRid($this->style_id);
113 if ($this->filenameExists($filename)) {
114 // the zip stream is not seekable, which is needed by Imagick
115 // so we create a seekable stream first
116 $tempStream = fopen('php://temp', 'w+');
117 stream_copy_to_stream($this->repo->getImageStream($rid, $filename)->detach(), $tempStream);
118 rewind($tempStream);
119 $stream = new Stream($tempStream);
120
121 $converter = $this->img_convert->resizeToFixedSize(
122 $stream,
123 $width,
124 $height
125 );
126 $this->repo->addStream(
127 $rid,
128 $filename,
129 $converter->getStream()
130 );
131 fclose($tempStream);
132 }
133 }
134
135 // resize image
136 public function supportsResize(
137 Image $image
138 ): bool {
139 // for svg, see
140 // https://stackoverflow.com/questions/6532261/how-do-you-get-the-width-and-height-of-an-svg-picture-in-php
141 if (in_array(
142 strtolower(pathinfo($image->getFilename(), PATHINFO_EXTENSION)),
143 ["jpg", "jpeg", "gif", "png"]
144 )) {
145 return true;
146 }
147 return false;
148 }
149
150 // upload image
151
152 public function deleteByFilename(string $filename): void
153 {
154 $this->repo->deleteImageByFilename($this->style_id, $filename);
155 }
156
157 public function importFromUploadResult(
158 UploadResult $result
159 ): void {
160 $rid = $this->style_repo->readRid($this->style_id);
161 if ($rid !== "") {
162 $this->repo->importFromUploadResult(
163 $rid,
164 $result
165 );
166 }
167 }
168
169}
$filename
Definition: buildRTE.php:78
Manages access to content style editing.
Main business logic for content style images.
getImages(bool $include_size_info=false)
Get images of style.
importFromUploadResult(UploadResult $result)
Access StyleAccessManager $access_manager
resizeImage(string $filename, int $width, int $height, bool $constrain_proportions)
Filesystem Util Convert LegacyImages $image_conversion
__construct(int $style_id, Access\StyleAccessManager $access_manager, InternalRepoService $repo, protected ResourceStakeholder $stakeholder)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26