【发布时间】:2018-12-01 18:52:19
【问题描述】:
简介:我有一个帖子,用户可以在其中上传最多 8 张图片。我的部署方法不允许我的总上传(所有图像的总和超过 10mb)。所以我不能使用 Pillow 或其他在上传后减小图像大小的包。我在想如果我使用 Javascript,我什至可以在提交表单之前减小图像大小。这样,当我点击提交时,图像已经减少,所有图像的总和小于 9mb(为了安全起见)
代码借自:
使用https://github.com/josefrichter/resize/blob/master/public/preprocess.js
我不知道如何使用它们。下面是我的表单模板
这只是我的主要 post_image 我仍然需要弄清楚如何减小我的表单集图像的大小
{% extends 'posts/post_base.html' %}
{% load bootstrap3 %}
{% load staticfiles %}
{% block postcontent %}
<h2> Add a new Post</h2>
<form action="" method="post" enctype="multipart/form-data" id="form">
{% csrf_token %}
{% bootstrap_form form %}
<img id="preview" src="" width="100" />
{{formset.management_form}}
{% for f in formset %}
<div style="border-style: inset; padding:20px;">
<p class="text-warning">Extra Image {{forloop.counter}}</p>
{% bootstrap_form f %}
<img src="" width="60" id="preview-extra{{forloop.counter}}"/>
</div>
{% endfor %}
<br/><br/><input type="submit" class="btn btn-primary" value="Post"/>
</form>
<script >
var fileinput = document.getElementById('fileinput');
var max_width = 500;
var max_height = 500;
var preview = document.getElementById('preview');
var form = document.getElementById('form');
function processfile(file) {
if( !( /image/i ).test( file.type ) )
{
alert( "File "+ file.name +" is not an image." );
return false;
}
// read the files
var reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = function (event) {
// blob stuff
var blob = new Blob([event.target.result]); // create blob...
window.URL = window.URL || window.webkitURL;
var blobURL = window.URL.createObjectURL(blob); // and get it is URL
// helper Image object
var image = new Image();
image.src = blobURL;
//preview.appendChild(image); // preview commented out, I am using the canvas instead
image.onload = function() {
// have to wait till it is loaded
var resized = resizeMe(image); // send it to canvas
var newinput = document.createElement("input");
newinput.type = 'hidden';
newinput.name = 'images[]';
newinput.value = resized; // put result from canvas into new hidden input
form.appendChild(newinput);
}
};
}
function readfiles(files) {
// remove the existing canvases and hidden inputs if user re-selects new pics
var existinginputs = document.getElementsByName('images[]');
var existingcanvases = document.getElementsByTagName('canvas');
// it is a live list so removing the first element each time DOMNode.prototype.remove = function() {this.parentNode.removeChild(this);}
while (existinginputs.length > 0) {
form.removeChild(existinginputs[0]);
preview.removeChild(existingcanvases[0]);
}
for (var i = 0; i < files.length; i++) {
processfile(files[i]); // process each file at once
}
fileinput.value = ""; //remove the original files from fileinput
// TODO remove the previous hidden inputs if user selects other files
}
// this is where it starts. event triggered when user selects files
fileinput.onchange = function(){
if ( !( window.File && window.FileReader && window.FileList && window.Blob ) ) {
alert('The File APIs are not fully supported in this browser.');
return false;
}
readfiles(fileinput.files);
};
// === RESIZE ====
function resizeMe(img) {
var canvas = document.createElement('canvas');
var width = img.width;
var height = img.height;
// calculate the width and height, constraining the proportions
if (width > height) {
if (width > max_width) {
//height *= max_width / width;
height = Math.round(height *= max_width / width);
width = max_width;
}
} else {
if (height > max_height) {
//width *= max_height / height;
width = Math.round(width *= max_height / height);
height = max_height;
}
}
// resize the canvas and draw the image data into it
canvas.width = width;
canvas.height = height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);
preview.appendChild(canvas); // do the actual resized preview
return canvas.toDataURL("image/jpeg",0.7); // get the data from canvas as 70% JPG (can be also PNG, etc.)
}
</script>
{% endblock %}
我希望图像大小减少到 400kb。如果用户上传的数量少于该数量,则无需调整大小
在尝试您的解决方案时遇到以下错误
【问题讨论】:
-
如果没有 Javascript 的帮助,我认为不可能做到这一点。
-
是的,关系应该是多对多的
-
看看这个答案stackoverflow.com/questions/19262141/…。你可以使用javascript画布
-
@NathanDo 将尝试在我的代码中实现这一点,谢谢
标签: javascript django django-forms javascript-objects dom-events