dpk20361 2013-12-19 12:51
浏览 71

如何使用php和ajax将Canvas元素保存到png文件?

I want to save a html canvas element as an image using php and jquery ajax. Here is my code for ajax.

   var  front_image=canvas.toDataURL("image/png");
//front image is a base_64 string
                 $.ajax({
                  url:base_url+'tabs/profile/save_front_image',
                  type:'POST',
                  data:'front_image='+front_image,
                  success:function(response){


                  }
              });

I m just doing echo in php echo $_POST['front_image'] so request and response are same.

When i use this code before ajax it loads image to new tab of browser

 var w = window.open('about:blank', 'image from canvas');
   w.document.write("<img src='" + frame_image + "' alt='from canvas'/>");

but when i put the same code on ajax response as bellow it doesn't work. Only a blank tab opens in browser. So i m not being able to save image as file.

var w = window.open('about:blank', 'image from canvas');
 w.document.write("<img src='" + response + "' alt='from canvas'/>");

I compared string length of frame_image and response also. They are same. I m not sure why image is not loading in response. Please suggest me the answer thanks.

  • 写回答

1条回答 默认 最新

  • dongshangan2074 2013-12-19 13:13
    关注
    // soon you can use front_image=canvas.toBlob("image/png")
    
    // construct a blob
    function b64toBlob(b64Data, contentType, sliceSize) {
        contentType = contentType || '';
        sliceSize = sliceSize || 512;
    
        var byteCharacters = atob(b64Data);
        var byteArrays = [];
    
        for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
            var slice = byteCharacters.slice(offset, offset + sliceSize);
    
            var byteNumbers = new Array(slice.length);
            for (var i = 0; i < slice.length; i++) {
                byteNumbers[i] = slice.charCodeAt(i);
            }
    
            var byteArray = new Uint8Array(byteNumbers);
    
            byteArrays.push(byteArray);
        }
    
        var blob = new Blob(byteArrays, {type: contentType});
        return blob;
    }
    
    // make an actually file from the base64 so we can send binary data
    var blob = b64toBlob(front_image.split(",")[1], "image/png")
    var fd = new FormData();
    
    fd.append("file", blob, "filename.png");
    
    $.ajax({
        url: 'http://example.com/script.php',
        data: fd,
        processData: false,
        contentType: false,
        type: 'POST',
        success: function(data){
            alert(data);
        }
    });
    

    // The saving

    <?php
    $uploads_dir = '/uploads';
    foreach ($_FILES["file"]["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            $tmp_name = $_FILES["file"]["tmp_name"][$key];
            $name = $_FILES["file"]["name"][$key];
            move_uploaded_file($tmp_name, "$uploads_dir/$name");
        }
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?