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],
128 $this->assertEquals($width, $dimensions[self::W]);
129 $this->assertEquals($height, $dimensions[self::H]);
132 $resized = $this->images->resizeByWidth($stream, self::BY_WIDTH_FINAL);
133 $this->assertTrue($resized->isOK());
136 $this->assertEquals($final_width, $new_dimensions[self::W]);
137 $this->assertEquals($final_height, $new_dimensions[self::H]);
141 round($width > $height),
142 round($final_width > $final_height)
145 round($width / $height),
146 round($new_dimensions[self::W] / $new_dimensions[self::H])
154 $new_dimensions[self::W] > $new_dimensions[self::H]
161 [400, 300, self::BY_HEIGHT_FINAL, 1008],
162 [300, 400, self::BY_HEIGHT_FINAL, 567],
163 [200, 200, self::BY_HEIGHT_FINAL, 756],
164 [248, 845, self::BY_HEIGHT_FINAL, 221],
180 $this->assertEquals($width, $dimensions[self::W]);
181 $this->assertEquals($height, $dimensions[self::H]);
184 $resized = $this->images->resizeByHeight($stream, self::BY_HEIGHT_FINAL);
185 $this->assertTrue($resized->isOK());
188 $this->assertEquals($final_width, $new_dimensions[self::W]);
189 $this->assertEquals($final_height, $new_dimensions[self::H]);
193 round($width > $height),
194 round($final_width > $final_height)
197 round($width / $height),
198 round($new_dimensions[self::W] / $new_dimensions[self::H])
206 $new_dimensions[self::W] > $new_dimensions[self::H]
213 [1024, 768, 300, 100,
true],
214 [1024, 768, 300, 100,
false],
215 [1024, 768, 100, 300,
true],
216 [1024, 768, 100, 300,
false],
217 [400, 300, 500, 400,
true],
218 [400, 300, 500, 400,
false],
234 $this->assertEquals($width, $dimensions[self::W]);
235 $this->assertEquals($height, $dimensions[self::H]);
237 $by_fixed = $this->images->resizeToFixedSize($stream, $final_width, $final_height, $crop);
238 $this->assertTrue($by_fixed->isOK());
241 $this->assertEquals($final_width, $new_dimensions[self::W]);
242 $this->assertEquals($final_height, $new_dimensions[self::H]);
247 $options =
new ImageOutputOptions();
249 [$options, self::IMAGE_JPEG, 75],
250 [$options->withPngOutput()->withQuality(22), self::IMAGE_PNG, 0],
251 [$options->withJpgOutput()->withQuality(100), self::IMAGE_JPEG, 100],
252 [$options->withFormat(
'png')->withQuality(50), self::IMAGE_PNG, 0],
253 [$options->withFormat(
'jpg')->withQuality(87), self::IMAGE_JPEG, 87],
254 [$options->withQuality(5)->withJpgOutput(), self::IMAGE_JPEG, 5],
255 [$options->withQuality(10)->withJpgOutput(), self::IMAGE_JPEG, 10],
256 [$options->withQuality(35)->withJpgOutput(), self::IMAGE_JPEG, 35],
257 [$options->withQuality(0)->withWebPOutput(), self::IMAGE_WEBP, 0],
258 [$options->withQuality(100)->withWebPOutput(), self::IMAGE_WEBP, 100],
267 ImageOutputOptions $options,
268 string $expected_mime_type,
269 int $expected_quality
271 $resized = $this->images->resizeToFixedSize(
285 $options =
new ImageOutputOptions();
288 $this->assertEquals(
'jpg', $options->getFormat());
289 $this->assertEquals(75, $options->getQuality());
291 $png = $options->withPngOutput();
292 $this->assertEquals(
'png', $png->getFormat());
293 $this->assertEquals(
'jpg', $options->getFormat());
294 $png_explicit = $options->withFormat(
'png');
295 $this->assertEquals(
'png', $png_explicit->getFormat());
297 $jpg = $options->withJpgOutput();
298 $this->assertEquals(
'jpg', $jpg->getFormat());
299 $jpg_explicit = $options->withFormat(
'jpg');
300 $this->assertEquals(
'jpg', $jpg_explicit->getFormat());
301 $jpeg = $options->withFormat(
'jpeg');
302 $this->assertEquals(
'jpg', $jpeg->getFormat());
305 $low = $options->withQuality(5);
306 $this->assertEquals(5, $low->getQuality());
307 $this->assertEquals(75, $options->getQuality());
324 $options =
new ImageOutputOptions();
325 $this->expectException(\InvalidArgumentException::class);
326 $wrong = $options->withFormat($format);
343 $options =
new ImageOutputOptions();
344 $this->expectException(\InvalidArgumentException::class);
345 $wrong = $options->withQuality($quality);
351 $png = $this->images->convertToFormat(
358 $this->assertEquals(10, $size[self::W]);
359 $this->assertEquals(10, $size[self::H]);
363 $png = $this->images->convertToFormat(
372 $this->assertEquals(20, $size[self::W]);
373 $this->assertEquals(20, $size[self::H]);
378 $false_stream = Streams::ofString(
'false');
379 $images =
new Images(
384 $resized = $images->resizeToFixedSize(
389 $this->assertFalse($resized->isOK());
390 $this->assertInstanceOf(\Throwable::class, $resized->getThrowableIfAny());
411 private function colorDiff(
string $hex_color_one,
string $hex_color_two):
int 413 preg_match(
'/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i', $hex_color_one, $rgb_one);
414 preg_match(
'/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i', $hex_color_two, $rgb_two);
416 return abs(hexdec($rgb_one[1]) - hexdec($rgb_two[1]))
417 + abs(hexdec($rgb_one[2]) - hexdec($rgb_two[2]))
418 + abs(hexdec($rgb_one[3]) - hexdec($rgb_two[3]));
426 $transparent_png = __DIR__ .
'/img/transparent.png';
427 $this->assertFileExists($transparent_png);
428 $png = Streams::ofResource(fopen($transparent_png,
'rb'));
430 $converter_options = (
new ImageConversionOptions())
431 ->withThrowOnError(
true)
432 ->withFixedDimensions(100, 100);
434 if ($color !== null) {
435 $converter_options = $converter_options->withBackgroundColor($color);
440 $output_options = (
new ImageOutputOptions())
444 $converter =
new ImageConverter($converter_options, $output_options, $png);
445 $this->assertTrue($converter->isOK());
446 $converted_stream = $converter->getStream();
447 $gd_image = imagecreatefromstring((
string) $converted_stream);
448 $colors = imagecolorsforindex($gd_image, imagecolorat($gd_image, 1, 1));
450 $color_in_converted_picture = sprintf(
"#%02x%02x%02x", $colors[
'red'], $colors[
'green'], $colors[
'blue']);
451 $color_diff = $this->
colorDiff($color, $color_in_converted_picture);
453 $this->assertLessThan(3, $color_diff);
460 $output_path = __DIR__ .
'/img/output.jpg';
461 $converter_options = (
new ImageConversionOptions())
462 ->withThrowOnError(
true)
463 ->withMakeTemporaryFiles(
false)
464 ->withFixedDimensions(100, 100)
465 ->withOutputPath($output_path);
467 $output_options = (
new ImageOutputOptions())
471 $converter =
new ImageConverter($converter_options, $output_options, $img);
472 $this->assertTrue($converter->isOK());
474 $this->assertFileExists($output_path);
475 $stream = $converter->getStream();
476 $this->assertEquals($output_path, $stream->getMetadata(
'uri'));
478 unlink($output_path);
483 $file =
'https://upload.wikimedia.org/wikipedia/commons/5/5e/Jan_Vermeer_-_The_Art_of_Painting_-_Google_Art_Project.jpg';
485 curl_setopt($curl, CURLOPT_URL, $file);
486 curl_setopt($curl, CURLOPT_RETURNTRANSFER,
true);
487 curl_setopt($curl, CURLOPT_USERAGENT,
'PHPUnit/1.0');
488 $string = curl_exec($curl);
491 $img = Streams::ofString($string);
492 $this->assertInstanceOf(FileStream::class, $img);
494 $converter_options = (
new ImageConversionOptions())
497 ->withKeepAspectRatio(
true)
499 ->withThrowOnError(
true);
501 $output_options = (
new ImageOutputOptions())
503 ->withFormat(ImageOutputOptions::FORMAT_PNG);
505 $converter =
new ImageConverter($converter_options, $output_options, $img);
506 $this->assertTrue($converter->isOK());
512 if (!class_exists(
'Imagick')) {
513 $this->markTestSkipped(
'Imagick not installed');
519 $getimagesizefromstring = getimagesizefromstring((
string) $stream);
521 self::W => (
int) round($getimagesizefromstring[0]),
522 self::H => (
int) round($getimagesizefromstring[1])
528 return finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $stream->read(255));
534 $img = new \Imagick();
535 $img->readImageBlob((
string) $stream);
537 return $img->getImageCompressionQuality();
542 $img = new \Imagick();
543 $img->newImage($width, $height,
new \ImagickPixel(
'black'));
544 $img->setImageFormat(
'jpg');
546 $stream = Streams::ofString($img->getImageBlob());
static getImageSizesByFixed()
testResizeToFitWidth(int $width, int $height, int $final_width, int $final_height)
getImageSizesByWidth
testImageOutputOptionSanity()
testWrongFormats(string $format)
getWrongFormats
static getImageSizesByHeight()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testResizeByFixedSize(int $width, int $height, int $final_width, int $final_height, bool $crop)
getImageSizesByFixed
static getWrongQualites()
static getImageSizesByWidth()
testImageOutputOptions(ImageOutputOptions $options, string $expected_mime_type, int $expected_quality)
getImageOptions
getImageTypeFromStream(FileStream $stream)
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)
getImageSizesByHeight
getImageQualityFromStream(FileStream $stream)
testWrongQualities(int $quality)
getWrongQualites
testImageSquareActualImage()
getImageSizeFromStream(FileStream $stream)
The base interface for all filesystem streams.
testBackgroundColor(?string $color)
getColors