return $out;
}
- return self::_optimizeSVG($in, $out);
+ return self::_optimizeSVG($in, $out, $opt);
}
public static function optimizeSVGStr($in, $fix = false, $opt = '')
}
copy($in, $beforeOpt);
- $svg = shell_exec('timeout -s 1 120 ' . FluidbookTools::tools_path('svgcleaner/svgcleaner', true) . ' ' . $opt . ' --allow-bigger-file --paths-coordinates-precision 3 --copy-on-error --stdout ' . $in);
+ try {
+ $xmlsvg = simplexml_load_string(file_get_contents($in));
+ $attr = $xmlsvg->attributes();
+ if (!isset($attr['width']) || !isset($attr['height'])) {
+ $viewbox = trim($attr['viewBox']);
+ $e = explode(' ', $viewbox);
+ if (!isset($attr['width'])) {
+ $xmlsvg->addAttribute('width', (float)$e[2]);
+ }
+ if (!isset($attr['height'])) {
+ $xmlsvg->addAttribute('height', (float)$e[3]);
+ }
+ $tmp = Files::tempnam() . '.svg';
+ file_put_contents($tmp, $xmlsvg->asXML());
+ }
+ } catch (\Exception $e) {
+
+ }
+
+ $cmd = 'timeout -s 1 120 ' . FluidbookTools::tools_path('svgcleaner/svgcleaner', true) . ' ' . $opt . ' --allow-bigger-file --paths-coordinates-precision 3 --copy-on-error --stdout ' . ($tmp ?? $in);
+ $svg = shell_exec($cmd);
+
if (!$svg) {
$svg = file_get_contents($in);
}
+
if (!stristr($svg, 'xmlns="http://www.w3.org/2000/svg"')) {
$svg = str_replace('<svg ', '<svg xmlns="http://www.w3.org/2000/svg" ', $svg);
}
$svg = self::_disablePreserveRatio($svg);
+
file_put_contents($out, $svg);
+ if (isset($tmp)) {
+ Files::unlink($tmp);
+ }
return $out;
}