egroupware/pixelegg/stylesheet2svg.php
Hadi Nategh 845e37e66d SVG images W.I.P.:
- Add svg icons
- Implement color change for stylesheet2svg script
2016-02-23 18:49:14 +00:00

86 lines
2.6 KiB
PHP
Executable File

#!/usr/bin/php
<?php
/**
* helper to add a stylesheet to a svg image
*
* @link http://www.egroupware.org
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @copyright (c) 2014 by Ralf Becker <rb@stylite.de>
* @version $Id$
*/
if (php_sapi_name() !== 'cli') die("This is a commandline ONLY tool!\n");
$args = $_SERVER['argv'];
$prog = array_shift($args);
if ($_SERVER['argc'] <= 1 || $prog != 'pixelegg/stylesheet2svg.php') die("
Usage: pixelegg/stylesheet2svg [-s stylesheet|-c color] svg-image(s)
Add an external stylesheet to an svg image and sets id of svg tag to app_image
Examples:
- pixelegg/stylesheet2svg -s pixelegg/less/svg.css */templates/pixelegg/images/*.svg pixelegg/images/*.svg
- pixelegg/stylesheet2svg -c '#6e6e6e,#939393' */templates/pixelegg/images/*.svg pixelegg/images/*.svg
\n");
$stylesheet = 'pixelegg/less/svg.css';
if ($args[0] == '-s')
{
$stylesheet = $args[1];
array_shift($args);
array_shift($args);
}
if ($args[0] == '-c')
{
$color = explode(',',$args[1]);
array_shift($args);
array_shift($args);
}
foreach($args as $path)
{
if (!preg_match('|^([^/]+)/.*/(.*).svg$|', $path, $matches) || !($svg = file_get_contents($path)))
{
error_log("SVG image $path NOT found or empty!\n");
continue;
}
// remove evtl. existing old stylesheet
$svg = preg_replace("|<\\?xml-stylesheet[^?]+\\?>\n?|", '', $svg);
// add stylesheet
$style_url = rel_path($stylesheet, $path);
$svg = preg_replace('|<svg|', '<?xml-stylesheet type="text/css" href="'.$style_url.'" ?>'."\n".'<svg', $svg);
// change id to app_image
$id = $matches[1].'_'.$matches[2];
$svg = preg_replace('/(<svg.*) id="[^"]+"/', '\\1 id="'.$id.'"', $svg);
// Replace fill color with color[0]
$svg = preg_replace('/(<.*) fill="[^"]*"/','\\1 fill="'.$color[0] .'"', $svg);
// Replace stroke color with color[1]
$svg = preg_replace('/(<.*) stroke="[^"]*"/', '\\1 stroke="'.$color[1] .'"', $svg);
error_log(__METHOD__."svg: ". var_dump($svg));
// store image again
file_put_contents($path, $svg);
echo "$path: added $style_url and id=\"$id\"\n";
}
function rel_path($stylesheet, $image)
{
$i_parts = explode('/', $image);
array_pop($i_parts);
$rel_parts = $s_parts = explode('/', $stylesheet);
foreach($i_parts as $n => $i_part)
{
if (isset($s_parts[$n]) && $s_parts[$n] === $i_part)
{
array_shift($rel_parts);
}
else
{
array_unshift($rel_parts, '..');
}
}
$rel_path = implode('/', $rel_parts);
//error_log(__FUNCTION__."($stylesheet, $image) returned $rel_path");
return $rel_path;
}