Reputation: 25
I was wondering if it's possible to save an generated image from my template to the MEDIA_URL? My image is generated in base64, I would like to give it a name and save it as png or jpeg to the MEDIA_URL
<img id="canvasImg" style="display:none;" src="data:image/png;base64,iVB...">
My image is generated using the html2canvas script. I then use this function to transform the canvas into an image:
function canvas2img(){
html2canvas(document.getElementById("bg"), {
onrendered: function(canvas) {
canvas.setAttribute("id", "canvas");
var dataURL = canvas.toDataURL('image/png', 1.0);
document.getElementById('canvasImg').src = dataURL;
}
})
};
Thank you!
Upvotes: 1
Views: 2388
Reputation: 4511
I won't post the full code here just some guidance, everything from StackOverflow:
First you need to send your base64 image to django using AJAX: https://stackoverflow.com/a/13198699/263989
Then get the base64 in an AJAX function:
from django.http import HttpResponse
def get_bas64(request):
if request.is_ajax():
# process the image
return HttpResponse('')
To convert the base64 string to an image using PIL https://stackoverflow.com/a/19911883/263989
Upvotes: 2