Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * moodle_image class
  20   *
  21   * @package    repository
  22   * @subpackage flickr_public
  23   * @author     Dongsheng Cai <dongsheng@moodle.com>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
  25   */
  26  class moodle_image {
  27      private $imagepath;
  28      private $info;
  29      private $width;
  30      private $height;
  31      private $image;
  32      private $backup;
  33  
  34      function __construct($img) {
  35          ini_set('gd.jpeg_ignore_warning', 1);
  36          if(!function_exists('imagecreatefrompng')
  37              and !function_exists('imagecreatefromjpeg')) {
  38              throw new moodle_exception('gdnotexist');
  39          }
  40          if(!file_exists($img) or !is_readable($img)) {
  41              throw new moodle_exception('invalidfile');
  42          }
  43  
  44          $this->imagepath = $img;
  45          unset($img);
  46          $this->info = getimagesize($this->imagepath);
  47  
  48          switch($this->info['mime']) {
  49          case 'image/jpeg':
  50              $this->image = imagecreatefromjpeg($this->imagepath);
  51              break;
  52          case 'image/png':
  53              $this->image = imagecreatefrompng($this->imagepath);
  54              break;
  55          case 'image/gif':
  56              $this->image = imagecreatefromgif($this->imagepath);
  57              break;
  58          default:
  59              break;
  60          }
  61          $this->width  = imagesx($this->image);
  62          $this->height = imagesy($this->image);
  63      }
  64  
  65      function destroy() {
  66           imagedestroy($this->image);
  67           imagedestroy($this->backup);
  68           return true;
  69      }
  70  
  71      function undo() {
  72          $this->image = $this->backup;
  73          return $this;
  74      }
  75  
  76      function watermark($text='', $pos=array(), $options=array()) {
  77          global $CFG;
  78          $text = iconv('ISO-8859-8', 'UTF-8', $text);
  79          if (empty($options['fontsize'])) {
  80              if (!empty($options['ttf'])) {
  81                  $options['fontsize'] = 12;
  82              } else {
  83                  $options['fontsize'] = 1;
  84              }
  85          }
  86  
  87          if (empty($options['font'])) {
  88              $options['font'] = $CFG->libdir . '/default.ttf';
  89          }
  90          if (empty($options['angle'])) {
  91              $options['angle'] = 0;
  92          }
  93          $clr = imagecolorallocate($this->image, 255, 255, 255);
  94          if (!empty($options['ttf'])) {
  95              imagettftext($this->image,
  96                  $options['fontsize'],        // font size
  97                  $options['angle'],
  98                  $pos[0],
  99                  $pos[1]+$options['fontsize'],
 100                  $clr,
 101                  $options['font'],
 102                  $text);
 103          } else {
 104              imagestring($this->image, $options['fontsize'], $pos[0], $pos[1], $text, $clr);
 105          }
 106          return $this;
 107      }
 108  
 109      function rotate($angle=0, $bgcolor=0) {
 110          $this->image = imagerotate($this->image, $angle, $bgcolor);
 111          return $this;
 112      }
 113  
 114      function resize($w, $h, $use_resize = true) {
 115          if(empty($h) && !empty($w)) {
 116              $h = $this->height * ($w/$this->width);
 117          }
 118          if(!empty($h) && empty($w)) {
 119              $w = $this->width  * ($h/$this->height);
 120          }
 121          $new_img = imagecreatetruecolor($w, $h);
 122          imagealphablending($new_img, false);
 123          imagecopyresampled($new_img /* dst */, $this->image /* src */, 0, 0, 0, 0, $w, $h, $this->width, $this->height);
 124          $this->image = $new_img;
 125          return $this;
 126      }
 127  
 128      function saveas($imagepath) {
 129          switch($this->info['mime']) {
 130          case 'image/jpeg':
 131              return imagejpeg($this->image, $imagepath);
 132              break;
 133          case 'image/png':
 134              return imagepng($this->image, $imagepath);
 135              break;
 136          case 'image/gif':
 137              return imagegif($this->image, $imagepath);
 138              break;
 139          default:
 140              break;
 141          }
 142          if(!$this->destroy()) {
 143              return false;
 144          } else {
 145              return $this;
 146          }
 147      }
 148  
 149      function display() {
 150          header('Content-type: '.$this->info['mime']);
 151          switch($this->info['mime']) {
 152          case 'image/png':
 153              imagepng($this->image);
 154              break;
 155          case 'image/jpeg':
 156              imagejpeg($this->image);
 157              break;
 158          case 'image/gif':
 159              imagegif($this->image);
 160              break;
 161          default:
 162              break;
 163          }
 164          $this->destroy();
 165          return $this;
 166      }
 167  }
 168