Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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  /**
  18   * Test gd functionality.
  19   *
  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
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  
  29  /**
  30   * A set of tests for some of the gd functionality within Moodle.
  31   *
  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
  36   */
  37  class core_gdlib_testcase extends basic_testcase {
  38  
  39      private $fixturepath = null;
  40  
  41      public function setUp() {
  42          $this->fixturepath = __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR;
  43      }
  44  
  45      public function test_generate_image_thumbnail() {
  46          global $CFG;
  47          require_once($CFG->libdir . '/gdlib.php');
  48  
  49          // Test with meaningless data.
  50  
  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));
  55  
  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']);
  61      }
  62  
  63      public function test_generate_image_thumbnail_from_string() {
  64          global $CFG;
  65          require_once($CFG->libdir . '/gdlib.php');
  66  
  67          // Test with meaningless data.
  68  
  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));
  73  
  74          // Now an invalid string.
  75          $this->assertFalse(generate_image_thumbnail_from_string('invalid', 24, 24));
  76  
  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));
  82  
  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']);
  88      }
  89  
  90      public function test_resize_image() {
  91          global $CFG;
  92          require_once($CFG->libdir . '/gdlib.php');
  93  
  94          $pngpath = $this->fixturepath . 'gd-logo.png';
  95  
  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']);
 103  
 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']);
 111  
 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']);
 119      }
 120  
 121      public function test_resize_image_from_image() {
 122          global $CFG;
 123          require_once($CFG->libdir . '/gdlib.php');
 124  
 125          $pngpath = $this->fixturepath . 'gd-logo.png';
 126          $origimageinfo = getimagesize($pngpath);
 127          $imagecontent = file_get_contents($pngpath);
 128  
 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']);
 137  
 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']);
 146  
 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']);
 155      }
 156  
 157  }