2

how can i convert a svg string

<svg>....<path>...</path>...</svg>

to a png base64 encoded string / file in php?

Martin Huwa
  • 401
  • 3
  • 7
  • 11
  • 1
    This is a duplicate of [this question](http://stackoverflow.com/questions/2995640/svg-to-jpg-png). – ldiqual Sep 19 '11 at 10:54
  • [This question](http://stackoverflow.com/questions/4809194/convert-svg-image-to-png-with-php) is an even closer match I think. – Erik Dahlström Sep 20 '11 at 09:15

2 Answers2

2

Take a look to svg2png library. Github project here.

An example of use:

data:image/svg+xml;base64, -> base64_encode()

Juan Antonio
  • 2,451
  • 3
  • 24
  • 34
user956584
  • 5,316
  • 3
  • 40
  • 50
1

Use this function

function svgToBase64 ($filepath){  

    if (file_exists($filepath)){

        $filetype = pathinfo($filepath, PATHINFO_EXTENSION);

        if ($filetype==='svg'){
            $filetype .= '+xml';
        }

        $get_img = file_get_contents($filepath);
        return 'data:image/' . $filetype . ';base64,' . base64_encode($get_img );
    }
}
Mansour Alnasser
  • 4,446
  • 5
  • 40
  • 51