1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
<?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));
});
|