2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * This script implements some useful svg manipulation tricks.
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 define('CLI_SCRIPT', true);
28 require(__DIR__
.'/../../config.php');
29 require_once($CFG->libdir
.'/clilib.php');
31 // Now get cli options.
32 list($options, $unrecognized) = cli_get_params(array('help'=>false, 'ie9fix'=>false, 'noaspectratio'=>false, 'path'=>$CFG->dirroot
),
36 $unrecognized = implode("\n ", $unrecognized);
37 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
40 // If necessary add files that should be ignored - such as in 3rd party plugins.
42 $path = $options['path'];
43 if (!file_exists($path)) {
44 cli_error("Invalid path $path");
47 if ($options['ie9fix']) {
48 core_admin_recurse_svgs($path, '', 'core_admin_svgtool_ie9fix', $blacklist);
50 } else if ($options['noaspectratio']) {
51 core_admin_recurse_svgs($path, '', 'core_admin_svgtool_noaspectratio', $blacklist);
55 "Some svg image tweaks for icon designers.
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
65 \$ php svgtool.php --ie9fix
66 \$ php svgtool.php --ie9fix --path=../../../pix
67 \$ php svgtool.php --noaspectratio
77 * Fixes SVG images for IE9.
81 function core_admin_svgtool_ie9fix($file) {
84 if (strpos($file, $CFG->dirroot
.DIRECTORY_SEPARATOR
) === 0) {
85 $relfile = substr($file, strlen($CFG->dirroot
));
90 $content = file_get_contents($file);
92 if (!preg_match('/<svg\s[^>]*>/', $content, $matches)) {
93 echo " skipping $relfile (invalid format)\n";
97 if (strpos($svg, 'preserveAspectRatio') !== false) {
101 if (!is_writable($file)) {
102 echo " skipping $relfile (can not modify file)\n";
106 $newsvg = rtrim($svg, '>').' preserveAspectRatio="xMinYMid meet">';
108 $content = str_replace($svg, $newsvg, $content);
109 echo "converting $relfile\n";
110 file_put_contents($file, $content);
114 * Removes preserveAspectRatio attributes from SVG images.
116 * @param string $file
118 function core_admin_svgtool_noaspectratio($file) {
121 if (strpos($file, $CFG->dirroot
.DIRECTORY_SEPARATOR
) === 0) {
122 $relfile = substr($file, strlen($CFG->dirroot
));
127 $content = file_get_contents($file);
129 if (!preg_match('/<svg\s[^>]*>/', $content, $matches)) {
130 echo " skipping $relfile (invalid format)\n";
134 if (strpos($svg, 'preserveAspectRatio="xMinYMid meet"') === false) {
138 if (!is_writable($file)) {
139 echo " skipping $relfile (can not modify file)\n";
143 $newsvg = preg_replace('/ ?preserveAspectRatio="xMinYMid meet"/', '', $svg);
145 $content = str_replace($svg, $newsvg, $content);
146 echo "resetting $relfile\n";
147 file_put_contents($file, $content);
151 * Recursively works through directories of this theme, finding and fixing SVG images.
153 * @param string $base
155 * @param string $filecallback
156 * @param array $blacklist
158 function core_admin_recurse_svgs($base, $sub, $filecallback, $blacklist) {
159 if (is_dir("$base/$sub")) {
160 $items = new DirectoryIterator("$base/$sub");
161 foreach ($items as $item) {
162 if ($item->isDot()) {
165 $file = $item->getFilename();
166 core_admin_recurse_svgs("$base/$sub", $file, $filecallback, $blacklist);
172 } else if (is_file("$base/$sub")) {
173 if (substr($sub, -4) !== '.svg') {
176 $file = realpath("$base/$sub");
177 if (in_array($file, $blacklist)) {
180 $filecallback($file);