I made a system that a user uploads an Gerber File (Printed circuit board format), and then i convert with PHP that file (GCode), to SVG.
I’m facing a problem now, that is actualy a arquitecture problem.
Shall i save the SVG in a File, or on the Database?
And should i return the SVG as an JSON like ({name: test, data: SVGFILEGOESHERE}) or just render as a .svg? I mean, is JSON safe enough for a BIG data structure?
EDIT:
The Converted SVG will be used in many views in the website, such as: Products pages, configure pages… It’s not going to show only one time…
The main idea, is to send a GCode to the server, and once anyone requires that file, if it’s not rendered, then it renders, and saves the SVG on the database, or in a file, and store a cache, to avoid re-processing the same file many times.
The SVG, would be retrieved with ajax, and also rendered inserted on the page (but i think i will load everything with ajax).
The file once sent to server, will never be MODIFIED, but can be deleted, and re-sent…
Thanks
Update
For what you are doing, I would recommend storing the SVG as a separate file and just returning the SVG as an image (hint:
header("Content-type: image/svg+xml");in PHP).As a side note, you said
The SVG, would be retrieved with ajax, and also rendered with PHP on the page.This is not quite right; SVG is a text/XML file. PHP does not render the SVG, it only sends the code of the SVG to the client. The client machine must be the one to parse the SVG code and render it as a visible image to the client.Original Answer
The answer: it depends. You haven’t given us much to go on.
SVG is ASCII text, kind of like HTML. You can learn more here. As such, there isn’t anything “wrong” with sending an SVG file in JSON, just make sure any quotes are escaped.
JSON is fine for big structures; the issue isn’t it’s size, it’s the time it takes to send it from the server to the client, and then the time it takes for javascript to parse the JSON and render the SVG as an image. I don’t know what your setup is like, or how large the SVG files are, but for very large SVG images, you may want to put them in a separate request that returns only the SVG so the client machine isn’t taking time to parse the JSON. You’ll have to do performance testing on your application to see what is best for your needs.
As for storing the SVG as a file or in the Database, it depends on the database, how much memory it has for indexing and how the indexes are built, whether it’s SQL or NoSQL, or how much storage space it has, how much traffic you have on the site, how you are backing up the database and/or files, etc etc. People have used databases to store thumbnails for user images, so it can most definitely hold SVG files in it. It all depends on how fast and stable the database is. Personally, I prefer to keep images and large amounts of text in separate files on the hard drive.