1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* A set of tests for some of the gd functionality within Moodle.
|
|
|
21 |
*
|
|
|
22 |
* @package core
|
|
|
23 |
* @category test
|
|
|
24 |
* @copyright 2015 Andrew Nicols <andrew@nicols.co.uk>
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
*/
|
|
|
27 |
class gdlib_test extends \basic_testcase {
|
|
|
28 |
|
|
|
29 |
private $fixturepath = null;
|
|
|
30 |
|
|
|
31 |
public function setUp(): void {
|
|
|
32 |
$this->fixturepath = __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
public function test_generate_image_thumbnail() {
|
|
|
36 |
global $CFG;
|
|
|
37 |
require_once($CFG->libdir . '/gdlib.php');
|
|
|
38 |
|
|
|
39 |
// Test with meaningless data.
|
|
|
40 |
|
|
|
41 |
// Now use a fixture.
|
|
|
42 |
$pngpath = $this->fixturepath . 'gd-logo.png';
|
|
|
43 |
$pngthumb = generate_image_thumbnail($pngpath, 24, 24);
|
|
|
44 |
$this->assertTrue(is_string($pngthumb));
|
|
|
45 |
|
|
|
46 |
// And check that the generated image was of the correct proportions and mimetype.
|
|
|
47 |
$imageinfo = getimagesizefromstring($pngthumb);
|
|
|
48 |
$this->assertEquals(24, $imageinfo[0]);
|
|
|
49 |
$this->assertEquals(24, $imageinfo[1]);
|
|
|
50 |
$this->assertEquals('image/png', $imageinfo['mime']);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
public function test_generate_image_thumbnail_from_string() {
|
|
|
54 |
global $CFG;
|
|
|
55 |
require_once($CFG->libdir . '/gdlib.php');
|
|
|
56 |
|
|
|
57 |
// Test with meaningless data.
|
|
|
58 |
|
|
|
59 |
// First empty values.
|
|
|
60 |
$this->assertFalse(generate_image_thumbnail_from_string('', 24, 24));
|
|
|
61 |
$this->assertFalse(generate_image_thumbnail_from_string('invalid', 0, 24));
|
|
|
62 |
$this->assertFalse(generate_image_thumbnail_from_string('invalid', 24, 0));
|
|
|
63 |
|
|
|
64 |
// Now an invalid string.
|
|
|
65 |
$this->assertFalse(generate_image_thumbnail_from_string('invalid', 24, 24));
|
|
|
66 |
|
|
|
67 |
// Now use a fixture.
|
|
|
68 |
$pngpath = $this->fixturepath . 'gd-logo.png';
|
|
|
69 |
$pngdata = file_get_contents($pngpath);
|
|
|
70 |
$pngthumb = generate_image_thumbnail_from_string($pngdata, 24, 24);
|
|
|
71 |
$this->assertTrue(is_string($pngthumb));
|
|
|
72 |
|
|
|
73 |
// And check that the generated image was of the correct proportions and mimetype.
|
|
|
74 |
$imageinfo = getimagesizefromstring($pngthumb);
|
|
|
75 |
$this->assertEquals(24, $imageinfo[0]);
|
|
|
76 |
$this->assertEquals(24, $imageinfo[1]);
|
|
|
77 |
$this->assertEquals('image/png', $imageinfo['mime']);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
public function test_resize_image() {
|
|
|
81 |
global $CFG;
|
|
|
82 |
require_once($CFG->libdir . '/gdlib.php');
|
|
|
83 |
|
|
|
84 |
$pngpath = $this->fixturepath . 'gd-logo.png';
|
|
|
85 |
|
|
|
86 |
// Preferred height.
|
|
|
87 |
$newpng = resize_image($pngpath, null, 24);
|
|
|
88 |
$this->assertTrue(is_string($newpng));
|
|
|
89 |
$imageinfo = getimagesizefromstring($newpng);
|
|
|
90 |
$this->assertEquals(90, $imageinfo[0]);
|
|
|
91 |
$this->assertEquals(24, $imageinfo[1]);
|
|
|
92 |
$this->assertEquals('image/png', $imageinfo['mime']);
|
|
|
93 |
|
|
|
94 |
// Preferred width.
|
|
|
95 |
$newpng = resize_image($pngpath, 100, null);
|
|
|
96 |
$this->assertTrue(is_string($newpng));
|
|
|
97 |
$imageinfo = getimagesizefromstring($newpng);
|
|
|
98 |
$this->assertEquals(100, $imageinfo[0]);
|
|
|
99 |
$this->assertEquals(27, $imageinfo[1]);
|
|
|
100 |
$this->assertEquals('image/png', $imageinfo['mime']);
|
|
|
101 |
|
|
|
102 |
// Preferred width and height.
|
|
|
103 |
$newpng = resize_image($pngpath, 50, 50);
|
|
|
104 |
$this->assertTrue(is_string($newpng));
|
|
|
105 |
$imageinfo = getimagesizefromstring($newpng);
|
|
|
106 |
$this->assertEquals(50, $imageinfo[0]);
|
|
|
107 |
$this->assertEquals(13, $imageinfo[1]);
|
|
|
108 |
$this->assertEquals('image/png', $imageinfo['mime']);
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
public function test_resize_image_from_image() {
|
|
|
112 |
global $CFG;
|
|
|
113 |
require_once($CFG->libdir . '/gdlib.php');
|
|
|
114 |
|
|
|
115 |
$pngpath = $this->fixturepath . 'gd-logo.png';
|
|
|
116 |
$origimageinfo = getimagesize($pngpath);
|
|
|
117 |
$imagecontent = file_get_contents($pngpath);
|
|
|
118 |
|
|
|
119 |
// Preferred height.
|
|
|
120 |
$imageresource = imagecreatefromstring($imagecontent);
|
|
|
121 |
$newpng = resize_image_from_image($imageresource, $origimageinfo, null, 24);
|
|
|
122 |
$this->assertTrue(is_string($newpng));
|
|
|
123 |
$imageinfo = getimagesizefromstring($newpng);
|
|
|
124 |
$this->assertEquals(90, $imageinfo[0]);
|
|
|
125 |
$this->assertEquals(24, $imageinfo[1]);
|
|
|
126 |
$this->assertEquals('image/png', $imageinfo['mime']);
|
|
|
127 |
|
|
|
128 |
// Preferred width.
|
|
|
129 |
$imageresource = imagecreatefromstring($imagecontent);
|
|
|
130 |
$newpng = resize_image_from_image($imageresource, $origimageinfo, 100, null);
|
|
|
131 |
$this->assertTrue(is_string($newpng));
|
|
|
132 |
$imageinfo = getimagesizefromstring($newpng);
|
|
|
133 |
$this->assertEquals(100, $imageinfo[0]);
|
|
|
134 |
$this->assertEquals(27, $imageinfo[1]);
|
|
|
135 |
$this->assertEquals('image/png', $imageinfo['mime']);
|
|
|
136 |
|
|
|
137 |
// Preferred width and height.
|
|
|
138 |
$imageresource = imagecreatefromstring($imagecontent);
|
|
|
139 |
$newpng = resize_image_from_image($imageresource, $origimageinfo, 50, 50);
|
|
|
140 |
$this->assertTrue(is_string($newpng));
|
|
|
141 |
$imageinfo = getimagesizefromstring($newpng);
|
|
|
142 |
$this->assertEquals(50, $imageinfo[0]);
|
|
|
143 |
$this->assertEquals(13, $imageinfo[1]);
|
|
|
144 |
$this->assertEquals('image/png', $imageinfo['mime']);
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
}
|