use Cubist\Util\PHP;
use Fluidbook\Tools\FluidbookTools;
-class SVGTools {
+class SVGTools
+{
protected static $_r;
protected static $_i;
protected static $_e;
protected static $_u;
- public static function optimizeSVG($in, $out = null) {
+ public static function optimizeSVG($in, $out = null)
+ {
if (null === $out) {
$e = explode('.', $in);
$ext = array_pop($e);
$out = implode('.', $e);
}
+ if (file_exists($out) && filemtime($out) >= filemtime($in) && filesize($out) === 0) {
+ return $out;
+ }
+
return self::_optimizeSVG($in, $out);
}
- public static function optimizeSVGStr($in, $fix = false) {
+ public static function optimizeSVGStr($in, $fix = false)
+ {
$dir = Files::mkdir(sys_get_temp_dir() . '/svgopt');
`rm $dir/*.svg`;
return $res;
}
- public static function _optimizeSVG($in, $out) {
+ public static function _optimizeSVG($in, $out)
+ {
$beforeOpt = str_replace('.svg', '.orig.svg', $in);
if (file_exists($beforeOpt) && is_link($beforeOpt)) {
if (!$svg) {
$svg = file_get_contents($in);
}
+ $svg = str_replace('<svg ', '<svg xmlns="http://www.w3.org/2000/svg" ', $svg);
$svg = self::_disablePreserveRatio($svg);
file_put_contents($out, $svg);
return $out;
}
- protected static function _disablePreserveRatio($in) {
+ protected static function _disablePreserveRatio($in)
+ {
$str = 'preserveAspectRatio="none"';
if (str_contains($in, $str)) {
return $in;
return str_replace("<svg ", '<svg ' . $str . ' ', $in);
}
- public static function optimizeSVGImages($in, $out, $resolution) {
+ public static function optimizeSVGImages($in, $out, $resolution)
+ {
$svg = file_get_contents($in);
$svg = preg_replace('/\<\?xml([^\?]*)\?\>/', '', $svg);
$svg = self::_disablePreserveRatio($svg);
file_put_contents($out, $osvg);
}
- protected static function _svg($c, $p) {
+ protected static function _svg($c, $p)
+ {
self::$_i = 0;
self::$_e = 0;
self::$_u = 0;
return $c;
}
- public static function optimizeRaster($matches, $resolution) {
+ public static function optimizeRaster($matches, $resolution)
+ {
PHP::neverStop();
preg_match_all('/([a-z\:\-]*)="([^"]*)"/', $matches[1], $m);
return $res;
}
- public static function fix($source, $dest) {
+ public static function fix($source, $dest)
+ {
$cli = new CommandLine('oslllo-svg-fixer');
$cli->setArg('s', $source);
$cli->setArg('d', $dest);
}
}
- public static function append($first, $second, $dest, $direction = 'h') {
+ public static function append($first, $second, $dest, $direction = 'h')
+ {
$cli = new CommandLine('python3');
$cli->setArg(null, '/usr/local/svg_stack/svg_stack.py');
$cli->setArg('direction', $direction);