39 protected const W =
'width';
40 protected const H =
'height';
46 protected function setUp(): void
49 $this->images =
new Images(
56 $img = __DIR__ .
'/img/robot.jpg';
57 $this->assertFileExists($img);
58 $getimagesize = getimagesize($img);
59 $original_width = $getimagesize[0];
60 $original_height = $getimagesize[1];
61 $this->assertEquals(600, $original_width);
62 $this->assertEquals(800, $original_height);
65 $original_stream = Streams::ofResource(fopen($img,
'rb'));
67 $thumbnail_converter = $this->images->thumbnail(
71 $this->assertTrue($thumbnail_converter->isOK());
72 $this->assertNull($thumbnail_converter->getThrowableIfAny());
73 $converted_stream = $thumbnail_converter->getStream();
75 $getimagesizefromstring = getimagesizefromstring((
string) $converted_stream);
77 $this->assertEquals(75, $getimagesizefromstring[0]);
78 $this->assertEquals(100, $getimagesizefromstring[1]);
83 $img = __DIR__ .
'/img/robot.jpg';
84 $this->assertFileExists($img);
85 $getimagesize = getimagesize($img);
86 $original_width = $getimagesize[0];
87 $original_height = $getimagesize[1];
88 $this->assertEquals(600, $original_width);
89 $this->assertEquals(800, $original_height);
92 $original_stream = Streams::ofResource(fopen($img,
'rb'));
94 $thumbnail_converter = $this->images->croppedSquare(
98 $this->assertTrue($thumbnail_converter->isOK());
99 $this->assertNull($thumbnail_converter->getThrowableIfAny());
103 $this->assertEquals(200, $getimagesizefromstring[self::W]);
104 $this->assertEquals(200, $getimagesizefromstring[self::H]);
110 [400, 300, self::BY_WIDTH_FINAL, 192],
111 [300, 400, self::BY_WIDTH_FINAL, 341],
112 [543, 431, self::BY_WIDTH_FINAL, 203],
113 [200, 200, self::BY_WIDTH_FINAL, 256],
117 #[DataProvider('getImageSizesByWidth')] 126 $this->assertEquals($width, $dimensions[self::W]);
127 $this->assertEquals($height, $dimensions[self::H]);
130 $resized = $this->images->resizeByWidth($stream, self::BY_WIDTH_FINAL);
131 $this->assertTrue($resized->isOK());
134 $this->assertEquals($final_width, $new_dimensions[self::W]);
135 $this->assertEquals($final_height, $new_dimensions[self::H]);
139 round($width > $height),
140 round($final_width > $final_height)
143 round($width / $height),
144 round($new_dimensions[self::W] / $new_dimensions[self::H])
152 $new_dimensions[self::W] > $new_dimensions[self::H]
159 [400, 300, self::BY_HEIGHT_FINAL, 1008],
160 [300, 400, self::BY_HEIGHT_FINAL, 567],
161 [200, 200, self::BY_HEIGHT_FINAL, 756],
162 [248, 845, self::BY_HEIGHT_FINAL, 221],
167 #[DataProvider('getImageSizesByHeight')] 176 $this->assertEquals($width, $dimensions[self::W]);
177 $this->assertEquals($height, $dimensions[self::H]);
180 $resized = $this->images->resizeByHeight($stream, self::BY_HEIGHT_FINAL);
181 $this->assertTrue($resized->isOK());
184 $this->assertEquals($final_width, $new_dimensions[self::W]);
185 $this->assertEquals($final_height, $new_dimensions[self::H]);
189 round($width > $height),
190 round($final_width > $final_height)
193 round($width / $height),
194 round($new_dimensions[self::W] / $new_dimensions[self::H])
202 $new_dimensions[self::W] > $new_dimensions[self::H]
209 [1024, 768, 300, 100,
true],
210 [1024, 768, 300, 100,
false],
211 [1024, 768, 100, 300,
true],
212 [1024, 768, 100, 300,
false],
213 [400, 300, 500, 400,
true],
214 [400, 300, 500, 400,
false],
218 #[DataProvider('getImageSizesByFixed')] 228 $this->assertEquals($width, $dimensions[self::W]);
229 $this->assertEquals($height, $dimensions[self::H]);
231 $by_fixed = $this->images->resizeToFixedSize($stream, $final_width, $final_height, $crop);
232 $this->assertTrue($by_fixed->isOK());
235 $this->assertEquals($final_width, $new_dimensions[self::W]);
236 $this->assertEquals($final_height, $new_dimensions[self::H]);
241 $options =
new ImageOutputOptions();
243 [$options, self::IMAGE_JPEG, 75],
244 [$options->withPngOutput()->withQuality(22), self::IMAGE_PNG, 0],
245 [$options->withJpgOutput()->withQuality(100), self::IMAGE_JPEG, 100],
246 [$options->withFormat(
'png')->withQuality(50), self::IMAGE_PNG, 0],
247 [$options->withFormat(
'jpg')->withQuality(87), self::IMAGE_JPEG, 87],
248 [$options->withQuality(5)->withJpgOutput(), self::IMAGE_JPEG, 5],
249 [$options->withQuality(10)->withJpgOutput(), self::IMAGE_JPEG, 10],
250 [$options->withQuality(35)->withJpgOutput(), self::IMAGE_JPEG, 35],
251 [$options->withQuality(0)->withWebPOutput(), self::IMAGE_WEBP, 0],
252 [$options->withQuality(100)->withWebPOutput(), self::IMAGE_WEBP, 100],
257 #[DataProvider('getImageOptions')] 259 ImageOutputOptions $options,
260 string $expected_mime_type,
261 int $expected_quality
263 $resized = $this->images->resizeToFixedSize(
277 $options =
new ImageOutputOptions();
280 $this->assertEquals(
'jpg', $options->getFormat());
281 $this->assertEquals(75, $options->getQuality());
283 $png = $options->withPngOutput();
284 $this->assertEquals(
'png', $png->getFormat());
285 $this->assertEquals(
'jpg', $options->getFormat());
286 $png_explicit = $options->withFormat(
'png');
287 $this->assertEquals(
'png', $png_explicit->getFormat());
289 $jpg = $options->withJpgOutput();
290 $this->assertEquals(
'jpg', $jpg->getFormat());
291 $jpg_explicit = $options->withFormat(
'jpg');
292 $this->assertEquals(
'jpg', $jpg_explicit->getFormat());
293 $jpeg = $options->withFormat(
'jpeg');
294 $this->assertEquals(
'jpg', $jpeg->getFormat());
297 $low = $options->withQuality(5);
298 $this->assertEquals(5, $low->getQuality());
299 $this->assertEquals(75, $options->getQuality());
311 #[DataProvider('getWrongFormats')] 314 $options =
new ImageOutputOptions();
315 $this->expectException(\InvalidArgumentException::class);
316 $wrong = $options->withFormat($format);
328 #[DataProvider('getWrongQualites')] 331 $options =
new ImageOutputOptions();
332 $this->expectException(\InvalidArgumentException::class);
333 $wrong = $options->withQuality($quality);
339 $png = $this->images->convertToFormat(
346 $this->assertEquals(10, $size[self::W]);
347 $this->assertEquals(10, $size[self::H]);
351 $png = $this->images->convertToFormat(
360 $this->assertEquals(20, $size[self::W]);
361 $this->assertEquals(20, $size[self::H]);
366 $false_stream = Streams::ofString(
'false');
367 $images =
new Images(
372 $resized = $images->resizeToFixedSize(
377 $this->assertFalse($resized->isOK());
378 $this->assertInstanceOf(\Throwable::class, $resized->getThrowableIfAny());
399 private function colorDiff(
string $hex_color_one,
string $hex_color_two):
int 401 preg_match(
'/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i', $hex_color_one, $rgb_one);
402 preg_match(
'/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i', $hex_color_two, $rgb_two);
404 return abs(hexdec($rgb_one[1]) - hexdec($rgb_two[1]))
405 + abs(hexdec($rgb_one[2]) - hexdec($rgb_two[2]))
406 + abs(hexdec($rgb_one[3]) - hexdec($rgb_two[3]));
409 #[DataProvider('getColors')] 412 $transparent_png = __DIR__ .
'/img/transparent.png';
413 $this->assertFileExists($transparent_png);
414 $png = Streams::ofResource(fopen($transparent_png,
'rb'));
416 $converter_options = (
new ImageConversionOptions())
417 ->withThrowOnError(
true)
418 ->withFixedDimensions(100, 100);
420 if ($color !==
null) {
421 $converter_options = $converter_options->withBackgroundColor($color);
426 $output_options = (
new ImageOutputOptions())
430 $converter =
new ImageConverter($converter_options, $output_options, $png);
431 $this->assertTrue($converter->isOK());
432 $converted_stream = $converter->getStream();
433 $gd_image = imagecreatefromstring((
string) $converted_stream);
434 $colors = imagecolorsforindex($gd_image, imagecolorat($gd_image, 1, 1));
436 $color_in_converted_picture = sprintf(
"#%02x%02x%02x", $colors[
'red'], $colors[
'green'], $colors[
'blue']);
437 $color_diff = $this->
colorDiff($color, $color_in_converted_picture);
439 $this->assertLessThan(3, $color_diff);
446 $output_path = __DIR__ .
'/img/output.jpg';
447 $converter_options = (
new ImageConversionOptions())
448 ->withThrowOnError(
true)
449 ->withMakeTemporaryFiles(
false)
450 ->withFixedDimensions(100, 100)
451 ->withOutputPath($output_path);
453 $output_options = (
new ImageOutputOptions())
457 $converter =
new ImageConverter($converter_options, $output_options, $img);
458 $this->assertTrue($converter->isOK());
460 $this->assertFileExists($output_path);
461 $stream = $converter->getStream();
462 $this->assertEquals($output_path, $stream->getMetadata(
'uri'));
464 unlink($output_path);
469 $file =
'https://upload.wikimedia.org/wikipedia/commons/5/5e/Jan_Vermeer_-_The_Art_of_Painting_-_Google_Art_Project.jpg';
471 curl_setopt($curl, CURLOPT_URL, $file);
472 curl_setopt($curl, CURLOPT_RETURNTRANSFER,
true);
473 curl_setopt($curl, CURLOPT_USERAGENT,
'PHPUnit/1.0');
474 $string = curl_exec($curl);
477 $img = Streams::ofString($string);
478 $this->assertInstanceOf(FileStream::class, $img);
480 $converter_options = (
new ImageConversionOptions())
483 ->withKeepAspectRatio(
true)
485 ->withThrowOnError(
true);
487 $output_options = (
new ImageOutputOptions())
489 ->withFormat(ImageOutputOptions::FORMAT_PNG);
491 $converter =
new ImageConverter($converter_options, $output_options, $img);
492 $this->assertTrue($converter->isOK());
498 if (!class_exists(
'Imagick')) {
499 $this->markTestSkipped(
'Imagick not installed');
505 $getimagesizefromstring = getimagesizefromstring((
string) $stream);
507 self::W => (
int) round($getimagesizefromstring[0]),
508 self::H => (
int) round($getimagesizefromstring[1])
514 return finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $stream->read(255));
520 $img = new \Imagick();
521 $img->readImageBlob((
string) $stream);
523 return $img->getImageCompressionQuality();
528 $img = new \Imagick();
529 $img->newImage($width, $height,
new \ImagickPixel(
'black'));
530 $img->setImageFormat(
'jpg');
532 $stream = Streams::ofString($img->getImageBlob());
static getImageSizesByFixed()
testResizeToFitWidth(int $width, int $height, int $final_width, int $final_height)
testImageOutputOptionSanity()
testWrongFormats(string $format)
static getImageSizesByHeight()
testResizeByFixedSize(int $width, int $height, int $final_width, int $final_height, bool $crop)
static getWrongQualites()
static getImageSizesByWidth()
testImageOutputOptions(ImageOutputOptions $options, string $expected_mime_type, int $expected_quality)
getImageTypeFromStream(FileStream $stream)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
testImageThumbnailActualImage()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
createTestImageStream(int $width, int $height)
colorDiff(string $hex_color_one, string $hex_color_two)
testResizeToFitHeight(int $width, int $height, int $final_height, int $final_width)
getImageQualityFromStream(FileStream $stream)
testWrongQualities(int $quality)
testImageSquareActualImage()
getImageSizeFromStream(FileStream $stream)
The base interface for all filesystem streams.
testBackgroundColor(?string $color)