How save image from canvas tag to php server?

前端 未结 3 1339
无人共我
无人共我 2021-01-06 05:22

I have a javascript code like this

var testCanvas = document.getElementById(\'canvas-1\');
var canvasData = testCanvas.toDataURL(\"image/png\");
var ajax = n         


        
3条回答
  •  旧时难觅i
    2021-01-06 06:14

    Here is what I do save image from canvas via ajax. I use jQuery on client side

     jQuery.ajax({
         url: 'save.php',
         type: 'POST',
         data: {
             data: c.toDataURL('image/png')
         },
         complete: function(data, status)
         {
             if(status=='success')
             {
                alert('saved!');
             }
             alert('Error has been occurred');
         }
    
     });
    

    php:

        $based64Image=substr($_POST['data'], strpos($_POST['data'], ',')+1);
    
        $image = imagecreatefromstring(base64_decode($based64Image));
    
        $fileName='';
        if($image != false)
        {
            $fileName=time().'.png';
            if(!imagepng($image, $fileName))
            {
    //          fail;
            }
        }
        else
        {
    //          fail;
        }
    

    I hope this helps.

提交回复
热议问题