ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ImageConversionOptions.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28{
29 public const DIMENSION_MODE_NONE = 0;
30 public const DIMENSION_MODE_FIT = 1;
34 public const DIMENSION_MODE_KEEP = 5;
35
37 private ?int $height = null;
38 private ?int $width = null;
39 private bool $keep_aspect_ratio = true;
40 private ?string $background_color = null;
41 private ?string $output_path = null;
42 private bool $crop = true;
43 private bool $throw_on_error = false;
44 private bool $make_temporary_files = false;
45
50 public function withThrowOnError(bool $throw_on_error): self
51 {
52 $clone = clone $this;
53 $clone->throw_on_error = $throw_on_error;
54 return $clone;
55 }
56
64 {
65 $clone = clone $this;
66 $clone->make_temporary_files = $make_temporary_files;
67 return $clone;
68 }
69
73 public function withFitIn(int $max_size): ImageConversionOptions
74 {
75 $clone = clone $this;
76 $clone->dimension_mode = self::DIMENSION_MODE_FIT;
77 $clone->width = $clone->height = $max_size;
78 return $clone;
79 }
80
85 {
86 $clone = clone $this;
87 $clone->keep_aspect_ratio = $keep_aspect_ratio;
88 return $clone;
89 }
90
94 public function withCrop(bool $crop): ImageConversionOptions
95 {
96 $clone = clone $this;
97 $clone->crop = $crop;
98 return $clone;
99 }
100
104 public function withKeepDimensions(bool $keep): ImageConversionOptions
105 {
106 $clone = clone $this;
107 $clone->dimension_mode = self::DIMENSION_MODE_KEEP;
108 $this->width = $this->height = null;
109 return $clone;
110 }
111
116 {
117 $clone = clone $this;
118 $clone->dimension_mode = ($this->height === null) ? self::DIMENSTION_MODE_RESIZE_BY_WIDTH : self::DIMENSTION_MODE_RESIZE_TO_FIXED;
119 $clone->width = $width;
120 return $clone;
121 }
122
127 {
128 $clone = clone $this;
129 $clone->dimension_mode = ($this->width === null) ? self::DIMENSTION_MODE_RESIZE_BY_HEIGHT : self::DIMENSTION_MODE_RESIZE_TO_FIXED;
130 $clone->height = $height;
131 return $clone;
132 }
133
138 {
139 return $this->withHeight($height)->withWidth($width);
140 }
141
147 {
148 $clone = clone $this;
149 $clone->output_path = $output_path;
150 return $clone;
151 }
152
158 {
159 $this->checkBackgroundColor($background_color);
160
161 $clone = clone $this;
162 $clone->background_color = $background_color;
163 return $clone;
164 }
165
166 public function getBackgroundColor(): ?string
167 {
169 }
170
171 public function getOutputPath(): ?string
172 {
173 return $this->output_path;
174 }
175
176
177 public function getDimensionMode(): int
178 {
180 }
181
182 public function getHeight(): ?int
183 {
184 return $this->height;
185 }
186
187 public function getWidth(): ?int
188 {
189 return $this->width;
190 }
191
192
193 public function keepAspectRatio(): bool
194 {
196 }
197
198
199 public function hasCrop(): bool
200 {
201 return $this->crop;
202 }
203
204 public function throwOnError(): bool
205 {
207 }
208
209 public function makeTemporaryFiles(): bool
210 {
212 }
213
214 protected function checkBackgroundColor(string $background_color): void
215 {
216 if (!preg_match('/^#?([a-f0-9]{6})$/i', $background_color)) {
217 throw new \InvalidArgumentException('Invalid background color');
218 }
219 }
220}
withHeight(int $height)
@description Resize the image to the given height.
withMakeTemporaryFiles(bool $make_temporary_files)
@description if passing a stream from memory, make a temporary file for this.
withFixedDimensions(int $width, int $height)
@description Resizes the Image to a fixed size.
withThrowOnError(bool $throw_on_error)
@description If there is any throwable during convertion, this will be thworn again.
withCrop(bool $crop)
@description Crops the final image if needed.
withFitIn(int $max_size)
@description Fit the image into the given size.
withWidth(int $width)
@description Resize the image to the given width.
withBackgroundColor(string $background_color)
@description Set a background color for the image.
withKeepDimensions(bool $keep)
@description No resizing, the original image dimension will be used.
withKeepAspectRatio(bool $keep_aspect_ratio)
@description Keep the aspect ratio while resizing the image.