Skip to content

Commit 875ba04

Browse files
tests: add comprehensive tests for Image utility
1 parent 9e2ee6b commit 875ba04

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

src/Transformers.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ class Transformers
1717

1818
protected static ?string $authToken = null;
1919

20-
protected static ?string $userAgent = 'transformers-php/0.4.0';
20+
protected static ?string $userAgent = 'transformers-php/0.5.3';
2121

22-
protected static ImageDriver $imageDriver;
22+
protected static ImageDriver $imageDriver = ImageDriver::VIPS;
2323

2424

2525
/**
@@ -155,4 +155,4 @@ public static function getImageDriver(): ?ImageDriver
155155

156156
return self::$imageDriver;
157157
}
158-
}
158+
}

tests/Utils/ImageTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
use Codewithkyrian\Transformers\Utils\Image;
4+
use Codewithkyrian\Transformers\Utils\ImageDriver;
5+
use Codewithkyrian\Transformers\Transformers;
6+
use Codewithkyrian\Transformers\Tensor\Tensor;
7+
8+
describe('VIPS Driver', function () {
9+
beforeEach(function () {
10+
Transformers::setup()->setImageDriver(ImageDriver::VIPS);
11+
});
12+
13+
it('can read and get dimensions', function () {
14+
$img = Image::read('tests/fixtures/images/sample.jpg');
15+
expect($img->width())->toBeInt()->and($img->height())->toBeInt();
16+
expect($img->size())->toHaveCount(2);
17+
});
18+
19+
it('can resize and thumbnail', function () {
20+
$img = Image::read('tests/fixtures/images/sample.jpg');
21+
$resized = $img->resize(64, 32);
22+
expect($resized->width())->toBe(64);
23+
expect($resized->height())->toBe(32);
24+
$thumb = $img->thumbnail(32, 32);
25+
expect($thumb->width())->toBeLessThanOrEqual(32);
26+
expect($thumb->height())->toBeLessThanOrEqual(32);
27+
});
28+
29+
it('can grayscale, rgb, and rgba', function () {
30+
$img = Image::read('tests/fixtures/images/sample.jpg');
31+
$gray = $img->grayscale();
32+
expect($gray->channels)->toBe(1);
33+
$rgb = $img->rgb();
34+
expect($rgb->channels)->toBe(3);
35+
$rgba = $img->rgba();
36+
expect($rgba->channels)->toBe(4);
37+
});
38+
39+
it('can center crop and crop', function () {
40+
$img = Image::read('tests/fixtures/images/sample.jpg');
41+
$center = $img->centerCrop(32, 32);
42+
expect($center->width())->toBe(32);
43+
expect($center->height())->toBe(32);
44+
$crop = $img->crop(0, 0, 15, 15);
45+
expect($crop->width())->toBe(16);
46+
expect($crop->height())->toBe(16);
47+
});
48+
49+
it('can pad and invert', function () {
50+
$img = Image::read('tests/fixtures/images/sample.jpg');
51+
$padded = $img->pad(2, 2, 2, 2);
52+
expect($padded->width())->toBe($img->width() + 4);
53+
expect($padded->height())->toBe($img->height() + 4);
54+
$inverted = $img->invert();
55+
expect($inverted)->toBeInstanceOf(Image::class);
56+
});
57+
58+
it('can convert to and from tensor', function () {
59+
$img = Image::read('tests/fixtures/images/sample.jpg');
60+
$tensor = $img->toTensor('CHW');
61+
expect($tensor)->toBeInstanceOf(Tensor::class);
62+
$img2 = Image::fromTensor($tensor, 'CHW');
63+
expect($img2->width())->toBe($img->width());
64+
expect($img2->height())->toBe($img->height());
65+
expect($img2->channels)->toBe($img->channels);
66+
});
67+
68+
it('can save an image', function () {
69+
$img = Image::read('tests/fixtures/images/sample.jpg');
70+
$tmp = tempnam(sys_get_temp_dir(), 'imgtest_') . '.jpg';
71+
$img->save($tmp);
72+
expect(file_exists($tmp))->toBeTrue();
73+
unlink($tmp);
74+
});
75+
});

tests/fixtures/images/sample.jpg

413 KB
Loading

0 commit comments

Comments
 (0)