通过AJAX和PHP传递画布图像dataURL进行解码和保存服务器端

时间:2017-09-30 07:53:26

标签: javascript php jquery ajax canvas

客户端,从div的内容创建图像(canvas.toDataURL):

        ...                
                    var img = new Image();
                    var dataUrl;
                    img.onload = function() {
                        //alert($('#top-content').width());
                        context.canvas.width = $('#divID').find('svg').width();
                        context.canvas.height = $('#divID').find('svg').height();
                        var width2heightratio = context.canvas.width / context.canvas.height;
                        //return;
                        context.drawImage(img, 0, 0);
                        // freeing up the memory as image is drawn to canvas
                        DOMURL.revokeObjectURL(url);

                        if (isIEBrowser()) { // Check of IE browser
                            var svg = $('#divID').highcharts().container.innerHTML;
                            canvg(canvasIE, svg);
                            dataUrl = canvasIE.toDataURL();
                        }
                        else{
                            dataUrl = canvas.toDataURL('image/png');
                        }
                        var dataString = 'img=' + dataUrl;
                        $.ajax({
                            url: "resources/assets/php/reports_generate_pdf.php",
                            type: "POST",
                            dataType:'json',
                            data: dataString,
                            cache: false,
                            success: function(data){

                                alert('success');

                            },
                            error: function(XMLHttpRequest, textStatus, errorThrown) {
                                $('#MsgDiv').html('XMLHttpRequest: ' + XMLHttpRequest + '<br>textStatus: ' + textStatus + '<br>errorThrown: ' + errorThrown);
                            }
                        })
                    };
                    img.src = url;

            function isIEBrowser(){
                var ieBrowser;
                var ua = window.navigator.userAgent;
                var msie = ua.indexOf("MSIE ");

                if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // Internet Explorer
                {
                    ieBrowser = true;
                }
                else  //Other browser
                {
                    console.log('Other Browser');
                    ieBrowser = false;
                }

                return ieBrowser;
            };
    ...

PHP:

<?php

$img= $_POST['img']; //data 'data:image/png;base64,~;
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$upload_dir='/public/images/';
$file = $upload_dir."image_name.png";
$success = file_put_contents($file, $data);

?>

我正在寻求能够将图像服务器端保存在/public/images/目录中。任何建议都表示赞赏。

1 个答案:

答案 0 :(得分:1)

$upload_dir是绝对路径。

如果您的public文件夹与脚本位于同一文件夹中,则应为$upload_dir='public/images/';