Search code examples
svg

How to remove space from svg file in PHP


How to remove the white space in svg file?

This is the my svg file.

<svg xmlns="http://www.w3.org/2000/svg" fill="none" xmlns:xlink="http://www.w3.org/1999/xlink" style="margin:auto;display:block;" width="100" height="100" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid">
<rect width="100%" height="100%" fill="none"/>

    <circle cx="72" cy="50" fill="#007a5c" r="4">
        <animate attributeName="r" values="2.4;2.4;4;2.4;2.4" times="0;0.1;0.2;0.3;1" dur="1s" repeatCount="indefinite" begin="-0.9s"></animate>
        <animate attributeName="fill" values="#007a5c;#007a5c;#125443;#007a5c;#007a5c" repeatCount="indefinite" times="0;0.1;0.2;0.3;1" dur="1s" begin="-0.9s"></animate>
    </circle>
    <circle cx="67.79837387624885" cy="62.93127555043441" fill="#007a5c" r="4">
        <animate attributeName="r" values="2.4;2.4;4;2.4;2.4" times="0;0.1;0.2;0.3;1" dur="1s" repeatCount="indefinite" begin="-0.8s"></animate>
        <animate attributeName="fill" values="#007a5c;#007a5c;#125443;#007a5c;#007a5c" repeatCount="indefinite" times="0;0.1;0.2;0.3;1" dur="1s" begin="-0.8s"></animate>
    </circle>
    <circle cx="56.798373876248846" cy="70.92324335849338" fill="#007a5c" r="4">
        <animate attributeName="r" values="2.4;2.4;4;2.4;2.4" times="0;0.1;0.2;0.3;1" dur="1s" repeatCount="indefinite" begin="-0.7s"></animate>
        <animate attributeName="fill" values="#007a5c;#007a5c;#125443;#007a5c;#007a5c" repeatCount="indefinite" times="0;0.1;0.2;0.3;1" dur="1s" begin="-0.7s"></animate>
    </circle>
    <circle cx="43.20162612375116" cy="70.92324335849338" fill="#007a5c" r="4">
        <animate attributeName="r" values="2.4;2.4;4;2.4;2.4" times="0;0.1;0.2;0.3;1" dur="1s" repeatCount="indefinite" begin="-0.6s"></animate>
        <animate attributeName="fill" values="#007a5c;#007a5c;#125443;#007a5c;#007a5c" repeatCount="indefinite" times="0;0.1;0.2;0.3;1" dur="1s" begin="-0.6s"></animate>
    </circle>
    <circle cx="32.201626123751154" cy="62.931275550434414" fill="#007a5c" r="4">
        <animate attributeName="r" values="2.4;2.4;4;2.4;2.4" times="0;0.1;0.2;0.3;1" dur="1s" repeatCount="indefinite" begin="-0.5s"></animate>
        <animate attributeName="fill" values="#007a5c;#007a5c;#125443;#007a5c;#007a5c" repeatCount="indefinite" times="0;0.1;0.2;0.3;1" dur="1s" begin="-0.5s"></animate>
    </circle>
    <circle cx="28" cy="50" fill="#007a5c" r="4">
        <animate attributeName="r" values="2.4;2.4;4;2.4;2.4" times="0;0.1;0.2;0.3;1" dur="1s" repeatCount="indefinite" begin="-0.4s"></animate>
        <animate attributeName="fill" values="#007a5c;#007a5c;#125443;#007a5c;#007a5c" repeatCount="indefinite" times="0;0.1;0.2;0.3;1" dur="1s" begin="-0.4s"></animate>
    </circle>
    <circle cx="32.201626123751154" cy="37.06872444956559" fill="#007a5c" r="4">
        <animate attributeName="r" values="2.4;2.4;4;2.4;2.4" times="0;0.1;0.2;0.3;1" dur="1s" repeatCount="indefinite" begin="-0.3s"></animate>
        <animate attributeName="fill" values="#007a5c;#007a5c;#125443;#007a5c;#007a5c" repeatCount="indefinite" times="0;0.1;0.2;0.3;1" dur="1s" begin="-0.3s"></animate>
    </circle>
    <circle cx="43.201626123751154" cy="29.076756641506623" fill="#007a5c" r="4">
        <animate attributeName="r" values="2.4;2.4;4;2.4;2.4" times="0;0.1;0.2;0.3;1" dur="1s" repeatCount="indefinite" begin="-0.2s"></animate>
        <animate attributeName="fill" values="#007a5c;#007a5c;#125443;#007a5c;#007a5c" repeatCount="indefinite" times="0;0.1;0.2;0.3;1" dur="1s" begin="-0.2s"></animate>
    </circle>
    <circle cx="56.79837387624884" cy="29.07675664150662" fill="#007a5c" r="4">
        <animate attributeName="r" values="2.4;2.4;4;2.4;2.4" times="0;0.1;0.2;0.3;1" dur="1s" repeatCount="indefinite" begin="-0.1s"></animate>
        <animate attributeName="fill" values="#007a5c;#007a5c;#125443;#007a5c;#007a5c" repeatCount="indefinite" times="0;0.1;0.2;0.3;1" dur="1s" begin="-0.1s"></animate>
    </circle>
    <circle cx="67.79837387624885" cy="37.068724449565586" fill="#007a5c" r="4">
        <animate attributeName="r" values="2.4;2.4;4;2.4;2.4" times="0;0.1;0.2;0.3;1" dur="1s" repeatCount="indefinite" begin="0s"></animate>
        <animate attributeName="fill" values="#007a5c;#007a5c;#125443;#007a5c;#007a5c" repeatCount="indefinite" times="0;0.1;0.2;0.3;1" dur="1s" begin="0s"></animate>
    </circle>
</svg>

I have used imagemagick, but it doesn't work well.


Solution

  • Looks like you calculate <circle> positions with PHP Server Side

    You can save server CPU Cycles with Christian his answer using rotate,
    and a Web Component in the <head> of your document
    to generate the SVG Client Side (and no spaces in the HTML) and animate with SMIL

    (I dismissed the subtle animated color change for brevity)

    <svg-spinner></svg-spinner>
    <svg-spinner dots=18></svg-spinner>
    
    <script>
      customElements.define("svg-spinner", class extends HTMLElement {
        connectedCallback() {
          this.innerHTML = 
            `<svg height=100 viewBox="-12 -12 24 24" style=background:beige>` +
              Array( ~~(this.getAttribute("dots")  || 10) )
                .fill(  this.getAttribute("color") || "#007a5c" )
                .map((v, i, a) =>
                  `<circle transform=rotate(${i*(360/a.length)}) fill=${v} cx=10>` +
                  `  <animate attributeName=r begin=${i*1/a.length} values=1;2 
                              dur=1s repeatCount=indefinite />` +
                  `</circle>`)
                .join("") + 
            `</svg>`
        }
      })
    </script>
    
    <svg-spinner color=blue></svg-spinner>

    And for those saying But this requires JavaScript...
    If you need a spinner on a webpage I bet you, my wife's car, your user is waiting for a sh*load of JavaScript to download.