частей кода, помогающих решить эту проблему:
views.py:
import re
import io
import base64
from django.core.files import File
from django.shortcuts import render
from .forms import StoreImageForm
def upload_canvas(request):
form = StoreImageForm()
if request.method == 'POST':
image_base64 = request.POST.get('image_base64', '')
res = re.match(r'^([^,]*),(.*)$', image_base64)
if res:
ext = re.match(r'^data:image/(.+);base64$', res.groups()[0]).groups()[0]
image = base64.b64decode(res.groups()[-1])
buf = io.BytesIO(image)
form = StoreImageForm(files={'upload_file': File(buf, name=f'name.{ext}')})
try:
form.is_valid()
except Exception as err:
return render(request, 'form.html', {'message': err, 'form': form})
instance = form.save()
return render(request, 'form.html', {'message': 'Image Uploaded Successfuly', 'form': form})
return render(request, 'form.html', {'message': 'Image Upload Failed', 'form': form})
return render(request, 'form.html', {'message': 'Upload Image...', 'form': form})
form.html:
<html>
<head>
<title>Upload Canvas</title>
<script lang="javascript">
function resize_image(event) {
var canvas = document.getElementById("my_canvas");
var ctx = canvas.getContext("2d");
var reader = new FileReader();
var img = new Image();
var type = '';
var ratio = 1;
img.onerror = function(e) {
console.log("Not ok", e);
}
img.onload = function (img_onload_event) {
canvas.height = canvas.width * (img.height / img.width);
// step 1 - resize to 50%
var oc = document.createElement('canvas'),
octx = oc.getContext('2d');
oc.width = img.width * ratio;
oc.height = img.height * ratio;
octx.drawImage(img, 0, 0, oc.width, oc.height);
// step 2
octx.drawImage(oc, 0, 0, oc.width, oc.height);
// step 3, resize to final size
ctx.drawImage(oc, 0, 0, oc.width, oc.height, 0, 0, canvas.width, canvas.height);
var dataURL = oc.toDataURL(type, ratio)
// var blob = dataURItoBlob(dataURL)
b64 = dataURL;
padding = (b64.charAt(b64.length - 2) === '=') ? 2 : ((b64.charAt(b64.length - 1) === '=') ? 1 : 0);
fileSize = (b64.length * 0.75 - padding) / 1024;
if(fileSize > 500) {
img.src = b64;
return;
}
var my_image_base64 = document.getElementById('my_image_base64')
my_image_base64.setAttribute("value", dataURL)
}
reader.onload = function (e) {
b64 = reader.result;
padding = (b64.charAt(b64.length - 2) === '=') ? 2 : ((b64.charAt(b64.length - 1) === '=') ? 1 : 0);
fileSize = (b64.length * 0.75 - padding) / 1024;
if(fileSize > 500){
ratio = 0.8
}
img.src = e.target.result;
}
type = event.target.files[0].type || 'image/jpeg';
reader.readAsDataURL(event.target.files[0]);
}
window.onload = function(){
document.getElementById('my_image').addEventListener('change', resize_image, false);
}
</script>
</head>
<body>
<h2>{{ message }}</h2>
<form method="post">
{% csrf_token %}
<input type="file" id="my_image" />
<input type="hidden" id="my_image_base64" name="image_base64" />
<button id="upload_button"> Upload </button>
</form>
<br/>
<canvas id="my_canvas" width="500" />
</body>
</html>
forms.py:
from django import forms
from .models import UploadModel
class StoreImageForm(forms.ModelForm):
class Meta:
model = UploadModel
fields = ['upload_file']
models.py:
from django.db import models
class UploadModel(models.Model):
upload_file = models.ImageField()
См. Мой image-minimizer-uploader проект в github.