|
| 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 | +}); |
0 commit comments