ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
ILIAS\Filesystem\Util\ImageConversionTest Class Reference
+ Inheritance diagram for ILIAS\Filesystem\Util\ImageConversionTest:
+ Collaboration diagram for ILIAS\Filesystem\Util\ImageConversionTest:

Public Member Functions

 testImageThumbnailActualImage ()
 
 testImageSquareActualImage ()
 
 testResizeToFitWidth (int $width, int $height, int $final_width, int $final_height)
 
 testResizeToFitHeight (int $width, int $height, int $final_height, int $final_width)
 
 testResizeByFixedSize (int $width, int $height, int $final_width, int $final_height, bool $crop)
 
 testImageOutputOptions (ImageOutputOptions $options, string $expected_mime_type, int $expected_quality)
 
 testImageOutputOptionSanity ()
 
 testWrongFormats (string $format)
 
 testWrongQualities (int $quality)
 
 testFormatConvert ()
 
 testFailed ()
 
 testBackgroundColor (?string $color)
 
 testAnimatedImageUsesFirstFrame (string $format)
 
 testAnimatedTransparentImageUsesFirstFrameOnBackground (string $format)
 
 testAnimatedImageWithSmallerFirstFrameUsesBackground (string $format)
 
 testWriteImage ()
 
 testHighDensityPixel ()
 

Static Public Member Functions

static getImageSizesByWidth ()
 
static getImageSizesByHeight ()
 
static getImageSizesByFixed ()
 
static getImageOptions ()
 
static getWrongFormats ()
 
static getWrongQualites ()
 
static getColors ()
 
static getOutputFormats ()
 

Protected Member Functions

 setUp ()
 
 checkImagick ()
 
 getImageSizeFromStream (FileStream $stream)
 
 getImageTypeFromStream (FileStream $stream)
 
 getImageQualityFromStream (FileStream $stream)
 
 createTestImageStream (int $width, int $height)
 

Protected Attributes

const BY_WIDTH_FINAL = 256
 
const BY_HEIGHT_FINAL = 756
 
const W = 'width'
 
const H = 'height'
 
const IMAGE_JPEG = 'image/jpeg'
 
const IMAGE_PNG = 'image/png'
 
const IMAGE_WEBP = 'image/webp'
 
Images $images
 

Private Member Functions

 colorDiff (string $hex_color_one, string $hex_color_two)
 
 convertAnimatedImage (string $img, string $format)
 
 assertPixels (\Imagick $image, array $expected)
 

Detailed Description

Author
Fabian Schmid fabia.nosp@m.n@sr.nosp@m..solu.nosp@m.tion.nosp@m.s

Definition at line 33 of file ImageConversionTest.php.

Member Function Documentation

◆ assertPixels()

ILIAS\Filesystem\Util\ImageConversionTest::assertPixels ( \Imagick  $image,
array  $expected 
)
private
Parameters
array<string,array<array{0int|float, 1: int|float}>> $expected colors with positions, a float is relative to the image size, a negative int is counted from the end

Definition at line 509 of file ImageConversionTest.php.

509 : void
510 {
511 $width = $image->getImageWidth();
512 $height = $image->getImageHeight();
513 $position = static fn(int|float $p, int $size): int => is_float($p)
514 ? (int) ($size * $p)
515 : ($p < 0 ? $size + $p : $p);
516
517 foreach ($expected as $expected_color => $positions) {
518 foreach ($positions as [$x, $y]) {
519 $x = $position($x, $width);
520 $y = $position($y, $height);
521 $pixel = $image->getImagePixelColor($x, $y)->getColor();
522 $color = sprintf('#%02x%02x%02x', $pixel['r'], $pixel['g'], $pixel['b']);
523 $this->assertLessThan(10, $this->colorDiff($expected_color, $color), "pixel $x/$y is $color");
524 $this->assertSame(1, $pixel['a'], "pixel $x/$y is not opaque");
525 }
526 }
527 }
colorDiff(string $hex_color_one, string $hex_color_two)

References ILIAS\Repository\int().

+ Here is the call graph for this function:

◆ checkImagick()

ILIAS\Filesystem\Util\ImageConversionTest::checkImagick ( )
protected

Definition at line 583 of file ImageConversionTest.php.

583 : void
584 {
585 if (!class_exists('Imagick')) {
586 $this->markTestSkipped('Imagick not installed');
587 }
588 }

Referenced by ILIAS\Filesystem\Util\ImageConversionTest\setUp().

+ Here is the caller graph for this function:

◆ colorDiff()

ILIAS\Filesystem\Util\ImageConversionTest::colorDiff ( string  $hex_color_one,
string  $hex_color_two 
)
private

Definition at line 385 of file ImageConversionTest.php.

385 : int
386 {
387 preg_match('/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i', $hex_color_one, $rgb_one);
388 preg_match('/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i', $hex_color_two, $rgb_two);
389
390 return abs(hexdec($rgb_one[1]) - hexdec($rgb_two[1]))
391 + abs(hexdec($rgb_one[2]) - hexdec($rgb_two[2]))
392 + abs(hexdec($rgb_one[3]) - hexdec($rgb_two[3]));
393 }

◆ convertAnimatedImage()

ILIAS\Filesystem\Util\ImageConversionTest::convertAnimatedImage ( string  $img,
string  $format 
)
private

Definition at line 474 of file ImageConversionTest.php.

474 : \Imagick
475 {
476 $this->assertFileExists($img);
477 $gif = Streams::ofResource(fopen($img, 'rb'));
478
479 $converter_options = (new ImageConversionOptions())
480 ->withThrowOnError(true)
481 ->withFixedDimensions(20, 20)
482 ->withCrop(true)
483 ->withKeepAspectRatio(true)
484 ->withBackgroundColor('#FFFFFF');
485
486 $output_options = (new ImageOutputOptions())
487 ->withQuality(100)
488 ->withFormat($format);
489
490 $converter = new ImageConverter($converter_options, $output_options, $gif);
491 $this->assertTrue($converter->isOK());
492
493 $converted = new \Imagick();
494 $converted->readImageBlob((string) $converter->getStream());
495 $this->assertSame(1, $converted->getNumberImages());
496 $this->assertSame(
497 $format === ImageOutputOptions::FORMAT_JPG ? 'JPEG' : strtoupper($format),
498 $converted->getImageFormat()
499 );
500 $this->assertSame($converted->getImageWidth(), $converted->getImageHeight());
501
502 return $converted;
503 }
static ofResource($resource)
Wraps an already created resource with the stream abstraction.
Definition: Streams.php:64

◆ createTestImageStream()

ILIAS\Filesystem\Util\ImageConversionTest::createTestImageStream ( int  $width,
int  $height 
)
protected

Definition at line 613 of file ImageConversionTest.php.

613 : FileStream
614 {
615 $img = new \Imagick();
616 $img->newImage($width, $height, new \ImagickPixel('black'));
617 $img->setImageFormat('jpg');
618
619 $stream = Streams::ofString($img->getImageBlob());
620 $stream->rewind();
621 return $stream;
622 }
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41

◆ getColors()

static ILIAS\Filesystem\Util\ImageConversionTest::getColors ( )
static

Definition at line 369 of file ImageConversionTest.php.

369 : \Iterator
370 {
371 yield [null];
372 yield ['#000000'];
373 yield ['#ff0000'];
374 yield ['#00ff00'];
375 yield ['#0000ff'];
376 yield ['#ffffff'];
377 yield ['#A3BF5A'];
378 yield ['#E9745A'];
379 yield ['#5A5AE9'];
380 yield ['#5AE9E9'];
381 yield ['#E95AE9'];
382 yield ['#E9E95A'];
383 }

◆ getImageOptions()

static ILIAS\Filesystem\Util\ImageConversionTest::getImageOptions ( )
static

Definition at line 233 of file ImageConversionTest.php.

233 : \Iterator
234 {
235 $options = new ImageOutputOptions();
236 yield [$options, self::IMAGE_JPEG, 75];
237 yield [$options->withPngOutput()->withQuality(22), self::IMAGE_PNG, 0];
238 yield [$options->withJpgOutput()->withQuality(100), self::IMAGE_JPEG, 100];
239 yield [$options->withFormat('png')->withQuality(50), self::IMAGE_PNG, 0];
240 yield [$options->withFormat('jpg')->withQuality(87), self::IMAGE_JPEG, 87];
241 yield [$options->withQuality(5)->withJpgOutput(), self::IMAGE_JPEG, 5];
242 yield [$options->withQuality(10)->withJpgOutput(), self::IMAGE_JPEG, 10];
243 yield [$options->withQuality(35)->withJpgOutput(), self::IMAGE_JPEG, 35];
244 yield [$options->withQuality(0)->withWebPOutput(), self::IMAGE_WEBP, 0];
245 yield [$options->withQuality(100)->withWebPOutput(), self::IMAGE_WEBP, 100];
246 }

References ILIAS\UI\Implementation\Component\Input\Field\$options.

◆ getImageQualityFromStream()

ILIAS\Filesystem\Util\ImageConversionTest::getImageQualityFromStream ( FileStream  $stream)
protected

Definition at line 604 of file ImageConversionTest.php.

604 : int
605 {
606 $stream->rewind();
607 $img = new \Imagick();
608 $img->readImageBlob((string) $stream);
609
610 return $img->getImageCompressionQuality();
611 }

◆ getImageSizeFromStream()

ILIAS\Filesystem\Util\ImageConversionTest::getImageSizeFromStream ( FileStream  $stream)
protected

Definition at line 590 of file ImageConversionTest.php.

590 : array
591 {
592 $getimagesizefromstring = getimagesizefromstring((string) $stream);
593 return [
594 self::W => (int) round($getimagesizefromstring[0]),
595 self::H => (int) round($getimagesizefromstring[1])
596 ];
597 }

References ILIAS\Repository\int().

Referenced by ILIAS\Filesystem\Util\ImageConversionTest\testImageSquareActualImage().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getImageSizesByFixed()

static ILIAS\Filesystem\Util\ImageConversionTest::getImageSizesByFixed ( )
static

Definition at line 202 of file ImageConversionTest.php.

202 : \Iterator
203 {
204 yield [1024, 768, 300, 100, true];
205 yield [1024, 768, 300, 100, false];
206 yield [1024, 768, 100, 300, true];
207 yield [1024, 768, 100, 300, false];
208 yield [400, 300, 500, 400, true];
209 yield [400, 300, 500, 400, false];
210 }

◆ getImageSizesByHeight()

static ILIAS\Filesystem\Util\ImageConversionTest::getImageSizesByHeight ( )
static

Definition at line 154 of file ImageConversionTest.php.

154 : \Iterator
155 {
156 yield [400, 300, self::BY_HEIGHT_FINAL, 1008];
157 yield [300, 400, self::BY_HEIGHT_FINAL, 567];
158 yield [200, 200, self::BY_HEIGHT_FINAL, 756];
159 yield [248, 845, self::BY_HEIGHT_FINAL, 221];
160 }

◆ getImageSizesByWidth()

static ILIAS\Filesystem\Util\ImageConversionTest::getImageSizesByWidth ( )
static

Definition at line 107 of file ImageConversionTest.php.

107 : \Iterator
108 {
109 yield [400, 300, self::BY_WIDTH_FINAL, 192];
110 yield [300, 400, self::BY_WIDTH_FINAL, 341];
111 yield [543, 431, self::BY_WIDTH_FINAL, 203];
112 yield [200, 200, self::BY_WIDTH_FINAL, 256];
113 }

References ILIAS\Filesystem\Util\ImageConversionTest\BY_WIDTH_FINAL.

◆ getImageTypeFromStream()

ILIAS\Filesystem\Util\ImageConversionTest::getImageTypeFromStream ( FileStream  $stream)
protected

Definition at line 599 of file ImageConversionTest.php.

599 : string
600 {
601 return finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $stream->read(255));
602 }

◆ getOutputFormats()

static ILIAS\Filesystem\Util\ImageConversionTest::getOutputFormats ( )
static

◆ getWrongFormats()

static ILIAS\Filesystem\Util\ImageConversionTest::getWrongFormats ( )
static

Definition at line 294 of file ImageConversionTest.php.

294 : \Iterator
295 {
296 yield ['gif'];
297 yield ['bmp'];
298 yield ['jpg2000'];
299 }

◆ getWrongQualites()

static ILIAS\Filesystem\Util\ImageConversionTest::getWrongQualites ( )
static

Definition at line 309 of file ImageConversionTest.php.

309 : \Iterator
310 {
311 yield [-1];
312 yield [101];
313 yield [102];
314 }

◆ setUp()

ILIAS\Filesystem\Util\ImageConversionTest::setUp ( )
protected

Definition at line 46 of file ImageConversionTest.php.

46 : void
47 {
48 $this->checkImagick();
49 $this->images = new Images(
50 true,
51 );
52 }

References ILIAS\Filesystem\Util\ImageConversionTest\checkImagick().

+ Here is the call graph for this function:

◆ testAnimatedImageUsesFirstFrame()

ILIAS\Filesystem\Util\ImageConversionTest::testAnimatedImageUsesFirstFrame ( string  $format)

Definition at line 436 of file ImageConversionTest.php.

436 : void
437 {
438 // three frames of 40x40, optimized: the second and third frame only contain
439 // a 10x10 green resp. blue square, the first frame is completely red
440 $converted = $this->convertAnimatedImage(__DIR__ . '/img/animated.gif', $format);
441
442 $this->assertPixels($converted, [
443 '#ff0000' => [[1, 1], [0.5, 0.5], [-2, -2]],
444 ]);
445 }
assertPixels(\Imagick $image, array $expected)
convertAnimatedImage(string $img, string $format)

◆ testAnimatedImageWithSmallerFirstFrameUsesBackground()

ILIAS\Filesystem\Util\ImageConversionTest::testAnimatedImageWithSmallerFirstFrameUsesBackground ( string  $format)

Definition at line 462 of file ImageConversionTest.php.

462 : void
463 {
464 // two frames of 40x40: the first one is an opaque red 20x20 square at 10/10, the
465 // second one is completely blue. The area not covered by the first frame is empty.
466 $converted = $this->convertAnimatedImage(__DIR__ . '/img/animated_offset.gif', $format);
467
468 $this->assertPixels($converted, [
469 '#ffffff' => [[1, 1], [-2, -2]],
470 '#ff0000' => [[0.5, 0.5]],
471 ]);
472 }

◆ testAnimatedTransparentImageUsesFirstFrameOnBackground()

ILIAS\Filesystem\Util\ImageConversionTest::testAnimatedTransparentImageUsesFirstFrameOnBackground ( string  $format)

Definition at line 448 of file ImageConversionTest.php.

448 : void
449 {
450 // three frames of 40x40: the first one is transparent with a red square in the
451 // center, the second and third one only contain a 10x10 green resp. blue square
452 // in the upper left and lower right corner
453 $converted = $this->convertAnimatedImage(__DIR__ . '/img/animated_transparent.gif', $format);
454
455 $this->assertPixels($converted, [
456 '#ffffff' => [[1, 1], [-2, -2]],
457 '#ff0000' => [[0.5, 0.5]],
458 ]);
459 }

◆ testBackgroundColor()

ILIAS\Filesystem\Util\ImageConversionTest::testBackgroundColor ( ?string  $color)

Definition at line 396 of file ImageConversionTest.php.

396 : void
397 {
398 $transparent_png = __DIR__ . '/img/transparent.png';
399 $this->assertFileExists($transparent_png);
400 $png = Streams::ofResource(fopen($transparent_png, 'rb'));
401
402 $converter_options = (new ImageConversionOptions())
403 ->withThrowOnError(true)
404 ->withFixedDimensions(100, 100);
405
406 if ($color !== null) {
407 $converter_options = $converter_options->withBackgroundColor($color);
408 } else {
409 $color = '#ffffff';
410 }
411
412 $output_options = (new ImageOutputOptions())
413 ->withQuality(100)
414 ->withJpgOutput();
415
416 $converter = new ImageConverter($converter_options, $output_options, $png);
417 $this->assertTrue($converter->isOK());
418 $converted_stream = $converter->getStream();
419 $gd_image = imagecreatefromstring((string) $converted_stream);
420 $colors = imagecolorsforindex($gd_image, imagecolorat($gd_image, 1, 1));
421
422 $color_in_converted_picture = sprintf("#%02x%02x%02x", $colors['red'], $colors['green'], $colors['blue']);
423 $color_diff = $this->colorDiff($color, $color_in_converted_picture);
424
425 $this->assertLessThan(3, $color_diff);
426 }

◆ testFailed()

ILIAS\Filesystem\Util\ImageConversionTest::testFailed ( )

Definition at line 352 of file ImageConversionTest.php.

352 : void
353 {
354 $false_stream = Streams::ofString('false');
355 $images = new Images(
356 false,
357 false
358 );
359
360 $resized = $images->resizeToFixedSize(
361 $false_stream,
362 5,
363 5
364 );
365 $this->assertFalse($resized->isOK());
366 $this->assertInstanceOf(\Throwable::class, $resized->getThrowableIfAny());
367 }
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

References ILIAS\Filesystem\Util\Convert\Images\resizeToFixedSize().

+ Here is the call graph for this function:

◆ testFormatConvert()

ILIAS\Filesystem\Util\ImageConversionTest::testFormatConvert ( )

Definition at line 324 of file ImageConversionTest.php.

324 : void
325 {
326 $jpg = $this->createTestImageStream(10, 10);
327 $png = $this->images->convertToFormat(
328 $jpg,
329 'png'
330 );
331
332 $this->assertSame(self::IMAGE_PNG, $this->getImageTypeFromStream($png->getStream()));
333 $size = $this->getImageSizeFromStream($png->getStream());
334 $this->assertEquals(10, $size[self::W]);
335 $this->assertEquals(10, $size[self::H]);
336
337 // With Dimensions
338 $jpg = $this->createTestImageStream(10, 10);
339 $png = $this->images->convertToFormat(
340 $jpg,
341 'png',
342 20,
343 20
344 );
345
346 $this->assertSame(self::IMAGE_PNG, $this->getImageTypeFromStream($png->getStream()));
347 $size = $this->getImageSizeFromStream($png->getStream());
348 $this->assertEquals(20, $size[self::W]);
349 $this->assertEquals(20, $size[self::H]);
350 }

◆ testHighDensityPixel()

ILIAS\Filesystem\Util\ImageConversionTest::testHighDensityPixel ( )

Definition at line 554 of file ImageConversionTest.php.

554 : void
555 {
556 $file = 'https://upload.wikimedia.org/wikipedia/commons/5/5e/Jan_Vermeer_-_The_Art_of_Painting_-_Google_Art_Project.jpg';
557 $curl = curl_init();
558 curl_setopt($curl, CURLOPT_URL, $file);
559 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
560 curl_setopt($curl, CURLOPT_USERAGENT, 'PHPUnit/1.0');
561 $string = curl_exec($curl);
562 $curl = null;
563
564 $img = Streams::ofString($string);
565 $this->assertInstanceOf(FileStream::class, $img);
566
567 $converter_options = (new ImageConversionOptions())
568 ->withWidth(80)
569 ->withHeight(80)
570 ->withKeepAspectRatio(true)
571 ->withCrop(true)
572 ->withThrowOnError(true);
573
574 $output_options = (new ImageOutputOptions())
575 ->withQuality(60)
576 ->withFormat(ImageOutputOptions::FORMAT_PNG);
577
578 $converter = new ImageConverter($converter_options, $output_options, $img);
579 $this->assertTrue($converter->isOK());
580 }

◆ testImageOutputOptions()

ILIAS\Filesystem\Util\ImageConversionTest::testImageOutputOptions ( ImageOutputOptions  $options,
string  $expected_mime_type,
int  $expected_quality 
)

Definition at line 250 of file ImageConversionTest.php.

254 : void {
255 $resized = $this->images->resizeToFixedSize(
256 $this->createTestImageStream(10, 10),
257 5,
258 5,
259 true,
261 );
262
263 $this->assertSame($expected_mime_type, $this->getImageTypeFromStream($resized->getStream()));
264 $this->assertSame($expected_quality, $this->getImageQualityFromStream($resized->getStream()));
265 }

◆ testImageOutputOptionSanity()

ILIAS\Filesystem\Util\ImageConversionTest::testImageOutputOptionSanity ( )

Definition at line 267 of file ImageConversionTest.php.

267 : void
268 {
269 $options = new ImageOutputOptions();
270
271 // Defaults
272 $this->assertSame('jpg', $options->getFormat());
273 $this->assertSame(75, $options->getQuality());
274
275 $png = $options->withPngOutput();
276 $this->assertSame('png', $png->getFormat());
277 $this->assertSame('jpg', $options->getFormat()); // original options should not change
278 $png_explicit = $options->withFormat('png');
279 $this->assertSame('png', $png_explicit->getFormat());
280
281 $jpg = $options->withJpgOutput();
282 $this->assertSame('jpg', $jpg->getFormat());
283 $jpg_explicit = $options->withFormat('jpg');
284 $this->assertSame('jpg', $jpg_explicit->getFormat());
285 $jpeg = $options->withFormat('jpeg');
286 $this->assertSame('jpg', $jpeg->getFormat());
287
288 // Quality
289 $low = $options->withQuality(5);
290 $this->assertSame(5, $low->getQuality());
291 $this->assertSame(75, $options->getQuality()); // original options should not change
292 }

References ILIAS\UI\Implementation\Component\Input\Field\$options.

◆ testImageSquareActualImage()

ILIAS\Filesystem\Util\ImageConversionTest::testImageSquareActualImage ( )

Definition at line 81 of file ImageConversionTest.php.

81 : void
82 {
83 $img = __DIR__ . '/img/robot.jpg';
84 $this->assertFileExists($img);
85 $getimagesize = getimagesize($img);
86 $original_width = $getimagesize[0]; // should be 600
87 $original_height = $getimagesize[1]; // should be 800
88 $this->assertSame(600, $original_width);
89 $this->assertSame(800, $original_height);
90
91 // make tumbnail
92 $original_stream = Streams::ofResource(fopen($img, 'rb'));
93
94 $thumbnail_converter = $this->images->croppedSquare(
95 $original_stream,
96 200
97 );
98 $this->assertTrue($thumbnail_converter->isOK());
99 $this->assertNotInstanceOf(\Throwable::class, $thumbnail_converter->getThrowableIfAny());
100
101 $getimagesizefromstring = $this->getImageSizeFromStream($thumbnail_converter->getStream());
102
103 $this->assertEquals(200, $getimagesizefromstring[self::W]);
104 $this->assertEquals(200, $getimagesizefromstring[self::H]);
105 }

References ILIAS\Filesystem\Util\ImageConversionTest\getImageSizeFromStream(), and ILIAS\Filesystem\Stream\Streams\ofResource().

+ Here is the call graph for this function:

◆ testImageThumbnailActualImage()

ILIAS\Filesystem\Util\ImageConversionTest::testImageThumbnailActualImage ( )

Definition at line 54 of file ImageConversionTest.php.

54 : void
55 {
56 $img = __DIR__ . '/img/robot.jpg';
57 $this->assertFileExists($img);
58 $getimagesize = getimagesize($img);
59 $original_width = $getimagesize[0]; // should be 600
60 $original_height = $getimagesize[1]; // should be 800
61 $this->assertSame(600, $original_width);
62 $this->assertSame(800, $original_height);
63
64 // make tumbnail
65 $original_stream = Streams::ofResource(fopen($img, 'rb'));
66
67 $thumbnail_converter = $this->images->thumbnail(
68 $original_stream,
69 100
70 );
71 $this->assertTrue($thumbnail_converter->isOK());
72 $this->assertNotInstanceOf(\Throwable::class, $thumbnail_converter->getThrowableIfAny());
73 $converted_stream = $thumbnail_converter->getStream();
74
75 $getimagesizefromstring = getimagesizefromstring((string) $converted_stream);
76
77 $this->assertSame(75, $getimagesizefromstring[0]); // width
78 $this->assertSame(100, $getimagesizefromstring[1]); // height
79 }

References ILIAS\Filesystem\Stream\Streams\ofResource().

+ Here is the call graph for this function:

◆ testResizeByFixedSize()

ILIAS\Filesystem\Util\ImageConversionTest::testResizeByFixedSize ( int  $width,
int  $height,
int  $final_width,
int  $final_height,
bool  $crop 
)

Definition at line 213 of file ImageConversionTest.php.

219 : void {
220 $stream = $this->createTestImageStream($width, $height);
221 $dimensions = $this->getImageSizeFromStream($stream);
222 $this->assertEquals($width, $dimensions[self::W]);
223 $this->assertEquals($height, $dimensions[self::H]);
224
225 $by_fixed = $this->images->resizeToFixedSize($stream, $final_width, $final_height, $crop);
226 $this->assertTrue($by_fixed->isOK());
227 $new_dimensions = $this->getImageSizeFromStream($by_fixed->getStream());
228
229 $this->assertEquals($final_width, $new_dimensions[self::W]);
230 $this->assertEquals($final_height, $new_dimensions[self::H]);
231 }

◆ testResizeToFitHeight()

ILIAS\Filesystem\Util\ImageConversionTest::testResizeToFitHeight ( int  $width,
int  $height,
int  $final_height,
int  $final_width 
)

Definition at line 164 of file ImageConversionTest.php.

169 : void {
170 $stream = $this->createTestImageStream($width, $height);
171 $dimensions = $this->getImageSizeFromStream($stream);
172 $this->assertEquals($width, $dimensions[self::W]);
173 $this->assertEquals($height, $dimensions[self::H]);
174
175 // resize to fit
176 $resized = $this->images->resizeByHeight($stream, self::BY_HEIGHT_FINAL);
177 $this->assertTrue($resized->isOK());
178 $new_dimensions = $this->getImageSizeFromStream($resized->getStream());
179
180 $this->assertEquals($final_width, $new_dimensions[self::W]);
181 $this->assertEquals($final_height, $new_dimensions[self::H]);
182
183 // check aspect ratio
184 $this->assertSame(
185 round($width > $height),
186 round($final_width > $final_height)
187 );
188 $this->assertSame(
189 round($width / $height),
190 round($new_dimensions[self::W] / $new_dimensions[self::H])
191 );
192 $this->assertSame(
193 $width > $height,
194 $width > $height
195 );
196 $this->assertSame(
197 $width > $height,
198 $new_dimensions[self::W] > $new_dimensions[self::H]
199 );
200 }

◆ testResizeToFitWidth()

ILIAS\Filesystem\Util\ImageConversionTest::testResizeToFitWidth ( int  $width,
int  $height,
int  $final_width,
int  $final_height 
)

Definition at line 116 of file ImageConversionTest.php.

121 : void {
122 $stream = $this->createTestImageStream($width, $height);
123 $dimensions = $this->getImageSizeFromStream($stream);
124 $this->assertEquals($width, $dimensions[self::W]);
125 $this->assertEquals($height, $dimensions[self::H]);
126
127 // resize to fit width
128 $resized = $this->images->resizeByWidth($stream, self::BY_WIDTH_FINAL);
129 $this->assertTrue($resized->isOK());
130 $new_dimensions = $this->getImageSizeFromStream($resized->getStream());
131
132 $this->assertEquals($final_width, $new_dimensions[self::W]);
133 $this->assertEquals($final_height, $new_dimensions[self::H]);
134
135 // check aspect ratio
136 $this->assertSame(
137 round($width > $height),
138 round($final_width > $final_height)
139 );
140 $this->assertSame(
141 round($width / $height),
142 round($new_dimensions[self::W] / $new_dimensions[self::H])
143 );
144 $this->assertSame(
145 $width > $height,
146 $width > $height
147 );
148 $this->assertSame(
149 $width > $height,
150 $new_dimensions[self::W] > $new_dimensions[self::H]
151 );
152 }

◆ testWriteImage()

ILIAS\Filesystem\Util\ImageConversionTest::testWriteImage ( )

Definition at line 529 of file ImageConversionTest.php.

529 : void
530 {
531 $img = $this->createTestImageStream(10, 10);
532
533 $output_path = __DIR__ . '/img/output.jpg';
534 $converter_options = (new ImageConversionOptions())
535 ->withThrowOnError(true)
536 ->withMakeTemporaryFiles(false)
537 ->withFixedDimensions(100, 100)
538 ->withOutputPath($output_path);
539
540 $output_options = (new ImageOutputOptions())
541 ->withQuality(10)
542 ->withJpgOutput();
543
544 $converter = new ImageConverter($converter_options, $output_options, $img);
545 $this->assertTrue($converter->isOK());
546
547 $this->assertFileExists($output_path);
548 $stream = $converter->getStream();
549 $this->assertEquals($output_path, $stream->getMetadata('uri'));
550
551 unlink($output_path);
552 }

◆ testWrongFormats()

ILIAS\Filesystem\Util\ImageConversionTest::testWrongFormats ( string  $format)

Definition at line 302 of file ImageConversionTest.php.

302 : void
303 {
304 $options = new ImageOutputOptions();
305 $this->expectException(\InvalidArgumentException::class);
306 $wrong = $options->withFormat($format);
307 }

References ILIAS\UI\Implementation\Component\Input\Field\$options.

◆ testWrongQualities()

ILIAS\Filesystem\Util\ImageConversionTest::testWrongQualities ( int  $quality)

Definition at line 317 of file ImageConversionTest.php.

317 : void
318 {
319 $options = new ImageOutputOptions();
320 $this->expectException(\InvalidArgumentException::class);
321 $wrong = $options->withQuality($quality);
322 }

References ILIAS\UI\Implementation\Component\Input\Field\$options.

Field Documentation

◆ $images

Images ILIAS\Filesystem\Util\ImageConversionTest::$images
protected

Definition at line 44 of file ImageConversionTest.php.

◆ BY_HEIGHT_FINAL

const ILIAS\Filesystem\Util\ImageConversionTest::BY_HEIGHT_FINAL = 756
protected

Definition at line 38 of file ImageConversionTest.php.

◆ BY_WIDTH_FINAL

const ILIAS\Filesystem\Util\ImageConversionTest::BY_WIDTH_FINAL = 256
protected

◆ H

const ILIAS\Filesystem\Util\ImageConversionTest::H = 'height'
protected

Definition at line 40 of file ImageConversionTest.php.

◆ IMAGE_JPEG

const ILIAS\Filesystem\Util\ImageConversionTest::IMAGE_JPEG = 'image/jpeg'
protected

Definition at line 41 of file ImageConversionTest.php.

◆ IMAGE_PNG

const ILIAS\Filesystem\Util\ImageConversionTest::IMAGE_PNG = 'image/png'
protected

Definition at line 42 of file ImageConversionTest.php.

◆ IMAGE_WEBP

const ILIAS\Filesystem\Util\ImageConversionTest::IMAGE_WEBP = 'image/webp'
protected

Definition at line 43 of file ImageConversionTest.php.

◆ W

const ILIAS\Filesystem\Util\ImageConversionTest::W = 'width'
protected

Definition at line 39 of file ImageConversionTest.php.


The documentation for this class was generated from the following file: