简体   繁体   English

在服务器上将画布另存为svg和json

[英]Save canvas as svg and json on server

i want to save canvas ( it have image,text,shape etc ) to svg on server. 我想将画布(具有图像,文本,形状等)保存到服务器上的svg中。

actually i want the canvas save the canvas in 3 formats - png file, svg file and JSON file. 实际上,我希望画布将画布保存为3种格式-png文件,svg文件和JSON文件。

this is my canvas to png code: 这是我的画布到png代码:

jquery : jQuery的:

jQuery(document).ready(function(){
    jQuery('#btnSave').click(function(){
     var pic =document.getElementById("myCanvas").toDataURL('image/png');
         $.ajax({
          type: "POST",
          url: "script.php",
          data: { 
             img: pic
          }
        }).done(function(o) {
          console.log('saved'); 
        });
    });
});

php code (script.php): php代码(script.php):

if (isset($_POST['img'])) {
    define('UPLOAD_DIR', 'uploaddesign/');
    $img = $_POST['img'];
    $img = str_replace('data:image/png;base64,', '', $img);
    $img = str_replace(' ', '+', $img);
    $data = base64_decode($img);
    $file = UPLOAD_DIR . uniqid() . '.png';
    $success = file_put_contents($file, $data);
    print $success ? $file : 'Unable to save the file.';
}

anyone know how to save canvas in other two format , svg and json : 任何人都知道如何将画布保存为其他两种格式svg和json:

JSON is a format for structured data. JSON是结构化数据的格式。 There is no meaningfull way to save an image as JSON. 没有将图像另存为JSON的有意义方法。

As for SVG, it is a format for vector grafics, while the image you get from the canvas is a rasterized image. 对于SVG,它是矢量grafics的格式,而从画布获得的图像是栅格化图像。 A conversion from raster images to vector images is mostly considered to be a bad idea. 从光栅图像到矢量图像的转换通常被认为是一个坏主意。 You can read up on that for example in the tutorial on the ImageMagick library. 您可以例如在ImageMagick库的教程中阅读有关内容。 While the library offers such a conversion, it warns 虽然图书馆提供了这种转换,但它警告

In other words, any output from IM will never be a true vector format. 换句话说,IM的任何输出都绝不会是真正的矢量格式。 While it can convert its internal raster format into a vector format file, the result is only a superficial vector image wrapper around an image in raster format. 虽然它可以将其内部栅格格式转换为矢量格式文件,但结果只是围绕栅格格式图像的表面矢量图像包装。 And unless the raster image is defined properly (at the right resolution) for the output device, the result will not be particularly good. 并且除非为输出设备正确定义了光栅图像(以正确的分辨率),否则结果将不会特别好。

Unfortunately new users to IM do not know anything about this. 不幸的是,IM的新用户对此一无所知。 They see IM as a converter that can convert say PDF to Postscript, producing images with 'blocky' aliasing effects, 'washed out' colors, or blurry images that just do not look good at all, on the intended output device. 他们将IM视为可以将PDF转换为Postscript的转换器,从而在预期的输出设备上产生具有“块状”混叠效果,“褪色”的颜色或看起来根本不佳的模糊图像。

What is said here about PDF to Postscript would also be true for PNG to SVG. 关于从PDF到Postscript的说法在PNG到SVG中也是如此。 And is not a feature of that library, but a basic problem of all raster-to-vector conversions. 它不是该库的功能,而是所有栅格到矢量转换的基本问题。

That said, the "best" way to convert png images to svg images is the mentioned "superficial vector image wrapper". 就是说,将png图像转换为svg图像的“最佳”方法是提到的“表面矢量图像包装器”。 This means, you would define a svg image that displays a PNG image just like a HTML page displays an image, defined as a data url. 这意味着,您将定义一个显示PNG图像的svg图像,就像HTML页面显示一个定义为数据url的图像一样。 SVG is a XML file format and could be constructed in PHP like this: SVG是一种XML文件格式,可以像这样在PHP中构建:

// $_POST validation

$str = <<<XML
<svg xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     version="1.1"
     width="$_POST['width']"
     height="$_POST['height']">
    <image xlink:href="$_POST['img']"
           width="$_POST['width']"
           height="$_POST['height']">
</svg>
XML;

$svg = new DOMDocument('1.0', 'UTF-8');
$svg->xmlStandalone = false;
$svg->loadXML($str);

$file = UPLOAD_DIR . uniqid() . '.svg';
file_put_contents($file, $svg->saveXML());

The data url string has to be copied into the svg file. 数据url字符串必须复制到svg文件中。 The values for width and height have to be replaced with the size of the canvas. 宽度和高度的值必须替换为画布的大小。 You'd have to transmit them in your post request: 您必须在您的帖子请求中发送它们:

jQuery(document).ready(function(){
    jQuery('#btnSave').click(function(){
        var pic = document.getElementById("myCanvas");
        $.ajax({
            type: "POST",
            url: "script.php",
            data: { 
                img: pic.toDataURL('image/png'),
                width: pic.width
                height: pic.height
            }
        }).done(function(o) {
            console.log('saved'); 
        });
    });
});

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

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