weekly release 3.5.8+
[moodle.git] / admin / cli / svgtool.php
blob929a7c924de7c01381745026d4e45282899ebc6a
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 * This script implements some useful svg manipulation tricks.
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
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),
33 array('h'=>'help'));
35 if ($unrecognized) {
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.
41 $blacklist = array();
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);
53 } else {
54 $help =
55 "Some svg image tweaks for icon designers.
57 Options:
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
64 Examples:
65 \$ php svgtool.php --ie9fix
66 \$ php svgtool.php --ie9fix --path=../../../pix
67 \$ php svgtool.php --noaspectratio
70 echo $help;
71 die;
74 exit(0);
76 /**
77 * Fixes SVG images for IE9.
79 * @param string $file
81 function core_admin_svgtool_ie9fix($file) {
82 global $CFG;
84 if (strpos($file, $CFG->dirroot.DIRECTORY_SEPARATOR) === 0) {
85 $relfile = substr($file, strlen($CFG->dirroot));
86 } else {
87 $relfile = $file;
90 $content = file_get_contents($file);
92 if (!preg_match('/<svg\s[^>]*>/', $content, $matches)) {
93 echo " skipping $relfile (invalid format)\n";
94 return;
96 $svg = $matches[0];
97 if (strpos($svg, 'preserveAspectRatio') !== false) {
98 return;
101 if (!is_writable($file)) {
102 echo " skipping $relfile (can not modify file)\n";
103 return;
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) {
119 global $CFG;
121 if (strpos($file, $CFG->dirroot.DIRECTORY_SEPARATOR) === 0) {
122 $relfile = substr($file, strlen($CFG->dirroot));
123 } else {
124 $relfile = $file;
127 $content = file_get_contents($file);
129 if (!preg_match('/<svg\s[^>]*>/', $content, $matches)) {
130 echo " skipping $relfile (invalid format)\n";
131 return;
133 $svg = $matches[0];
134 if (strpos($svg, 'preserveAspectRatio="xMinYMid meet"') === false) {
135 return;
138 if (!is_writable($file)) {
139 echo " skipping $relfile (can not modify file)\n";
140 return;
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
154 * @param string $sub
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()) {
163 continue;
165 $file = $item->getFilename();
166 core_admin_recurse_svgs("$base/$sub", $file, $filecallback, $blacklist);
168 unset($item);
169 unset($items);
170 return;
172 } else if (is_file("$base/$sub")) {
173 if (substr($sub, -4) !== '.svg') {
174 return;
176 $file = realpath("$base/$sub");
177 if (in_array($file, $blacklist)) {
178 return;
180 $filecallback($file);