ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Images.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 
29 class Images
30 {
33 
37  public function __construct(
38  bool $throw_on_error = false
39  ) {
40  // Defaults
41  $this->conversion_options = (new ImageConversionOptions())
42  ->withMakeTemporaryFiles(false)
43  ->withThrowOnError($throw_on_error);
44  $this->image_output_options = (new ImageOutputOptions())
45  ->withJpgOutput()
46  ->withQuality(75);
47  }
48 
53  public function thumbnail(
54  FileStream $stream,
55  int $fit_into_size,
56  ?ImageOutputOptions $image_output_options = null
57  ): ImageConverter {
58  return new ImageConverter(
59  $this->conversion_options
60  ->withFitIn($fit_into_size)
61  ->withCrop(false)
62  ->withKeepAspectRatio(true),
63  $this->merge($image_output_options),
64  $stream
65  );
66  }
67 
72  public function croppedSquare(
73  FileStream $stream,
74  int $square_size,
75  ?ImageOutputOptions $image_output_options = null
76  ): ImageConverter {
77  return new ImageConverter(
78  $this->conversion_options
79  ->withFitIn($square_size)
80  ->withKeepAspectRatio(true)
81  ->withCrop(true),
82  $this->merge($image_output_options),
83  $stream
84  );
85  }
86 
87 
93  public function resizeByWidth(
94  FileStream $stream,
95  int $width,
96  ?ImageOutputOptions $image_output_options = null
97  ): ImageConverter {
98  return new ImageConverter(
99  $this->conversion_options
100  ->withWidth($width)
101  ->withKeepAspectRatio(true),
102  $this->merge($image_output_options),
103  $stream
104  );
105  }
106 
107 
113  public function resizeByHeight(
114  FileStream $stream,
115  int $height,
116  ?ImageOutputOptions $image_output_options = null
117  ): ImageConverter {
118  return new ImageConverter(
119  $this->conversion_options
120  ->withHeight($height)
121  ->withKeepAspectRatio(true),
122  $this->merge($image_output_options),
123  $stream
124  );
125  }
126 
127 
133  public function resizeToFixedSize(
134  FileStream $stream,
135  int $width,
136  int $height,
137  bool $crop_or_otherwise_squeeze = true,
138  ?ImageOutputOptions $image_output_options = null
139  ): ImageConverter {
140  return new ImageConverter(
141  $this->conversion_options
142  ->withWidth($width)
143  ->withHeight($height)
144  ->withCrop($crop_or_otherwise_squeeze)
145  ->withKeepAspectRatio(true),
146  $this->merge($image_output_options),
147  $stream
148  );
149  }
150 
158  public function convertToFormat(
159  FileStream $stream,
160  string $to_format,
161  ?int $width = null,
162  ?int $height = null,
163  ?ImageOutputOptions $image_output_options = null
164  ): ImageConverter {
165  $conversion_options = $this->conversion_options
166  ->withKeepAspectRatio(true)
167  ->withCrop(true);
168 
169  if ($height !== null) {
170  $conversion_options = $conversion_options->withHeight($height);
171  }
172  if ($width !== null) {
173  $conversion_options = $conversion_options->withWidth($width);
174  }
175  if ($width === null && $height === null) {
176  $conversion_options = $conversion_options->withKeepDimensions(true);
177  }
178  return new ImageConverter(
179  $conversion_options,
180  $this->merge($image_output_options)->withFormat($to_format),
181  $stream
182  );
183  }
184 
185  private function merge(?ImageOutputOptions $image_output_options): ImageOutputOptions
186  {
187  if ($image_output_options !== null) {
188  return $this->image_output_options
189  ->withQuality($image_output_options->getQuality())
190  ->withFormat($image_output_options->getFormat());
191  }
193  }
194 }
withWidth(int $width)
Resize the image to the given width.
withKeepAspectRatio(bool $keep_aspect_ratio)
Keep the aspect ratio while resizing the image.
convertToFormat(FileStream $stream, string $to_format, ?int $width=null, ?int $height=null, ?ImageOutputOptions $image_output_options=null)
Creates an image from the given stream, converted to the desired format.
Definition: Images.php:158
croppedSquare(FileStream $stream, int $square_size, ?ImageOutputOptions $image_output_options=null)
Creates an image from the given stream which fits into the given size, but is cropped to fill the who...
Definition: Images.php:72
ImageConversionOptions $conversion_options
Definition: Images.php:31
withKeepDimensions(bool $keep)
No resizing, the original image dimension will be used.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
resizeToFixedSize(FileStream $stream, int $width, int $height, bool $crop_or_otherwise_squeeze=true, ?ImageOutputOptions $image_output_options=null)
Creates an image from the given stream, resized to width and height given.
Definition: Images.php:133
withQuality(int $image_quality)
set the image compression quality.
resizeByHeight(FileStream $stream, int $height, ?ImageOutputOptions $image_output_options=null)
Resizes an image to an image with the given height.
Definition: Images.php:113
__construct(bool $throw_on_error=false)
Definition: Images.php:37
ImageOutputOptions $image_output_options
Definition: Images.php:32
merge(?ImageOutputOptions $image_output_options)
Definition: Images.php:185
thumbnail(FileStream $stream, int $fit_into_size, ?ImageOutputOptions $image_output_options=null)
Creates an image from the given stream which fits into the given size and keeps the aspect ratio...
Definition: Images.php:53
resizeByWidth(FileStream $stream, int $width, ?ImageOutputOptions $image_output_options=null)
Resizes an image to an image with the given width.
Definition: Images.php:93
The base interface for all filesystem streams.
Definition: FileStream.php:31
withHeight(int $height)
Resize the image to the given height.