Mujtaba Haider
Mujtaba Haider

Reputation: 1650

SVG to JSON on server side?

Our web application is storing SVG files on server, we want to get JSON outputs from SVG files on server side. I've looked into PETESAIA's SVG to JSON php program. But the output i am getting is null or an empty array.

<?php
require_once “PeachSVG.php”;
$filename = “filename-2012-03-06.svg”;
$json = PeachSVG::convert($filename, $to_json = true);
//$json = convert($filename, $to_json = true);   //also used this one
var_dump(json_decode($json, true));
?>

This php code, PeachSVG.php and the svg file are in the same directory.

Can anyone suggest where i am wrong going with this?

Or any alternative of SVG to JSON on server side

EDIT : In response to @halfer and his query about why we need server side validation of SVG (converted to JSON). We have a cleint-side SVG(RaphaelJs) web app in which a user can perform certain actions, output is sent to and saved on our server and posted on a website. We want to make sure that output file is validated before posted on the website. For this we need to have server side validation to make sure that the user does not abuse the rules set in the application.

Raphael.serialize can not be used because it converts SVG to JSON on the client side which may be abused by the user. So we sending the SVG document as a string to server side.

Upvotes: 2

Views: 3061

Answers (3)

Andrew Shaban
Andrew Shaban

Reputation: 137

You made a mistake in require_once() function. The path to the php file should be in parentheses, like this:

require_once("PeachSVG.php");

And for strings you seem to use not good double quotes. You probably copied them from somewhere. Because these are left double quotation mark "“" (U+201C) and right double quotation mark "”" (U+201D). In code it should look not like this:

“some your string”

but like this:

"some your string"

Upvotes: 1

Prince Uchendu
Prince Uchendu

Reputation: 21

Your Script ran very fine on my localhost server but i have to remove the quotation marks you had and replace it with one from my notepad++ which looks like string quotes to me. hope this helps if had not found a solution yet

Upvotes: 0

ActionOwl
ActionOwl

Reputation: 1473

If you can install Node.js on your server you might be able to use fabric.js to parse the SVG then export the objects as JSON.

https://github.com/kangax/fabric.js

http://kangax.github.com/fabric.js/svg_rendering/

Upvotes: 1

Related Questions