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.

Differences Between: [Versions 39 and 401]

   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   * This script implements some useful svg manipulation tricks.
  19   *
  20   * @package    core_admin
  21   * @subpackage cli
  22   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  define('CLI_SCRIPT', true);
  27  
  28  require(__DIR__.'/../../config.php');
  29  require_once($CFG->libdir.'/clilib.php');
  30  
  31  // Now get cli options.
  32  list($options, $unrecognized) = cli_get_params(array('help'=>false, 'ie9fix'=>false, 'noaspectratio'=>false, 'path'=>$CFG->dirroot),
  33      array('h'=>'help'));
  34  
  35  if ($unrecognized) {
  36      $unrecognized = implode("\n  ", $unrecognized);
  37      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  38  }
  39  
  40  // If necessary add files that should be ignored - such as in 3rd party plugins.
  41  $ignorelist = array();
  42  $path = $options['path'];
  43  if (!file_exists($path)) {
  44      cli_error("Invalid path $path");
  45  }
  46  
  47  if ($options['ie9fix']) {
  48      core_admin_recurse_svgs($path, '', 'core_admin_svgtool_ie9fix', $ignorelist);
  49  
  50  } else if ($options['noaspectratio']) {
  51      core_admin_recurse_svgs($path, '', 'core_admin_svgtool_noaspectratio', $ignorelist);
  52  
  53  } else {
  54      $help =
  55          "Some svg image tweaks for icon designers.
  56  
  57  Options:
  58  
  59  -h, --help            Print out this help
  60  --ie9fix              Adds preserveAspectRatio=\"xMinYMid meet\" to every svg image
  61  --noaspectratio       Removes preserveAspectRatio from svg files
  62  --path=PATH           Path to directory or file to be converted, by default \$CFG->dirroot
  63  
  64  Examples:
  65  \$ php svgtool.php --ie9fix
  66  \$ php svgtool.php --ie9fix --path=../../../pix
  67  \$ php svgtool.php --noaspectratio
  68  ";
  69  
  70      echo $help;
  71      die;
  72  }
  73  
  74  exit(0);
  75  
  76  /**
  77   * Fixes SVG images for IE9.
  78   *
  79   * @param string $file
  80   */
  81  function core_admin_svgtool_ie9fix($file) {
  82      global $CFG;
  83  
  84      if (strpos($file, $CFG->dirroot.DIRECTORY_SEPARATOR) === 0) {
  85          $relfile = substr($file, strlen($CFG->dirroot));
  86      } else {
  87          $relfile = $file;
  88      }
  89  
  90      $content = file_get_contents($file);
  91  
  92      if (!preg_match('/<svg\s[^>]*>/', $content, $matches)) {
  93          echo "  skipping $relfile (invalid format)\n";
  94          return;
  95      }
  96      $svg = $matches[0];
  97      if (strpos($svg, 'preserveAspectRatio') !== false) {
  98          return;
  99      }
 100  
 101      if (!is_writable($file)) {
 102          echo "  skipping $relfile (can not modify file)\n";
 103          return;
 104      }
 105  
 106      $newsvg = rtrim($svg, '>').' preserveAspectRatio="xMinYMid meet">';
 107  
 108      $content = str_replace($svg, $newsvg, $content);
 109      echo "converting $relfile\n";
 110      file_put_contents($file, $content);
 111  }
 112  
 113  /**
 114   * Removes preserveAspectRatio attributes from SVG images.
 115   *
 116   * @param string $file
 117   */
 118  function core_admin_svgtool_noaspectratio($file) {
 119      global $CFG;
 120  
 121      if (strpos($file, $CFG->dirroot.DIRECTORY_SEPARATOR) === 0) {
 122          $relfile = substr($file, strlen($CFG->dirroot));
 123      } else {
 124          $relfile = $file;
 125      }
 126  
 127      $content = file_get_contents($file);
 128  
 129      if (!preg_match('/<svg\s[^>]*>/', $content, $matches)) {
 130          echo "  skipping $relfile (invalid format)\n";
 131          return;
 132      }
 133      $svg = $matches[0];
 134      if (strpos($svg, 'preserveAspectRatio="xMinYMid meet"') === false) {
 135          return;
 136      }
 137  
 138      if (!is_writable($file)) {
 139          echo "  skipping $relfile (can not modify file)\n";
 140          return;
 141      }
 142  
 143      $newsvg = preg_replace('/ ?preserveAspectRatio="xMinYMid meet"/', '', $svg);
 144  
 145      $content = str_replace($svg, $newsvg, $content);
 146      echo "resetting $relfile\n";
 147      file_put_contents($file, $content);
 148  }
 149  
 150  /**
 151   * Recursively works through directories of this theme, finding and fixing SVG images.
 152   *
 153   * @param string $base
 154   * @param string $sub
 155   * @param string $filecallback
 156   * @param array $ignorelist List of files to be ignored and skipped.
 157   */
 158  function core_admin_recurse_svgs($base, $sub, $filecallback, $ignorelist) {
 159      if (is_dir("$base/$sub")) {
 160          $items = new DirectoryIterator("$base/$sub");
 161          foreach ($items as $item) {
 162              if ($item->isDot()) {
 163                  continue;
 164              }
 165              $file = $item->getFilename();
 166              core_admin_recurse_svgs("$base/$sub", $file, $filecallback, $ignorelist);
 167          }
 168          unset($item);
 169          unset($items);
 170          return;
 171  
 172      } else if (is_file("$base/$sub")) {
 173          if (substr($sub, -4) !== '.svg') {
 174              return;
 175          }
 176          $file = realpath("$base/$sub");
 177          if (in_array($file, $ignorelist)) {
 178              return;
 179          }
 180          $filecallback($file);
 181      }
 182  }