ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
ILIAS\Filesystem\Util\Convert\ImageConverter Class Reference
+ Collaboration diagram for ILIAS\Filesystem\Util\Convert\ImageConverter:

Public Member Functions

 __construct (protected ImageConversionOptions $conversion_options, protected ImageOutputOptions $output_options, protected FileStream $stream)
 
 isOK ()
 
 getThrowableIfAny ()
 
 getStream ()
 

Protected Member Functions

 handleImageDimension ()
 
 buildOutputStream ()
 
 handleFormatAndQuality ()
 
 handleBackgroundColor ()
 
 readInputStream ()
 
 doCrop (int $width, int $height)
 
 doResize (int $width, int $height)
 
 doThumbnail (int $width, int $height)
 

Protected Attributes

const STATUS_OK = 1
 
const STATUS_FAILED = 2
 
const STATUS_UNKNOWN = 4
 
const RESOLUTION = 72
 
const RESOLUTION_FACTOR = self::RESOLUTION / 72
 
int $status = self::STATUS_UNKNOWN
 
FileStream $output_stream = null
 
Throwable $throwable = null
 
string $requested_background = null
 
Imagick $image
 

Private Member Functions

 convert ()
 
 factoredResolution (int $initial)
 

Detailed Description

Author
Fabian Schmid fabia.nosp@m.n@sr.nosp@m..solu.nosp@m.tion.nosp@m.s

Definition at line 31 of file ImageConverter.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\Filesystem\Util\Convert\ImageConverter::__construct ( protected ImageConversionOptions  $conversion_options,
protected ImageOutputOptions  $output_options,
protected FileStream  $stream 
)

Definition at line 48 of file ImageConverter.php.

References ILIAS\Filesystem\Util\Convert\ImageConverter\convert().

52  {
53  $this->image = new \Imagick();
54  $this->convert();
55  }
+ Here is the call graph for this function:

Member Function Documentation

◆ buildOutputStream()

ILIAS\Filesystem\Util\Convert\ImageConverter::buildOutputStream ( )
protected

Definition at line 156 of file ImageConverter.php.

References null.

Referenced by ILIAS\Filesystem\Util\Convert\ImageConverter\convert().

156  : void
157  {
158  if ($this->conversion_options->getOutputPath() === null) {
159  $this->output_stream = Streams::ofString($this->image->getImageBlob());
160  } else {
161  $this->image->writeImage($this->conversion_options->getOutputPath());
162  $this->output_stream = Streams::ofResource(fopen($this->conversion_options->getOutputPath(), 'rb'));
163  }
164 
165  $this->output_stream->rewind();
166 
167  $this->status = self::STATUS_OK;
168  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
+ Here is the caller graph for this function:

◆ convert()

ILIAS\Filesystem\Util\Convert\ImageConverter::convert ( )
private

Definition at line 57 of file ImageConverter.php.

References ILIAS\Filesystem\Util\Convert\ImageConverter\buildOutputStream(), ILIAS\Filesystem\Util\Convert\ImageConverter\handleBackgroundColor(), ILIAS\Filesystem\Util\Convert\ImageConverter\handleFormatAndQuality(), ILIAS\Filesystem\Util\Convert\ImageConverter\handleImageDimension(), and ILIAS\Filesystem\Util\Convert\ImageConverter\readInputStream().

Referenced by ILIAS\Filesystem\Util\Convert\ImageConverter\__construct().

57  : void
58  {
59  try {
60  $this->handleBackgroundColor();
61  $this->readInputStream();
62  $this->handleFormatAndQuality();
63  $this->handleImageDimension();
64  $this->buildOutputStream();
65  } catch (\Throwable $t) {
66  $this->status = self::STATUS_FAILED;
67  $this->throwable = $t;
68  if ($this->conversion_options->throwOnError()) {
69  throw $t;
70  }
71  }
72  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ doCrop()

ILIAS\Filesystem\Util\Convert\ImageConverter::doCrop ( int  $width,
int  $height 
)
protected

Definition at line 273 of file ImageConverter.php.

References ILIAS\Filesystem\Util\Convert\ImageConverter\factoredResolution().

Referenced by ILIAS\Filesystem\Util\Convert\ImageConverter\handleImageDimension().

273  : void
274  {
275  $this->image->setGravity(\Imagick::GRAVITY_CENTER);
276  $this->image->cropThumbnailImage(
277  $this->factoredResolution($width),
278  $this->factoredResolution($height)
279  );
280  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ doResize()

ILIAS\Filesystem\Util\Convert\ImageConverter::doResize ( int  $width,
int  $height 
)
protected

Definition at line 282 of file ImageConverter.php.

References ILIAS\Filesystem\Util\Convert\ImageConverter\factoredResolution().

Referenced by ILIAS\Filesystem\Util\Convert\ImageConverter\handleImageDimension().

282  : void
283  {
284  $this->image->resizeImage(
285  $this->factoredResolution($width),
286  $this->factoredResolution($height),
287  \Imagick::FILTER_LANCZOS,
288  1
289  );
290  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ doThumbnail()

ILIAS\Filesystem\Util\Convert\ImageConverter::doThumbnail ( int  $width,
int  $height 
)
protected

Definition at line 292 of file ImageConverter.php.

References ILIAS\Filesystem\Util\Convert\ImageConverter\factoredResolution().

Referenced by ILIAS\Filesystem\Util\Convert\ImageConverter\handleImageDimension().

292  : void
293  {
294  $this->image->thumbnailImage(
295  $this->factoredResolution($width),
296  $this->factoredResolution($height),
297  true,
298  !$this->conversion_options->keepAspectRatio(),
299  );
300  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ factoredResolution()

ILIAS\Filesystem\Util\Convert\ImageConverter::factoredResolution ( int  $initial)
private

Definition at line 268 of file ImageConverter.php.

Referenced by ILIAS\Filesystem\Util\Convert\ImageConverter\doCrop(), ILIAS\Filesystem\Util\Convert\ImageConverter\doResize(), and ILIAS\Filesystem\Util\Convert\ImageConverter\doThumbnail().

268  : int
269  {
270  return intval(round($initial * self::RESOLUTION_FACTOR, 0));
271  }
+ Here is the caller graph for this function:

◆ getStream()

ILIAS\Filesystem\Util\Convert\ImageConverter::getStream ( )

Definition at line 263 of file ImageConverter.php.

References ILIAS\Filesystem\Util\Convert\ImageConverter\$output_stream.

Referenced by ILIAS\Filesystem\Util\Convert\LegacyImages\storeStream().

263  : FileStream
264  {
265  return $this->output_stream;
266  }
+ Here is the caller graph for this function:

◆ getThrowableIfAny()

ILIAS\Filesystem\Util\Convert\ImageConverter::getThrowableIfAny ( )

Definition at line 257 of file ImageConverter.php.

References ILIAS\Filesystem\Util\Convert\ImageConverter\$throwable.

Referenced by ILIAS\Filesystem\Util\Convert\LegacyImages\storeStream().

257  : ?\Throwable
258  {
259  return $this->throwable;
260  }
+ Here is the caller graph for this function:

◆ handleBackgroundColor()

ILIAS\Filesystem\Util\Convert\ImageConverter::handleBackgroundColor ( )
protected

Definition at line 226 of file ImageConverter.php.

References ILIAS\Filesystem\Util\Convert\ImageOutputOptions\FORMAT_JPG, ILIAS\Filesystem\Util\Convert\ImageOutputOptions\FORMAT_PNG, and null.

Referenced by ILIAS\Filesystem\Util\Convert\ImageConverter\convert().

226  : void
227  {
228  $this->requested_background = $this->conversion_options->getBackgroundColor();
229  if ($this->output_options->getFormat(
230  ) === ImageOutputOptions::FORMAT_JPG && $this->requested_background === null) {
231  $this->requested_background = '#FFFFFF';
232  }
233  if ($this->output_options->getFormat(
234  ) === ImageOutputOptions::FORMAT_PNG && $this->requested_background === null) {
235  $this->requested_background = 'transparent';
236  }
237  if ($this->requested_background !== null) {
238  $this->image->setBackgroundColor(new \ImagickPixel($this->requested_background));
239  }
240  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
+ Here is the caller graph for this function:

◆ handleFormatAndQuality()

ILIAS\Filesystem\Util\Convert\ImageConverter::handleFormatAndQuality ( )
protected

Definition at line 170 of file ImageConverter.php.

References ILIAS\Filesystem\Util\Convert\ImageOutputOptions\FORMAT_JPG, ILIAS\Filesystem\Util\Convert\ImageOutputOptions\FORMAT_KEEP, ILIAS\Filesystem\Util\Convert\ImageOutputOptions\FORMAT_PNG, ILIAS\Filesystem\Util\Convert\ImageOutputOptions\FORMAT_WEBP, and null.

Referenced by ILIAS\Filesystem\Util\Convert\ImageConverter\convert().

170  : void
171  {
172  $this->image->setImageResolution(
173  self::RESOLUTION,
174  self::RESOLUTION
175  );
176  // High density images do not support Resampling, cache to small. we deactivate this
177  /*$this->image->resampleImage(
178  self::RESOLUTION,
179  self::RESOLUTION,
180  \Imagick::FILTER_LANCZOS,
181  1
182  );*/
183  $quality = $this->output_options->getQuality();
184 
185  // if $this->output_options->getFormat() is 'keep', we map it to the original format
186  if ($this->output_options->getFormat() === ImageOutputOptions::FORMAT_KEEP) {
187  try {
188  $this->output_options = $this->output_options->withFormat(strtolower($this->image->getImageFormat()));
189  } catch (\InvalidArgumentException) {
190  }
191  }
192 
193  switch ($this->output_options->getFormat()) {
195  $this->image->setImageFormat('webp');
196  $this->image->setImageAlphaChannel(\Imagick::ALPHACHANNEL_REMOVE);
197  $this->image = $this->image->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN);
198  if ($quality === 0) {
199  $this->image->setOption('webp:lossless', 'false');
200  }
201  if ($quality === 100) {
202  $this->image->setOption('webp:lossless', 'true');
203  }
204  break;
206  $this->image->setImageFormat('jpeg');
207  $this->image->setImageAlphaChannel(\Imagick::ALPHACHANNEL_REMOVE);
208  $this->image = $this->image->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN);
209  $this->image->setImageCompression(\Imagick::COMPRESSION_JPEG);
210  $this->image->setImageCompressionQuality($quality);
211  break;
213  $png_compression_level = round($quality / 100 * 9, 0);
214  if ($this->requested_background !== null && $this->requested_background !== 'transparent') {
215  $this->image->setImageAlphaChannel(\Imagick::ALPHACHANNEL_REMOVE);
216  } else {
217  $this->image->setImageAlphaChannel(\Imagick::ALPHACHANNEL_ACTIVATE);
218  }
219  $this->image->setImageFormat('png');
220  $this->image->setOption('png:compression-level', (string) $png_compression_level);
221  break;
222  }
223  $this->image->stripImage();
224  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
+ Here is the caller graph for this function:

◆ handleImageDimension()

ILIAS\Filesystem\Util\Convert\ImageConverter::handleImageDimension ( )
protected

Definition at line 75 of file ImageConverter.php.

References ILIAS\Filesystem\Util\Convert\ImageConversionOptions\DIMENSION_MODE_FIT, ILIAS\Filesystem\Util\Convert\ImageConversionOptions\DIMENSION_MODE_KEEP, ILIAS\Filesystem\Util\Convert\ImageConversionOptions\DIMENSION_MODE_NONE, ILIAS\Filesystem\Util\Convert\ImageConversionOptions\DIMENSTION_MODE_RESIZE_BY_HEIGHT, ILIAS\Filesystem\Util\Convert\ImageConversionOptions\DIMENSTION_MODE_RESIZE_BY_WIDTH, ILIAS\Filesystem\Util\Convert\ImageConversionOptions\DIMENSTION_MODE_RESIZE_TO_FIXED, ILIAS\Filesystem\Util\Convert\ImageConverter\doCrop(), ILIAS\Filesystem\Util\Convert\ImageConverter\doResize(), ILIAS\Filesystem\Util\Convert\ImageConverter\doThumbnail(), and null.

Referenced by ILIAS\Filesystem\Util\Convert\ImageConverter\convert().

75  : void
76  {
77  $requested_width = $this->conversion_options->getWidth();
78  $requested_height = $this->conversion_options->getHeight();
79  $original_image_width = $this->image->getImageWidth();
80  $original_image_height = $this->image->getImageHeight();
81 
82  switch ($this->conversion_options->getDimensionMode()) {
83  default:
85  // no resizing
86  return;
88  if ($this->conversion_options->hasCrop()) {
89  $final_height = $requested_height;
90  $final_width = $requested_width;
91  } else {
92  // this is a special case, where we want to fit the image into the given dimensions and
93  // Imagick knows the thumbnail method for that
94  $this->doThumbnail(
95  $requested_width,
96  $requested_height
97  );
98  return;
99  }
100  break;
102  // by width and height
103  if ($requested_width > 0 && $requested_height > 0) {
104  $final_width = $requested_width;
105  $final_height = $requested_height;
106  } else {
107  throw new \InvalidArgumentException('Dimension Mode does not match the given width/height');
108  }
109  break;
111  // by height
112  if ($requested_width === null && $requested_height > 0) {
113  $ratio = $original_image_height / $requested_height;
114  $final_width = intval($original_image_width / $ratio);
115  $final_height = $requested_height;
116  $l = 1;
117  } else {
118  throw new \InvalidArgumentException('Dimension Mode does not match the given width/height');
119  }
120  break;
122  // by width
123  if ($requested_width > 0 && $requested_height === null) {
124  $ratio = $original_image_width / $requested_width;
125  $final_width = $requested_width;
126  $final_height = intval($original_image_height / $ratio);
127  } else {
128  throw new \InvalidArgumentException('Dimension Mode does not match the given width/height');
129  }
130  break;
132  // by none of them
133  if ($requested_width === null && $requested_height === null) {
134  $final_width = $original_image_width;
135  $final_height = $original_image_height;
136  } else {
137  throw new \InvalidArgumentException('Dimension Mode does not match the given width/height');
138  }
139  break;
140  }
141 
142  if ($this->conversion_options->hasCrop()) {
143  $this->doCrop(
144  $final_width,
145  $final_height
146  );
147  } else {
148  $this->doResize(
149  $final_width,
150  $final_height
151  );
152  }
153  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isOK()

ILIAS\Filesystem\Util\Convert\ImageConverter::isOK ( )

Definition at line 252 of file ImageConverter.php.

Referenced by ILIAS\Filesystem\Util\Convert\LegacyImages\storeStream().

252  : bool
253  {
254  return $this->status === self::STATUS_OK;
255  }
+ Here is the caller graph for this function:

◆ readInputStream()

ILIAS\Filesystem\Util\Convert\ImageConverter::readInputStream ( )
protected

Definition at line 242 of file ImageConverter.php.

Referenced by ILIAS\Filesystem\Util\Convert\ImageConverter\convert().

242  : void
243  {
244  if ($this->conversion_options->makeTemporaryFiles()) {
245  $this->stream = $this->maybeSafeToTempStream($this->stream);
246  }
247  $this->stream->rewind();
248  $this->image->readImageFile($this->stream->detach());
249  }
+ Here is the caller graph for this function:

Field Documentation

◆ $image

Imagick ILIAS\Filesystem\Util\Convert\ImageConverter::$image
protected

Definition at line 46 of file ImageConverter.php.

◆ $output_stream

FileStream ILIAS\Filesystem\Util\Convert\ImageConverter::$output_stream = null
protected

◆ $requested_background

string ILIAS\Filesystem\Util\Convert\ImageConverter::$requested_background = null
protected

Definition at line 45 of file ImageConverter.php.

◆ $status

int ILIAS\Filesystem\Util\Convert\ImageConverter::$status = self::STATUS_UNKNOWN
protected

Definition at line 42 of file ImageConverter.php.

◆ $throwable

Throwable ILIAS\Filesystem\Util\Convert\ImageConverter::$throwable = null
protected

◆ RESOLUTION

const ILIAS\Filesystem\Util\Convert\ImageConverter::RESOLUTION = 72
protected

Definition at line 39 of file ImageConverter.php.

◆ RESOLUTION_FACTOR

const ILIAS\Filesystem\Util\Convert\ImageConverter::RESOLUTION_FACTOR = self::RESOLUTION / 72
protected

Definition at line 40 of file ImageConverter.php.

◆ STATUS_FAILED

const ILIAS\Filesystem\Util\Convert\ImageConverter::STATUS_FAILED = 2
protected

Definition at line 36 of file ImageConverter.php.

◆ STATUS_OK

const ILIAS\Filesystem\Util\Convert\ImageConverter::STATUS_OK = 1
protected

Definition at line 35 of file ImageConverter.php.

◆ STATUS_UNKNOWN

const ILIAS\Filesystem\Util\Convert\ImageConverter::STATUS_UNKNOWN = 4
protected

Definition at line 37 of file ImageConverter.php.


The documentation for this class was generated from the following file: