ILIAS  trunk Revision v11.0_alpha-1731-gff9cd7e2bd3
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
GdImageToStreamTrait.php
Go to the documentation of this file.
1 <?php
18 declare(strict_types=1);
19 
21 
25 
31 {
32  use PHPMemoryLimit;
37  protected function to(\GdImage $image, ?int $quality = null): FileStream
38  {
39  ob_start();
40  imagejpeg($image, null, $quality ?? 75);
41  $stringdata = ob_get_contents();
42  imagedestroy($image);
43  ob_end_clean();
44 
45  return Streams::ofString($stringdata);
46  }
47 
48  protected function from(FileStream $stream): ?\GdImage
49  {
50  if ($stream->getSize() > $this->getSizeLimitInBytes()) {
51  return null;
52  }
53 
54  try {
55  // we try to use the most common formats first
56  // this is faster than using imagecreatefromstring
57  // and also more memory efficient
58  $filename = $stream->getMetadata('uri');
59  $mime = mime_content_type($filename);
60  return match ($mime) {
61  'image/jpeg' => imagecreatefromjpeg($filename),
62  'image/png' => imagecreatefrompng($filename),
63  'image/gif' => imagecreatefromgif($filename),
64  'image/bmp' => imagecreatefrombmp($filename),
65  'image/webp' => imagecreatefromwebp($filename),
66  default => imagecreatefromstring((string) $stream)
67  };
68  } catch (\Throwable) {
69  return null;
70  }
71  }
72 }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$filename
Definition: buildRTE.php:78
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
to(\GdImage $image, ?int $quality=null)
Currently this is the only way to make a FileStream from a GD image resource.
The base interface for all filesystem streams.
Definition: FileStream.php:31