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