-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathWriteTest.php
92 lines (76 loc) · 2.3 KB
/
WriteTest.php
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
<?php
namespace Jcupitt\Vips\Test;
use Jcupitt\Vips;
use PHPUnit\Framework\TestCase;
class WriteTest extends TestCase
{
/**
* @var array
*/
private $tmps;
protected function setUp(): void
{
$this->tmps = [];
}
protected function tearDown(): void
{
foreach ($this->tmps as $tmp) {
@unlink($tmp);
}
}
public function tmp($suffix)
{
$tmp = tempnam(sys_get_temp_dir(), 'vips-test');
unlink($tmp);
// race condition, sigh
$tmp .= $suffix;
$this->tmps[] = $tmp;
return $tmp;
}
public function testVipsWriteToFile()
{
$filename = __DIR__ . '/images/img_0076.jpg';
$image = Vips\Image::newFromFile($filename, ['shrink' => 8]);
$output_filename = $this->tmp('.jpg');
$image->writeToFile($output_filename);
$image = Vips\Image::newFromFile($output_filename);
$this->assertEquals($image->width, 200);
$this->assertEquals($image->height, 150);
$this->assertEquals($image->bands, 3);
}
public function testVipsWriteToBuffer()
{
$filename = __DIR__ . '/images/img_0076.jpg';
$image = Vips\Image::newFromFile($filename, ['shrink' => 8]);
$buffer1 = $image->writeToBuffer('.jpg', ['Q' => 75]);
$output_filename = $this->tmp('.jpg');
$image->writeToFile($output_filename);
$buffer2 = file_get_contents($output_filename);
$this->assertEquals($buffer1, $buffer2);
}
public function testVipsWriteToMemory()
{
$binaryStr = pack('C*', ...array_fill(0, 200, 0));
$image = Vips\Image::newFromMemory($binaryStr, 20, 10, 1, Vips\BandFormat::UCHAR);
$memStr = $image->writeToMemory();
$this->assertEquals($binaryStr, $memStr);
}
public function testVipsWriteToArray()
{
$filename = __DIR__ . '/images/img_0076.jpg';
$image = Vips\Image::newFromFile($filename, ['shrink' => 8]);
$array = $image->crop(0, 0, 2, 2)->writeToArray();
$this->assertEquals(
$array,
[34, 39, 35, 44, 49, 45, 67, 52, 49, 120, 105, 102]
);
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: expandtab sw=4 ts=4 fdm=marker
* vim<600: expandtab sw=4 ts=4
*/