use Cubist\Util\Files\Files;
use Cubist\Util\PHP;
use Fluidbook\Tools\FluidbookTools;
+use Illuminate\Support\Facades\Log;
class SVGTools
{
}
copy($in, $beforeOpt);
+ $cmd = 'timeout -s 1 120 ' . FluidbookTools::tools_path('svgcleaner/svgcleaner', true) . ' ' . $opt . ' --allow-bigger-file --paths-coordinates-precision 3 --copy-on-error --stdout ' . $in;
+ $svg = shell_exec($cmd);
+
+ Log::debug($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);
+ return $out;
+ }
+
+ public static function addDimensionsAttributes($in)
+ {
+ if (!strstr($in, '<svg') && file_exists($in)) {
+ $in = file_get_contents($in);
+ }
try {
- $xmlsvg = simplexml_load_string(file_get_contents($in));
+ $xmlsvg = simplexml_load_string($in);
$attr = $xmlsvg->attributes();
if (!isset($attr['width']) || !isset($attr['height'])) {
$viewbox = trim($attr['viewBox']);
if (!isset($attr['height'])) {
$xmlsvg->addAttribute('height', (float)$e[3]);
}
- $tmp = Files::tempnam() . '.svg';
- file_put_contents($tmp, $xmlsvg->asXML());
+ return preg_replace('\<\?xml.*\?\>', '', $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;
+ return self::_disablePreserveRatio($in);
}
protected static function _disablePreserveRatio($in)