简体   繁体   English

关于保存SVG文件和检索的疑问

[英]Doubts about saving SVG files and retrieving

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.我制作了一个用户上传 Gerber 文件(印刷电路板格式)的系统,然后我用 PHP 将该文件(GCode)转换为 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?我应该将 SVG 保存在文件中还是数据库中?

And should i return the SVG as an JSON like ({name: test, data: SVGFILEGOESHERE}) or just render as a .svg?我应该将 SVG 作为 JSON 返回 ({name: test, data: SVGFILEGOESHERE}) 还是只呈现为 .svg? I mean, is JSON safe enough for a BIG data structure?我的意思是,对于大数据结构来说,JSON 是否足够安全?

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...转换后的SVG会用在网站的很多视图中,比如:产品页面、配置页面……不会只显示一次……

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.主要思想是将 GCode 发送到服务器,一旦有人需要该文件,如果没有渲染,则渲染,并将 SVG 保存在数据库或文件中,并存储缓存,以避免重新- 多次处理同一个文件。

The SVG, would be retrieved with ajax, and also rendered inserted on the page (but i think i will load everything with ajax). SVG 将使用 ajax 检索,并渲染插入页面(但我想我会用 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).对于您正在做的事情,我建议将 SVG 存储为单独的文件,并将 SVG 作为图像返回(提示: header("Content-type: image/svg+xml");在 PHP 中)。

As a side note, you said The SVG, would be retrieved with ajax, and also rendered with PHP on the page.作为旁注,您说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. SVG 是一个文本/XML 文件。 PHP does not render the SVG, it only sends the code of the SVG to the client. PHP 不渲染 SVG,它只将 SVG 的代码发送给客户端。 The client machine must be the one to parse the SVG code and render it as a visible image to the client.客户端机器必须是解析 SVG 代码并将其呈现为客户端可见图像的机器。

Original Answer原答案

The answer: it depends .答案是:视情况而定 You haven't given us much to go on.你没有给我们太多的事情要做。

SVG is ASCII text, kind of like HTML. SVG 是 ASCII 文本,有点像 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 格式发送 SVG 文件没有任何“错误”,只需确保对任何引号进行转义即可。

JSON is fine for big structures; JSON 适用于大型结构; 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.问题不在于它的大小,而是将它从服务器发送到客户端所需的时间,然后是 javascript 解析 JSON 并将 SVG 呈现为图像所需的时间。 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.我不知道你的设置是什么样的,或者 SVG 文件有多大,但是对于非常大的 SVG 图像,你可能希望将它们放在一个单独的请求中,只返回 SVG,这样客户端机器就不会花时间解析 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.至于是把SVG存成文件还是存入数据库,取决于数据库,它有多少用于索引的内存以及索引是如何构建的,是SQL还是NoSQL,或者它有多少存储空间,有多少流量你在网站上,你如何备份数据库和/或文件等。人们使用数据库来存储用户图像的缩略图,所以它绝对可以在其中保存 SVG 文件。 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.就个人而言,我更喜欢将图像和大量文本保存在硬盘驱动器上的单独文件中。

It depends on (at least) two things:这取决于(至少)两件事:

  1. How you plan on using the data (can it be modified later?)您计划如何使用这些数据(以后可以修改吗?)
  2. How much traffic the site is going to receive.该网站将获得多少流量。

If the SVG data can be modified later, I would save it in the database.如果以后可以修改SVG数据,我会把它保存在数据库中。 JSON data should be fine there. JSON 数据在那里应该没问题。

If you think you're going to get a lot of traffic, then I would personally not save it in the database unless you have good caching mechanisms in place.如果您认为您将获得大量流量,那么我个人不会将其保存在数据库中,除非您有良好的缓存机制。 I'm not sure if you're using MySQL, but the query cache in MySQL might not be too happy about caching huge amounts of data.我不确定您是否在使用 MySQL,但 MySQL 中的查询缓存可能不太喜欢缓存大量数据。

What I would personally do is save the SVG in a file, and store it in such a way that it can be retrieved based on the associated record in the database, ie /uploads/svg/$username/$circuitboardid.svg我个人会做的是将SVG保存在一个文件中,并以可以根据数据库中的关联记录检索它的方式存储它,即/uploads/svg/$username/$circuitboardid.svg

If you have lots of users (30,000+), your svg directory will get huge and depending on the filesystem you may run into files-per-folder limits.如果您有很多用户(30,000+),您的 svg 目录将变得很大,并且根据文件系统,您可能会遇到每个文件夹的文件限制。

I accomplished this by using the following code segments in php and mySQL database : First of all, remember svg is basically stored in text just like HTML is also.我通过在 php 和 mySQL 数据库中使用以下代码段来实现这一点: 首先,记住 svg 基本上像 HTML 一样存储在文本中。 And svg tags, all be it different ones, are laid out very similar to HTML tags.和 svg 标签,无论是不同的标签,都与 HTML 标签非常相似。

Storing into the database.存入数据库。 You must use the following code segment in the actual mySQL Insertcall.您必须在实际的 mySQL Insertcall 中使用以下代码段。 I found out if you do this to the variable first and then put the variable in the insert call it will not work.我发现如果您先对变量执行此操作,然后将变量放入插入调用中,它将不起作用。 The function must be in the mySQL statement.该函数必须在 mySQL 语句中。
mysql_real_escape_string($myValue)

Retrieving Into textbox in value.检索到文本框的值。 Assuming your values have been already retrieved from the database and now are in an array Called theValues.假设您的值已经从数据库中检索到并且现在位于名为 theValues 的数组中。 Basically I am Removing any backslashes but before hand I'm making sure it can be displayed correctly using htmlentities.基本上我正在删除任何反斜杠,但在此之前我要确保它可以使用 htmlentities 正确显示。 Since you are no Backslashes in svg that I know of it fixes it where servers replace quotes with \\". If you do encounter some Backslashes in svg you'll just have to be a bit more clever in your replacement function.因为你在 svg 中没有反斜杠,我知道它修复了服务器用 \\" 替换引号的地方。如果你在 svg 中遇到一些反斜杠,你只需要在替换函数中更聪明一点。
$myValue= str_replace("\\\\", "", htmlentities($theValues->myValue)); echo $myValue;

echoing out on to a page same reasons as above, but the htmlentities function Makes it only display the text of the svg Instead of processing the svg And displaying your picture.回显到页面上的原因与上述相同,但 htmlentities 功能使其仅显示 svg 的文本而不是处理 svg 并显示您的图片。 this is only required for displaying svg after the text of the Has been stored in a data base but it will not hurt your display If it wasn't the data the first, just A needless function call .这仅用于在已存储在数据库中的文本之后显示 svg 但它不会损害您的显示如果它不是第一个数据,只是一个不必要的函数调用。
str_replace("\\\\", "",$myValue)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM