| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- /**
- * Test: Nette\Utils\Image crop, resize & flip.
- * @phpExtension gd
- */
- declare(strict_types=1);
- use Nette\Utils\Image;
- use Tester\Assert;
- require __DIR__ . '/../bootstrap.php';
- $main = Image::fromFile(__DIR__ . '/fixtures.images/logo.gif');
- test('cropping...', function () use ($main) {
- $image = clone $main;
- Assert::same(176, $image->width);
- Assert::same(104, $image->height);
- $image->crop(10, 20, 50, 300);
- Assert::same(50, $image->width);
- Assert::same(84, $image->height);
- });
- test('resizing X', function () use ($main) {
- $image = clone $main;
- $image->resize(150, null);
- Assert::same(150, $image->width);
- Assert::same(89, $image->height);
- });
- test('resizing Y shrink', function () use ($main) {
- $image = clone $main;
- $image->resize(null, 150, Image::ShrinkOnly);
- Assert::same(176, $image->width);
- Assert::same(104, $image->height);
- });
- test('resizing X Y shrink', function () use ($main) {
- $image = clone $main;
- $image->resize(300, 150, Image::ShrinkOnly);
- Assert::same(176, $image->width);
- Assert::same(104, $image->height);
- });
- test('resizing X Y', function () use ($main) {
- $image = clone $main;
- $image->resize(300, 150);
- Assert::same(254, $image->width);
- Assert::same(150, $image->height);
- });
- test('resizing X Y stretch', function () use ($main) {
- $image = clone $main;
- $image->resize(300, 100, Image::Stretch);
- Assert::same(300, $image->width);
- Assert::same(100, $image->height);
- });
- test('resizing X Y shrink stretch', function () use ($main) {
- $image = clone $main;
- $image->resize(300, 100, Image::ShrinkOnly | Image::Stretch);
- Assert::same(176, $image->width);
- Assert::same(100, $image->height);
- });
- test('resizing X%', function () use ($main) {
- $image = clone $main;
- $image->resize('110%', null);
- Assert::same(194, $image->width);
- Assert::same(115, $image->height);
- });
- test('resizing X% Y%', function () use ($main) {
- $image = clone $main;
- $image->resize('110%', '90%');
- Assert::same(194, $image->width);
- Assert::same(94, $image->height);
- });
- test('flipping X', function () use ($main) {
- $image = clone $main;
- $image->resize(-150, null);
- Assert::same(150, $image->width);
- Assert::same(89, $image->height);
- });
- test('flipping Y shrink', function () use ($main) {
- $image = clone $main;
- $image->resize(null, -150, Image::ShrinkOnly);
- Assert::same(176, $image->width);
- Assert::same(104, $image->height);
- });
- test('flipping X Y shrink', function () use ($main) {
- $image = clone $main;
- $image->resize(-300, -150, Image::ShrinkOnly);
- Assert::same(176, $image->width);
- Assert::same(104, $image->height);
- });
- test('exact resize', function () use ($main) {
- $image = clone $main;
- $image->resize(300, 150, Image::Cover);
- Assert::same(300, $image->width);
- Assert::same(150, $image->height);
- });
- test('rotate', function () use ($main) {
- $image = clone $main;
- $image->rotate(90, Image::rgb(0, 0, 0));
- Assert::same(104, $image->width);
- Assert::same(176, $image->height);
- });
- test('alpha crop', function () use ($main) {
- $image = Image::fromFile(__DIR__ . '/fixtures.images/alpha1.png');
- $image->crop(1, 1, 8, 8);
- Assert::same(file_get_contents(__DIR__ . '/expected/Image.alpha.crop.png'), $image->toString($image::PNG));
- });
- test('alpha resize', function () use ($main) {
- $image = Image::fromFile(__DIR__ . '/fixtures.images/alpha1.png');
- $image->resize(20, 20);
- Assert::same(file_get_contents(__DIR__ . '/expected/Image.alpha.resize1.png'), $image->toString($image::PNG));
- });
- test('alpha flip', function () use ($main) {
- $image = Image::fromFile(__DIR__ . '/fixtures.images/alpha1.png');
- $image->resize(-10, -10);
- Assert::same(file_get_contents(__DIR__ . '/expected/Image.alpha.flip1.png'), $image->toString($image::PNG));
- });
- test('palette alpha resize', function () use ($main) {
- $image = Image::fromFile(__DIR__ . '/fixtures.images/alpha3.gif');
- $image->resize(20, 20);
- Assert::same(file_get_contents(__DIR__ . '/expected/Image.alpha.resize2.png'), $image->toString($image::PNG));
- });
|