ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
GdImageToStreamTrait.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use ILIAS\ResourceStorage\Flavour\Engine\PHPMemoryLimit;
26
32{
33 use PHPMemoryLimit;
38 protected function to(\GdImage $image, ?int $quality = null): FileStream
39 {
40 ob_start();
41 imagejpeg($image, null, $quality ?? 75);
42 $stringdata = ob_get_contents();
43 imagedestroy($image);
44 ob_end_clean();
45
46 return Streams::ofString($stringdata);
47 }
48
49 protected function from(FileStream $stream): ?\GdImage
50 {
51 if ($stream->getSize() > $this->getSizeLimitInBytes()) {
52 return null;
53 }
54
55 try {
56 // we try to use the most common formats first
57 // this is faster than using imagecreatefromstring
58 // and also more memory efficient
59 $filename = $stream->getMetadata('uri');
60 $mime = mime_content_type($filename);
61 return match ($mime) {
62 'image/jpeg' => imagecreatefromjpeg($filename),
63 'image/png' => imagecreatefrompng($filename),
64 'image/gif' => imagecreatefromgif($filename),
65 'image/bmp' => imagecreatefrombmp($filename),
66 'image/webp' => imagecreatefromwebp($filename),
67 default => imagecreatefromstring((string) $stream)
68 };
69 } catch (\Throwable) {
70 return null;
71 }
72 }
73}
$filename
Definition: buildRTE.php:78
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
The base interface for all filesystem streams.
Definition: FileStream.php:32
to(\GdImage $image, ?int $quality=null)
Currently this is the only way to make a FileStream from a GD image resource.