Merge branch 'MDL-75604-401' of https://github.com/ssj365/moodle into MOODLE_401_STABLE
[moodle.git] / lib / tests / gdlib_test.php
blobd322d33d5cf610bea6705e8c2031998ca201c989
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.
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/>.
17 namespace core;
19 /**
20 * A set of tests for some of the gd functionality within Moodle.
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
27 class gdlib_test extends \basic_testcase {
29 private $fixturepath = null;
31 public function setUp(): void {
32 $this->fixturepath = __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR;
35 public function test_generate_image_thumbnail() {
36 global $CFG;
37 require_once($CFG->libdir . '/gdlib.php');
39 // Test with meaningless data.
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));
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']);
53 public function test_generate_image_thumbnail_from_string() {
54 global $CFG;
55 require_once($CFG->libdir . '/gdlib.php');
57 // Test with meaningless data.
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));
64 // Now an invalid string.
65 $this->assertFalse(generate_image_thumbnail_from_string('invalid', 24, 24));
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));
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']);
80 public function test_resize_image() {
81 global $CFG;
82 require_once($CFG->libdir . '/gdlib.php');
84 $pngpath = $this->fixturepath . 'gd-logo.png';
86 // Preferred height.
87 $newpng = resize_image($pngpath, null, 24);
88 $this->assertTrue(is_string($newpng));
89 $imageinfo = getimagesizefromstring($newpng);
90 $this->assertEquals(89, $imageinfo[0]);
91 $this->assertEquals(24, $imageinfo[1]);
92 $this->assertEquals('image/png', $imageinfo['mime']);
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(26, $imageinfo[1]);
100 $this->assertEquals('image/png', $imageinfo['mime']);
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']);
111 public function test_resize_image_from_image() {
112 global $CFG;
113 require_once($CFG->libdir . '/gdlib.php');
115 $pngpath = $this->fixturepath . 'gd-logo.png';
116 $origimageinfo = getimagesize($pngpath);
117 $imagecontent = file_get_contents($pngpath);
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(89, $imageinfo[0]);
125 $this->assertEquals(24, $imageinfo[1]);
126 $this->assertEquals('image/png', $imageinfo['mime']);
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(26, $imageinfo[1]);
135 $this->assertEquals('image/png', $imageinfo['mime']);
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']);