ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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;
27 
33 {
34  protected ImageFileRepo $repo;
36  protected int $style_id;
37  private Filesystem\Util\Convert\LegacyImages $image_conversion;
38 
39  public function __construct(
40  int $style_id,
41  Access\StyleAccessManager $access_manager,
42  ImageFileRepo $repo
43  ) {
44  global $DIC;
45  $this->repo = $repo;
46  $this->access_manager = $access_manager;
47  $this->style_id = $style_id;
48  $this->image_conversion = $DIC->fileConverters()->legacyImages();
49  }
50 
56  public function getImages(): Generator
57  {
58  return $this->repo->getImages($this->style_id);
59  }
60 
61  public function filenameExists(string $filename): bool
62  {
64  foreach ($this->getImages() as $i) {
65  if ($i->getFilename() == $filename) {
66  return true;
67  }
68  }
69  return false;
70  }
71 
72  // get web data dir path for output
73  public function getWebPath(Image $image): string
74  {
75  return $this->repo->getWebPath($image->getPath());
76  }
77 
78  // get image data object by filename
79  public function getByFilename(string $filename): Image
80  {
81  return $this->repo->getByFilename($this->style_id, $filename);
82  }
83 
84  // resize image
85  public function resizeImage(
86  string $filename,
87  int $width,
88  int $height,
89  bool $constrain_proportions
90  ): void {
91  if ($this->filenameExists($filename)) {
92  $file = $this->getWebPath($this->getByFilename($filename));
93 
94  $this->image_conversion->resizeToFixedSize(
95  $file,
96  $file,
97  $width,
98  $height,
99  $constrain_proportions
100  );
101  }
102  }
103 
104  // resize image
105  public function supportsResize(
106  Image $image
107  ): bool {
108  // for svg, see
109  // https://stackoverflow.com/questions/6532261/how-do-you-get-the-width-and-height-of-an-svg-picture-in-php
110  if (in_array(
111  strtolower(pathinfo($image->getFilename(), PATHINFO_EXTENSION)),
112  ["jpg", "jpeg", "gif", "png"]
113  )) {
114  return true;
115  }
116  return false;
117  }
118 
119  // upload image
120  public function uploadImage(): void
121  {
122  $this->repo->uploadImage($this->style_id);
123  }
124 
125  public function deleteByFilename(string $filename): void
126  {
127  $this->repo->deleteImageByFilename($this->style_id, $filename);
128  }
129 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(int $style_id, Access\StyleAccessManager $access_manager, ImageFileRepo $repo)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Filesystem Util Convert LegacyImages $image_conversion
global $DIC
Definition: feed.php:28
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...
resizeImage(string $filename, int $width, int $height, bool $constrain_proportions)
$filename
Definition: buildRTE.php:78
Manages access to content style editing.
Access StyleAccessManager $access_manager