forked from timber/timber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-timber-image-letterbox.php
142 lines (128 loc) · 6.19 KB
/
test-timber-image-letterbox.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
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
<?php
class TestTimberImageLetterbox extends TimberAttachment_UnitTestCase
{
/**
* @requires extension gd
*/
public function set_up()
{
parent::set_up();
if (!extension_loaded('gd')) {
self::markTestSkipped('Letterbox image operation tests requires GD extension');
}
}
public function testLetterbox()
{
$file_loc = TestTimberImage::copyTestAttachment('eastern.jpg');
$upload_dir = wp_upload_dir();
$image = $upload_dir['url'] . '/eastern.jpg';
$new_file = Timber\ImageHelper::letterbox($image, 500, 500, '#CCC', true);
$location_of_image = Timber\ImageHelper::get_server_location($new_file);
$this->addFile($location_of_image);
$this->assertTrue(TestTimberImage::checkSize($location_of_image, 500, 500));
//whats the bg/color of the image
$this->assertTrue(TestTimberImage::checkPixel($location_of_image, 1, 1, "#CCC"));
}
public function testLetterboxColorChange()
{
$file_loc = TestTimberImage::copyTestAttachment('eastern.jpg');
$upload_dir = wp_upload_dir();
$new_file_red = Timber\ImageHelper::letterbox($upload_dir['url'] . '/eastern.jpg', 500, 500, '#FF0000');
$new_file = Timber\ImageHelper::letterbox($upload_dir['url'] . '/eastern.jpg', 500, 500, '#00FF00');
$location_of_image = Timber\ImageHelper::get_server_location($new_file);
$this->addFile($location_of_image);
$this->assertTrue(TestTimberImage::checkSize($location_of_image, 500, 500));
//whats the bg/color of the image
$image = imagecreatefromjpeg($location_of_image);
$pixel_rgb = imagecolorat($image, 1, 1);
$colors = imagecolorsforindex($image, $pixel_rgb);
$this->assertSame(0, $colors['red']);
$this->assertSame(255, $colors['green']);
}
public function testLetterboxTransparent()
{
$base_file = 'eastern-trans.png';
$file_loc = TestTimberImage::copyTestAttachment($base_file);
$upload_dir = wp_upload_dir();
$new_file = Timber\ImageHelper::letterbox($upload_dir['url'] . '/' . $base_file, 500, 500, '00FF00', true);
$location_of_image = Timber\ImageHelper::get_server_location($new_file);
$this->addFile($location_of_image);
$this->assertTrue(TestTimberImage::checkSize($location_of_image, 500, 500));
//whats the bg/color of the image
$is_green = TestTimberImage::checkPixel($location_of_image, 250, 250, '#00FF00');
$this->assertTrue($is_green);
$this->assertFileExists($location_of_image);
}
public function testLetterboxTransparentBackground()
{
$base_file = 'eastern-trans.png';
$file_loc = TestTimberImage::copyTestAttachment($base_file);
$upload_dir = wp_upload_dir();
$new_file = Timber\ImageHelper::letterbox($upload_dir['url'] . '/' . $base_file, 500, 500);
$location_of_image = Timber\ImageHelper::get_server_location($new_file);
$this->addFile($location_of_image);
$this->assertTrue(TestTimberImage::checkSize($location_of_image, 500, 500));
// whats the bg/color of the image?
$is_trans = TestTimberImage::checkPixel($location_of_image, 250, 250, false);
$this->assertFileExists($location_of_image);
$this->assertTrue($is_trans);
}
public function testLetterboxGif()
{
$base_file = 'panam.gif';
$file_loc = TestTimberImage::copyTestAttachment($base_file);
$upload_dir = wp_upload_dir();
$new_file = Timber\ImageHelper::letterbox($upload_dir['url'] . '/' . $base_file, 300, 100, '00FF00', true);
$location_of_image = Timber\ImageHelper::get_server_location($new_file);
$this->addFile($location_of_image);
$this->assertTrue(TestTimberImage::checkSize($location_of_image, 300, 100));
//whats the bg/color of the image
$this->assertTrue(TestTimberImage::checkPixel($location_of_image, 50, 10, "#00FF00", "#00FF10"));
$this->assertFileExists($location_of_image);
}
public function testLetterboxSixCharHex()
{
$data = [];
$file_loc = TestTimberImage::copyTestAttachment('eastern.jpg');
$upload_dir = wp_upload_dir();
$new_file = Timber\ImageHelper::letterbox($upload_dir['url'] . '/eastern.jpg', 500, 500, '#FFFFFF', true);
$location_of_image = Timber\ImageHelper::get_server_location($new_file);
$this->addFile($location_of_image);
$this->assertTrue(TestTimberImage::checkSize($location_of_image, 500, 500));
//whats the bg/color of the image
$image = imagecreatefromjpeg($location_of_image);
$pixel_rgb = imagecolorat($image, 1, 1);
$colors = imagecolorsforindex($image, $pixel_rgb);
$this->assertSame(255, $colors['red']);
$this->assertSame(255, $colors['blue']);
$this->assertSame(255, $colors['green']);
}
public function testImageLetterboxFilterNotAnImage()
{
self::enable_error_log(false);
$str = 'Image? {{"/wp-content/uploads/2016/07/stuff.jpg"|letterbox(500, 500)}}';
$compiled = Timber::compile_string($str);
$this->assertEquals('Image? /wp-content/uploads/2016/07/stuff.jpg', $compiled);
self::enable_error_log(true);
}
public function testSideloadedJPGWithLetterbox()
{
$url = 'https://pbs.twimg.com/profile_images/768086933310476288/acGwPDj4_400x400.jpg';
$sideloaded = Timber::compile_string('{{ file|letterbox(500, 500) }}', [
'file' => $url,
]);
$base_url = str_replace(basename($sideloaded), '', $sideloaded);
$expected = $base_url . md5($url) . '-lbox-500x500-trans.jpg';
$this->assertEquals($expected, $sideloaded);
}
public function testSideloadedPNGWithLetterbox()
{
$url = 'https://user-images.githubusercontent.com/2084481/31230351-116569a8-a9e4-11e7-8310-48b7f679892b.png';
$sideloaded = Timber::compile_string('{{ file|letterbox(500, 500) }}', [
'file' => $url,
]);
$base_url = str_replace(basename($sideloaded), '', $sideloaded);
$expected = $base_url . md5($url) . '-lbox-500x500-trans.png';
$this->assertEquals($expected, $sideloaded);
}
}