ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Images.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24
29class 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,
59 $this->conversion_options
60 ->withFitIn($fit_into_size)
61 ->withCrop(false)
62 ->withKeepAspectRatio(true),
64 $stream
65 );
66 }
67
72 public function croppedSquare(
73 FileStream $stream,
74 int $square_size,
75 ?ImageOutputOptions $image_output_options = null
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
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 {
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 {
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 }
192 return $this->image_output_options;
193 }
194}
withHeight(int $height)
@description Resize the image to the given height.
withWidth(int $width)
@description Resize the image to the given width.
withKeepDimensions(bool $keep)
@description No resizing, the original image dimension will be used.
withQuality(int $image_quality)
@description set the image compression quality.
__construct(bool $throw_on_error=false)
Definition: Images.php:37
ImageConversionOptions $conversion_options
Definition: Images.php:31
resizeByWidth(FileStream $stream, int $width, ?ImageOutputOptions $image_output_options=null)
@description Resizes an image to an image with the given width.
Definition: Images.php:93
convertToFormat(FileStream $stream, string $to_format, ?int $width=null, ?int $height=null, ?ImageOutputOptions $image_output_options=null)
@description Creates an image from the given stream, converted to the desired format.
Definition: Images.php:158
resizeToFixedSize(FileStream $stream, int $width, int $height, bool $crop_or_otherwise_squeeze=true, ?ImageOutputOptions $image_output_options=null)
@description Creates an image from the given stream, resized to width and height given.
Definition: Images.php:133
croppedSquare(FileStream $stream, int $square_size, ?ImageOutputOptions $image_output_options=null)
@description Creates an image from the given stream which fits into the given size,...
Definition: Images.php:72
resizeByHeight(FileStream $stream, int $height, ?ImageOutputOptions $image_output_options=null)
@description Resizes an image to an image with the given height.
Definition: Images.php:113
ImageOutputOptions $image_output_options
Definition: Images.php:32
thumbnail(FileStream $stream, int $fit_into_size, ?ImageOutputOptions $image_output_options=null)
@description Creates an image from the given stream which fits into the given size and keeps the aspe...
Definition: Images.php:53
merge(?ImageOutputOptions $image_output_options)
Definition: Images.php:185
return true
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
The base interface for all filesystem streams.
Definition: FileStream.php:32