ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
Images.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
25 
30 class Images
31 {
34 
38  public function __construct(
39  bool $throw_on_error = false
40  ) {
41  // Defaults
42  $this->conversion_options = (new ImageConversionOptions())
43  ->withMakeTemporaryFiles(false)
44  ->withThrowOnError($throw_on_error);
45  $this->image_output_options = (new ImageOutputOptions())
46  ->withJpgOutput()
47  ->withQuality(75);
48  }
49 
54  public function thumbnail(
55  FileStream $stream,
56  int $fit_into_size,
57  ImageOutputOptions $image_output_options = null
58  ): ImageConverter {
59  return new ImageConverter(
60  $this->conversion_options
61  ->withFitIn($fit_into_size)
62  ->withCrop(false)
63  ->withKeepAspectRatio(true),
64  $this->merge($image_output_options),
65  $stream
66  );
67  }
68 
73  public function croppedSquare(
74  FileStream $stream,
75  int $square_size,
76  ImageOutputOptions $image_output_options = null
77  ): ImageConverter {
78  return new ImageConverter(
79  $this->conversion_options
80  ->withFitIn($square_size)
81  ->withKeepAspectRatio(true)
82  ->withCrop(true),
83  $this->merge($image_output_options),
84  $stream
85  );
86  }
87 
88 
94  public function resizeByWidth(
95  FileStream $stream,
96  int $width,
97  ImageOutputOptions $image_output_options = null
98  ): ImageConverter {
99  return new ImageConverter(
100  $this->conversion_options
101  ->withWidth($width)
102  ->withKeepAspectRatio(true),
103  $this->merge($image_output_options),
104  $stream
105  );
106  }
107 
108 
114  public function resizeByHeight(
115  FileStream $stream,
116  int $height,
117  ImageOutputOptions $image_output_options = null
118  ): ImageConverter {
119  return new ImageConverter(
120  $this->conversion_options
121  ->withHeight($height)
122  ->withKeepAspectRatio(true),
123  $this->merge($image_output_options),
124  $stream
125  );
126  }
127 
128 
134  public function resizeToFixedSize(
135  FileStream $stream,
136  int $width,
137  int $height,
138  bool $crop_or_otherwise_squeeze = true,
139  ImageOutputOptions $image_output_options = null
140  ): ImageConverter {
141  return new ImageConverter(
142  $this->conversion_options
143  ->withWidth($width)
144  ->withHeight($height)
145  ->withCrop($crop_or_otherwise_squeeze)
146  ->withKeepAspectRatio(true),
147  $this->merge($image_output_options),
148  $stream
149  );
150  }
151 
159  public function convertToFormat(
160  FileStream $stream,
161  string $to_format,
162  ?int $width = null,
163  ?int $height = null,
164  ImageOutputOptions $image_output_options = null
165  ): ImageConverter {
166  $conversion_options = $this->conversion_options
167  ->withKeepAspectRatio(true)
168  ->withCrop(true);
169 
170  if ($height !== null) {
171  $conversion_options = $conversion_options->withHeight($height);
172  }
173  if ($width !== null) {
174  $conversion_options = $conversion_options->withWidth($width);
175  }
176  if ($width === null && $height === null) {
177  $conversion_options = $conversion_options->withKeepDimensions(true);
178  }
179  return new ImageConverter(
180  $conversion_options,
181  $this->merge($image_output_options)->withFormat($to_format),
182  $stream
183  );
184  }
185 
186  private function merge(?ImageOutputOptions $image_output_options): ImageOutputOptions
187  {
188  if ($image_output_options !== null) {
189  return $this->image_output_options
190  ->withQuality($image_output_options->getQuality())
191  ->withFormat($image_output_options->getFormat());
192  }
194  }
195 }
resizeByHeight(FileStream $stream, int $height, ImageOutputOptions $image_output_options=null)
Resizes an image to an image with the given height.
Definition: Images.php:114
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:159
withWidth(int $width)
Resize the image to the given width.
withKeepAspectRatio(bool $keep_aspect_ratio)
Keep the aspect ratio while resizing the image.
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:54
ImageConversionOptions $conversion_options
Definition: Images.php:32
withKeepDimensions(bool $keep)
No resizing, the original image dimension will be used.
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:73
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:134
withQuality(int $image_quality)
set the image compression quality.
__construct(bool $throw_on_error=false)
Definition: Images.php:38
ImageOutputOptions $image_output_options
Definition: Images.php:33
merge(?ImageOutputOptions $image_output_options)
Definition: Images.php:186
The base interface for all filesystem streams.
Definition: FileStream.php:31
withHeight(int $height)
Resize the image to the given height.
resizeByWidth(FileStream $stream, int $width, ImageOutputOptions $image_output_options=null)
Resizes an image to an image with the given width.
Definition: Images.php:94