cui
cui

Reputation: 23

How to insert canvas image into the database after i converting to base64

Javascript

document.getElementById("input1").value=canvas.toDataURL('image/png');

codetreatment.php

 $data = $_POST["input1"];
    $data = str_replace('data:image/png;base64,', '', $data);
    $data = str_replace(' ','+',$data);

    $data = base64_decode($data);

php:

    <form action="codetreatment.php" method="post">
 <input type="text" name="input1" id="input1" >
     <button  type="submit" name="treatmenthistoryupdatebtn" class="btn btn-primary">Save</button>
</form>

But this code for database didn't work.

What is the next step after converting the canvas image to base64 string? I want to save to the database

Upvotes: 0

Views: 358

Answers (1)

Sumeet Amipara
Sumeet Amipara

Reputation: 40

DATABASE

$data = $_POST["input1"];       
$data = explode(",", $data)[1];
$decoded_image = base64_decode($data);
$temp_name = 'http://your-domain.com/folder/'.md5(time().rand().time()).".png";
file_put_contents($temp_name, $decoded_image);
// Store this $file to table.
$file = basename($temp_name);

Upvotes: 2

Related Questions