ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
LegacyImages.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
26
32{
35
36 public function __construct()
37 {
38 $this->image_converters = new Images(false);
39 $this->output_options = new ImageOutputOptions();
40 }
41 //
42 // Thumbnails
43 //
44
49 public function thumbnail(
50 string $path_to_original,
51 string $path_to_output,
52 int $fit_into_size,
53 string $output_format = ImageOutputOptions::FORMAT_KEEP,
54 int $image_quality = 75
55 ): string {
56 $converter = $this->image_converters->thumbnail(
57 $this->buildStream($path_to_original),
58 $fit_into_size,
59 $this->output_options
60 ->withQuality($image_quality)
61 ->withFormat($output_format)
62 );
63 return $this->storeStream($converter, $path_to_output);
64 }
65
66 //
67 // Cropped Square
68 //
69
74 public function croppedSquare(
75 string $path_to_original,
76 string $path_to_output,
77 int $square_size,
78 string $output_format = ImageOutputOptions::FORMAT_KEEP,
79 int $image_quality = 75
80 ): string {
81 $converter = $this->image_converters->croppedSquare(
82 $this->buildStream($path_to_original),
83 $square_size,
84 $this->output_options
85 ->withQuality($image_quality)
86 ->withFormat($output_format)
87 );
88
89 return $this->storeStream($converter, $path_to_output);
90 }
91
92 //
93 // Resize
94 //
95
100 public function resizeByWidth(
101 string $path_to_original,
102 string $path_to_output,
103 int $width,
104 string $output_format = ImageOutputOptions::FORMAT_KEEP,
105 int $image_quality = 60
106 ): string {
107 $converter = $this->image_converters->resizeByWidth(
108 $this->buildStream($path_to_original),
109 $width,
110 $this->output_options
111 ->withQuality($image_quality)
112 ->withFormat($output_format)
113 );
114 return $this->storeStream($converter, $path_to_output);
115 }
116
117
122 public function resizeByHeight(
123 string $path_to_original,
124 string $path_to_output,
125 int $height,
126 string $output_format = ImageOutputOptions::FORMAT_KEEP,
127 int $image_quality = 60
128 ): string {
129 $converter = $this->image_converters->resizeByHeight(
130 $this->buildStream($path_to_original),
131 $height,
132 $this->output_options
133 ->withQuality($image_quality)
134 ->withFormat($output_format)
135 );
136 return $this->storeStream($converter, $path_to_output);
137 }
138
139
144 public function resizeToFixedSize(
145 string $path_to_original,
146 string $path_to_output,
147 int $width,
148 int $height,
149 bool $crop_if_true_and_resize_if_false = true,
150 string $output_format = ImageOutputOptions::FORMAT_KEEP,
151 int $image_quality = 60
152 ): string {
153 $converter = $this->image_converters->resizeToFixedSize(
154 $this->buildStream($path_to_original),
155 $width,
156 $height,
157 $crop_if_true_and_resize_if_false,
158 $this->output_options
159 ->withQuality($image_quality)
160 ->withFormat($output_format)
161 );
162 return $this->storeStream($converter, $path_to_output);
163 }
164
165 public function convertToFormat(
166 string $path_to_original,
167 string $path_to_output,
168 string $output_format,
169 ?int $width = null,
170 ?int $height = null
171 ): string {
172 $converter = $this->image_converters->convertToFormat(
173 $this->buildStream($path_to_original),
174 $output_format,
175 $width,
176 $height
177 );
178 return $this->storeStream($converter, $path_to_output);
179 }
180
181 private function storeStream(ImageConverter $converter, string $path): string
182 {
183 if (!$converter->isOK()) {
184 throw $converter->getThrowableIfAny() ?? new \RuntimeException('Could not create requested image');
185 }
186
187 $stream = $converter->getStream();
188
189 $stream->rewind();
190 if (file_put_contents($path, $stream->getContents()) === false) {
191 throw new \RuntimeException('Could not store image');
192 }
193 return $path;
194 }
195
196 private function buildStream(string $path_to_original): Stream|FileStream
197 {
198 return Streams::ofResource(fopen($path_to_original, 'rb'));
199 }
200}
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
resizeToFixedSize(string $path_to_original, string $path_to_output, int $width, int $height, bool $crop_if_true_and_resize_if_false=true, string $output_format=ImageOutputOptions::FORMAT_KEEP, int $image_quality=60)
convertToFormat(string $path_to_original, string $path_to_output, string $output_format, ?int $width=null, ?int $height=null)
croppedSquare(string $path_to_original, string $path_to_output, int $square_size, string $output_format=ImageOutputOptions::FORMAT_KEEP, int $image_quality=75)
thumbnail(string $path_to_original, string $path_to_output, int $fit_into_size, string $output_format=ImageOutputOptions::FORMAT_KEEP, int $image_quality=75)
storeStream(ImageConverter $converter, string $path)
resizeByHeight(string $path_to_original, string $path_to_output, int $height, string $output_format=ImageOutputOptions::FORMAT_KEEP, int $image_quality=60)
resizeByWidth(string $path_to_original, string $path_to_output, int $width, string $output_format=ImageOutputOptions::FORMAT_KEEP, int $image_quality=60)
The base interface for all filesystem streams.
Definition: FileStream.php:32
$path
Definition: ltiservices.php:30