Merge branch 'MDL-62969_master' of git://github.com/markn86/moodle
[moodle.git] / lib / tests / gdlib_test.php
blob8875c3c87d5e153b468856482e9b2403588be76d
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 /**
18 * Test gd functionality.
20 * @package core
21 * @category phpunit
22 * @copyright 2015 Andrew Nicols <andrew@nicols.co.uk>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 /**
30 * A set of tests for some of the gd functionality within Moodle.
32 * @package core
33 * @category phpunit
34 * @copyright 2015 Andrew Nicols <andrew@nicols.co.uk>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class core_gdlib_testcase extends basic_testcase {
39 private $fixturepath = null;
41 public function setUp() {
42 $this->fixturepath = __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR;
45 public function test_generate_image_thumbnail() {
46 global $CFG;
47 require_once($CFG->libdir . '/gdlib.php');
49 // Test with meaningless data.
51 // Now use a fixture.
52 $pngpath = $this->fixturepath . 'gd-logo.png';
53 $pngthumb = generate_image_thumbnail($pngpath, 24, 24);
54 $this->assertTrue(is_string($pngthumb));
56 // And check that the generated image was of the correct proportions and mimetype.
57 $imageinfo = getimagesizefromstring($pngthumb);
58 $this->assertEquals(24, $imageinfo[0]);
59 $this->assertEquals(24, $imageinfo[1]);
60 $this->assertEquals('image/png', $imageinfo['mime']);
63 public function test_generate_image_thumbnail_from_string() {
64 global $CFG;
65 require_once($CFG->libdir . '/gdlib.php');
67 // Test with meaningless data.
69 // First empty values.
70 $this->assertFalse(generate_image_thumbnail_from_string('', 24, 24));
71 $this->assertFalse(generate_image_thumbnail_from_string('invalid', 0, 24));
72 $this->assertFalse(generate_image_thumbnail_from_string('invalid', 24, 0));
74 // Now an invalid string.
75 $this->assertFalse(generate_image_thumbnail_from_string('invalid', 24, 24));
77 // Now use a fixture.
78 $pngpath = $this->fixturepath . 'gd-logo.png';
79 $pngdata = file_get_contents($pngpath);
80 $pngthumb = generate_image_thumbnail_from_string($pngdata, 24, 24);
81 $this->assertTrue(is_string($pngthumb));
83 // And check that the generated image was of the correct proportions and mimetype.
84 $imageinfo = getimagesizefromstring($pngthumb);
85 $this->assertEquals(24, $imageinfo[0]);
86 $this->assertEquals(24, $imageinfo[1]);
87 $this->assertEquals('image/png', $imageinfo['mime']);
90 public function test_resize_image() {
91 global $CFG;
92 require_once($CFG->libdir . '/gdlib.php');
94 $pngpath = $this->fixturepath . 'gd-logo.png';
96 // Preferred height.
97 $newpng = resize_image($pngpath, null, 24);
98 $this->assertTrue(is_string($newpng));
99 $imageinfo = getimagesizefromstring($newpng);
100 $this->assertEquals(89, $imageinfo[0]);
101 $this->assertEquals(24, $imageinfo[1]);
102 $this->assertEquals('image/png', $imageinfo['mime']);
104 // Preferred width.
105 $newpng = resize_image($pngpath, 100, null);
106 $this->assertTrue(is_string($newpng));
107 $imageinfo = getimagesizefromstring($newpng);
108 $this->assertEquals(100, $imageinfo[0]);
109 $this->assertEquals(26, $imageinfo[1]);
110 $this->assertEquals('image/png', $imageinfo['mime']);
112 // Preferred width and height.
113 $newpng = resize_image($pngpath, 50, 50);
114 $this->assertTrue(is_string($newpng));
115 $imageinfo = getimagesizefromstring($newpng);
116 $this->assertEquals(50, $imageinfo[0]);
117 $this->assertEquals(13, $imageinfo[1]);
118 $this->assertEquals('image/png', $imageinfo['mime']);
121 public function test_resize_image_from_image() {
122 global $CFG;
123 require_once($CFG->libdir . '/gdlib.php');
125 $pngpath = $this->fixturepath . 'gd-logo.png';
126 $origimageinfo = getimagesize($pngpath);
127 $imagecontent = file_get_contents($pngpath);
129 // Preferred height.
130 $imageresource = imagecreatefromstring($imagecontent);
131 $newpng = resize_image_from_image($imageresource, $origimageinfo, null, 24);
132 $this->assertTrue(is_string($newpng));
133 $imageinfo = getimagesizefromstring($newpng);
134 $this->assertEquals(89, $imageinfo[0]);
135 $this->assertEquals(24, $imageinfo[1]);
136 $this->assertEquals('image/png', $imageinfo['mime']);
138 // Preferred width.
139 $imageresource = imagecreatefromstring($imagecontent);
140 $newpng = resize_image_from_image($imageresource, $origimageinfo, 100, null);
141 $this->assertTrue(is_string($newpng));
142 $imageinfo = getimagesizefromstring($newpng);
143 $this->assertEquals(100, $imageinfo[0]);
144 $this->assertEquals(26, $imageinfo[1]);
145 $this->assertEquals('image/png', $imageinfo['mime']);
147 // Preferred width and height.
148 $imageresource = imagecreatefromstring($imagecontent);
149 $newpng = resize_image_from_image($imageresource, $origimageinfo, 50, 50);
150 $this->assertTrue(is_string($newpng));
151 $imageinfo = getimagesizefromstring($newpng);
152 $this->assertEquals(50, $imageinfo[0]);
153 $this->assertEquals(13, $imageinfo[1]);
154 $this->assertEquals('image/png', $imageinfo['mime']);