namespace Fluidbook\Tools\SVG;
+use Cubist\Util\CommandLine\Inkscape;
use Cubist\Util\Files\Files;
use Fluidbook\Tools\FluidbookTools;
+use Illuminate\Validation\Rules\In;
-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);
return self::_optimizeSVG($in, $out);
}
- public static function optimizeSVGStr($in)
- {
+ public static function optimizeSVGStr($in, $strokeToPaths = false) {
$infile = Files::tempnam() . '.svg';
file_put_contents($infile, $in);
$outfile = Files::tempnam() . '.svg';
self::optimizeSVG($infile, $outfile);
+ if ($strokeToPaths) {
+ self::strokeToPaths($outfile);
+ }
$res = file_get_contents($outfile);
unlink($infile);
unlink($outfile);
return $res;
}
- public static function _optimizeSVG($in, $out)
- {
+ public static function _optimizeSVG($in, $out) {
$svg = shell_exec('timeout -s 1 120 ' . FluidbookTools::tools_path('svgcleaner/svgcleaner', true) . ' --allow-bigger-file --paths-coordinates-precision 3 --copy-on-error --stdout ' . $in);
if (!$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) {
preg_match_all('/([a-z\:\-]*)="([^"]*)"/', $matches[1], $m);
foreach ($m[1] as $i => $key) {
$res .= '/>';
return $res;
}
+
+ public static function strokeToPaths($svgfile) {
+ $inkscape = new Inkscape();
+ $inkscape->strokeToPath($svgfile);
+ $inkscape->execute();
+ }
}