I wrote something to "compress" svg files. The svg files I am using often have comments and empty <g>
tags, and I want to remove them. My main goal is not the speed of the compression, but the size of the compressed svg file.
Here is an example svg file: https://image.flaticon.com/icons/svg/222/222436.svg
And here is the code I am using:
public function compress($svg)
{
$svg = preg_replace('/<!--.*-->/', '', $svg);
$svg = preg_replace('/<g>[\n\r\s]*<\/g>/', '', $svg);
$svg = preg_replace('/\n/', ' ', $svg);
$svg = preg_replace('/\t/', ' ', $svg);
$svg = preg_replace('/\s\s+/', ' ', $svg);
$svg = str_replace('> <', '><', $svg);
$svg = str_replace(';"', '"', $svg);
return $svg;
}
- Do you see any dangers here, perhaps this could ruin some svg files?
- Is there something I could do to compress the file even more?
- Is there any way to speed this up?