【问题标题】:svg to png conversion is not similar to svgsvg 到 png 的转换与 svg 不同
【发布时间】:2013-05-25 13:16:00
【问题描述】:

我正在创建 SVG 图像,然后将其转换为 PNG。但是,这不起作用 - PNG 没有创建或创建不正确。

SVG 示例:http://placementearth.com/977013694537154275/svg52136945630084.svg

示例 PNG:http://placementearth.com/977013694537154275/svg52136945630084.png

$im = new Imagick(); 
$im->setBackgroundColor(new ImagickPixel('transparent')); 
$svg = file_get_contents($svgImage); 
$im->readImageBlob($svg); 
$im->setImageFormat("png32"); 
$dpngImage= $imgname.'.png'; 
file_put_contents($dpngImage,$im); 

shell_exec('convert ex.svg ex.png;);

任何其他方式来做到这一点。请告诉我。

【问题讨论】:

    标签: php svg imagemagick png


    【解决方案1】:

    你不必为此使用 imagemagick..

    试试这个..

    在 svg-editor.js 中

    var exportHandler = function(window, data) {
                var issues = data.issues;
    
                if(!$('#export_canvas').length) {
                    $('<canvas>', {id: 'export_canvas'}).hide().appendTo('body');
                }
                var c = $('#export_canvas')[0];
    
                c.width = svgCanvas.contentW;
                c.height = svgCanvas.contentH;
    
                var mime = 'image/png';
                var type = 'PNG';
                var datauri = "";
                if(typeof svgCanvas.export_as != 'undefined' && svgCanvas.export_as != null && typeof svgCanvas.export_as.length != 'undefined' && svgCanvas.export_as.length != null && svgCanvas.export_as.length > 0) {
                    if(svgCanvas.export_as != 'image/png') {
                        mime = svgCanvas.export_as;
                        /* to make white backgrounf for jpeg images */
                        if(mime == 'image/jpeg') {
                            type = 'JPG';
                            data.svg = data.svg.replace('<svg width="800" height="600" xmlns="http://www.w3.org/2000/svg">', '<svg width="800" height="600" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><rect width="100%" height="100%" fill="white" />');   // baseProfile="tiny"
                            data.svg = data.svg.replace('<svg width="800" height="600" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">', '<svg width="800" height="600" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><rect width="100%" height="100%" fill="white" />');    // baseProfile="tiny"
                        }
                    }
                }
                                svgCanvas.export_as = '';
                canvg(c, data.svg, {renderCallback: function() {
                    // var datauri = c.toDataURL('image/png');
                    var datauri = c.toDataURL(mime);
                                        $.ajax({
                                            type:"post",
                                            url: 'create.php',
                                            data:{
                                                'datauri' : datauri,
                                                'type' : type
                                            },
                                            success: function(data) {
                                                console.log(window.location.toString().replace('svg-editor.html',data));
                                                exportWindow.location.href = window.location.toString().replace('svg-editor.html',data);
                                            }
                                        });
                    // exportWindow.location.href = datauri;
                    var done = $.pref('export_notice_done');
                    if(done !== "all") {
                        //var note = uiStrings.notification.saveFromBrowser.replace('%s', 'PNG');
                        var note = uiStrings.notification.saveFromBrowser.replace('%s', type);
    
                        // Check if there's issues
                        if(issues.length) {
                            var pre = "\n \u2022 ";
                            note += ("\n\n" + uiStrings.notification.noteTheseIssues + pre + issues.join(pre));
                        }
    
                        // Note that this will also prevent the notice even though new issues may appear later.
                        // May want to find a way to deal with that without annoying the user
                        $.pref('export_notice_done', 'all');
                        exportWindow.alert(note);
                    }
                }});
            };
    

    创建.php

    <?php
    $imgFile = "generated/test-".time().".".strtolower($_POST['type']);
    $fh = fopen($imgFile, 'w');
    // ='data:image/png;base64,data:image/png;base64
    $image_content = $_POST['datauri'];
    $image_content = substr($image_content, strpos($image_content,'base64,')+7);
    fwrite($fh, base64_decode($image_content));
    fclose($fh);
    echo $imgFile;
    exit;
    ?>
    

    使用 URI 中的图像内容并将其写入文件

    【讨论】:

    • 这应该是一种非常简单的再现用户所见内容的方法。
    • $imgFile = time()."t.png"; $fh = fopen($imgFile, 'w'); $image_content = 'svg52136945630084.svg'; $image_content = substr($image_content, strpos($image_content,'base64,')+7); fwrite($fh, base64_decode($image_content)); fclose($fh);回声 $imgFile;出口;我试过了,但没有创建图像。如果我用错了,请告诉我。
    • @user2115891 你是否改变了 svg-editor.js 中的 exportHandler 函数?
    • @BhuvanRikka웃 过去我一直认为我的标记渲染与用户在野外看到的相同...我看到了很多好处在这种方法中。
    • 我没有使用 js 文件,因为 svg 已经通过拖放方法创建。
    猜你喜欢
    • 2016-05-24
    • 2015-08-15
    • 2012-07-09
    • 2012-09-05
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    相关资源
    最近更新 更多