简体   繁体   中英

Storing and Retrieving Image Data URL

Is it possible to store 'Data URL' extracted from a canvas by toDataURL("image/png"); in a database and reconstruct the image by retrieving this 'Data URL'? I have tried to store the 'data URL' to a BLOB .

toDataURL("image/png") method returns a data URI only. You can store that to sql database easily and the same can be retrieved and used to construct the image. You will have to set the src of the image with the retrieved data URI. Please refer this one also, this should give you an idea.

I don't understand all you want to make, but here is a little example of the process I used.

First convert the canvas to an imageUrl with canvas.toDataURL() which returns a DataURL of the canvas in .png format.

Client:

var canvas=document.getElementById("canvas");
var dataURL=canvas.toDataURL();

$.ajax({
  type: "POST",
  url: "PHPfile.php",
  data: {
     image: dataURL
  }
})

PHP:

<?php

    $conn = new PDO('mysql:host=XXXX;dbname=YYY', "ZZZ", "1234");

    $insert="insert into designs(image) values(:image)";        
    $stmt = $conn->prepare($insert);
    $stmt->bindValue(":image",$_POST["image"]);
    $stmt->execute();

Now you have the image stored on your DB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM