File: Image.save.phpt

package info (click to toggle)
php-nette-utils 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,460 kB
  • sloc: php: 4,146; xml: 9; makefile: 4
file content (108 lines) | stat: -rw-r--r-- 2,988 bytes parent folder | download
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
<?php

/**
 * Test: Nette\Utils\Image save method exceptions.
 * @phpExtension gd
 */

declare(strict_types=1);

use Nette\Utils\Image;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$main = Image::fromFile(__DIR__ . '/fixtures.images/alpha1.png');


test('saving image as PNG with inferred extension', function () use ($main) {
	$main->save(getTempDir() . '/foo.png');
	Assert::true(is_file(getTempDir() . '/foo.png'));
	Assert::same(IMAGETYPE_PNG, getimagesize(getTempDir() . '/foo.png')[2]);
});


test('saving image with custom extension parameter', function () use ($main) {
	$main->save(getTempDir() . '/foo.x', null, Image::PNG);
	Assert::true(is_file(getTempDir() . '/foo.x'));
	Assert::same(IMAGETYPE_PNG, getimagesize(getTempDir() . '/foo.x')[2]);
});


test('saving WEBP image if supported', function () use ($main) {
	if (!Image::isTypeSupported(Image::WEBP)) {
		return;
	}

	$main->save(getTempDir() . '/foo.webp');
	Assert::true(is_file(getTempDir() . '/foo.webp'));
	Assert::same('WEBP', file_get_contents(getTempDir() . '/foo.webp', false, null, 8, 4));

	$main->save(getTempDir() . '/foo.y', null, Image::WEBP);
	Assert::true(is_file(getTempDir() . '/foo.y'));
	Assert::same('WEBP', file_get_contents(getTempDir() . '/foo.y', false, null, 8, 4));
});


test('saving AVIF image if supported', function () use ($main) {
	if (!Image::isTypeSupported(Image::AVIF)) {
		return;
	}

	$main->save(getTempDir() . '/foo.avif');
	Assert::true(is_file(getTempDir() . '/foo.avif'));
	Assert::same('avif', file_get_contents(getTempDir() . '/foo.avif', false, null, 8, 4));

	$main->save(getTempDir() . '/foo.y', null, Image::AVIF);
	Assert::true(is_file(getTempDir() . '/foo.y'));
	Assert::same('avif', file_get_contents(getTempDir() . '/foo.y', false, null, 8, 4));
});


test('saving BMP image if supported', function () use ($main) {
	if (!function_exists('imagebmp')) {
		return;
	}

	$main->save(getTempDir() . '/foo.bmp');
	Assert::true(is_file(getTempDir() . '/foo.bmp'));
	Assert::same(IMAGETYPE_BMP, getimagesize(getTempDir() . '/foo.bmp')[2]);

	$main->save(getTempDir() . '/foo.y', null, Image::BMP);
	Assert::true(is_file(getTempDir() . '/foo.y'));
	Assert::same(IMAGETYPE_BMP, getimagesize(getTempDir() . '/foo.y')[2]);
});


Assert::exception(
	fn() => $main->save('foo', null, IMG_WBMP),
	Nette\InvalidArgumentException::class,
	sprintf('Unsupported image type \'%d\'.', IMG_WBMP),
);


Assert::exception(
	fn() => $main->save('foo.psd'),
	Nette\InvalidArgumentException::class,
	'Unsupported file extension \'psd\'.',
);


test('saving palette-based as WEBP should fail without creating file', function () {
	if (!Image::isTypeSupported(Image::WEBP)) {
		return;
	}

	$paletteImage = Image::fromFile(__DIR__ . '/fixtures.images/logo.gif');
	$filename = getTempDir() . '/palette-test.webp';

	Assert::exception(
		fn() => $paletteImage->save($filename),
		Nette\Utils\ImageException::class,
		'Palette %a%',
	);

	Assert::false(is_file($filename));
});